Skip to content

Instantly share code, notes, and snippets.

@jgranick
Created February 14, 2012 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgranick/1830627 to your computer and use it in GitHub Desktop.
Save jgranick/1830627 to your computer and use it in GitHub Desktop.
How to bind keyboard events using the "KeyBinding" library (NME recipe)
import com.eclecticdesignstudio.control.KeyBinding;
import nme.display.Sprite;
import nme.text.TextField;
/**
* @author Joshua Granick
*/
class KeyboardTest extends Sprite {
private var label:TextField;
public function new () {
super ();
KeyBinding.addOnPress ("a", Keyboard_onPressA);
KeyBinding.addOnRelease ("a", Keyboard_onReleaseA);
}
// Event Handlers
private function Keyboard_onPressA ():Void {
// user pressed the A key
}
private function Keyboard_onReleaseA ():Void {
// user released the A key
}
}
@jgranick
Copy link
Author

Although you can listen to keyboard events directly, the KeyBinding library makes it easier to bind or unbind actions to keys on the keyboard.

You can install it with "haxelib install keybinding" from a command-prompt or terminal, then you can include it in your project with <haxelib name="keybinding" /> in your NMML project file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment