Version 1, last updated by Kirill Mourzenko at September 13, 2008 22:19 UTC
Keyboard Input
HATKeyboard class is the central class for intercepting input from the keyboard. It has a singleton instance you can access using the static instance property. Whenever a user types something it will send a HATKey event object for the state of the key pressed and the key that was pressed. So you can handle just the HATKey.DOWN event and receive events about every key that was pressed or handle a specific key event like HATKey.ENTER or "x" and receive events about it when it becomes pressed or released.
Example:
import com.kirillam.handy.input.HATKeyboard;
import com.kirillam.handy.events.HATKey;
HATKeyboard.instance.handle( HATKey.DOWN, this, onKeyPress );
HATKeyboard.instance.handle( "a", this, onAKey );
// Receives DOWN events about all keys.
function onKeyPress( e : HATKey )
{
traceI( e.key + " " + e.state )
}
// Receives DOWN and UP events about the "a" character key.
function onAKey ( e : HATKey )
{
traceI( e.key + " " + e.state )
}
When you need to know when a combination of keys was pressed HATKeyCombo class can help. HATKeyCombo is for capturing keyboard shotcuts-like sequences of key strokes. To be a valid key combo no modifier key ( HATKey.SHIFT and HATKey.CONTROL ) can be pressed after a non-modifier key was already pressed and no key that is pressed after at least one key is released will count towards the sequence. Once all keys are released it will start waiting for the next key sequence. It has a non-standard way of notifying about key combos. The way you use it is this:
import com.kirillam.handy.input.HATKeyCombo;
import com.kirillam.handy.events.HATKey;
import com.kirillam.handy.events.HATEvent;
HATKeyCombo.instance.handle( HATKeyCombo.format( HATKey.DOWN, HATKey.SHIFT, HATKey.ENTER ), this, onShiftEnter );
function onShiftEnter( e : HATEvent )
{
traceI( "SHIFT ENTER DOWN" )
}
What the static format() method does is it takes the state you want to handle plus all the keys that are part of the sequence. Only the order of the non-modifier keys matters. Modifier keys can be passed in in any order and not necessarily before non-modifier keys. The format method takes those keys and puts them into a string that can be used to handle an event sent by the class. The state must be present as an argument. When the user enters a sequence of keys that correspond to the keys you're interested in an event with the same name as the one returned by the format() method will be sent.