Avg. Rating 1.0

Problem

In a Flex login screen after you enter user id and password you must click on a button to invoke login. Want to be able to either click on the button or simply click the "Enter" key.

Solution

Simply add a keyboard event listener to your Login component and make sure you remove it once the login screen disappears.

Detailed explanation

Adding a keyboard event listener in AS3 is easy. All you have to remember is to add the listener for keyboard input for the entire Stage.

stage.addEventListener(KeyBoardEvent.KEY_DOWN, keyDownHandler);

The second thing to consider is another component in your application might listen to the ENTER key. So you have to remove the listener on the component once you're logged. You can listen for the show & hide events on your login component to add & remove key listeners.

In Flex, you can only access the stage only once the applicationComplete event has been dispatched. So you have to wait to receive this event before adding the key listener the first time.

You'll find attached an example of this.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes