Question

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.

Was it helpful?

Solution

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();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top