문제

I have a flash movie I am making that has a search box and a search button. The button has this code:

on (release, keyPress "<Enter>") {
    searchbox.execute();     
    /*the function above processes searches*/
}

Clicking on the button works just fine. Pressing Enter doesn't do a bean! Does anyone know why this is, and any ways I can work around it? I'd prefer not to use listeners if I can possibly avoid it at all.

도움이 되었습니까?

해결책

Using on() is a deprecated AS1 practice, so maybe you should just stop using it. Thanks to the onKeyDown event handler of the MovieClip class, it is possible to use proper code to do it without listeners, so you don't have to worry about them. ;)

Anyway, on with the code. Type this into the timeline which contains the button:

//Enable focus for and set focus to your button
searchButton.focusEnabled = true;
Selection.setFocus(searchButton);

//The onRelease handler for the button
searchButton.onRelease = function(){
    //You need this._parent this code belongs to the button
    this._parent.searchbox.execute();
}

//The onKeyDown handler for the button
searchButton.onKeyDown = function(){
    //Key.getCode() returns the key code of the last key press
    //Key.ENTER is a constant equal to the key code of the enter key
    if(Key.getCode() == Key.ENTER){
        this._parent.searchbox.execute();
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top