Source Code Snippet: Making A Custom Customize Keyboard Shortcuts Feature – For A Game (ActionScript)…

Share Button

This is a simple example that I’m throwing up here because it may help some googler looking for a quick solution (just strip out the trace and txt_output).
This lets you set an alphanumeric (letters and numbers) character as a desired “keyboard shortcut” which is saved for use throughout your game (or application).

I’m currently working on the “settings” area for (if you haven’t heard yet, I love saying the title) my upcoming game, Offender 2 (World War B: War on Rabbits).

For those that are interested I have a gallery of the game’s artwork here: http://offender2.alienmelon.com/artwork/ which I’m continuously updating as I make progress. Visit it for a sneak peek of the game’s artwork, style, and more… I’m excited.
Ok. The snippet:

Preview it here:

Download the .fla here: http://nathalielawhead.com/sourcefiles/Offender2/code_examples/

Where txt_key_menu is an input text field with a 1 character limit.
The input is saved to a shared object which is checked against in the KeyboardEvent.
If the keyboard character pressed matches the SO then the desired event is triggered.

var so_keys:SharedObject = SharedObject.getLocal("keyexample/data");

if(so_keys.data.key_menu == undefined){
so_keys.data.key_menu = "m";
so_keys.flush();
};

//keyboard input restrictions
txt_key_menu.restrict = "a-z 0-9";
txt_key_menu.text = so_keys.data.key_menu;
//
txt_key_menu.addEventListener(FocusEvent.FOCUS_IN, evnt_key_menu);
txt_key_menu.addEventListener(FocusEvent.FOCUS_OUT, evnt_key_menu);
//
function evnt_key_menu(event:FocusEvent):void {
trace(event.type);
if(event.type=="focusIn"){
txt_key_menu.text = "";
}
if(event.type=="focusOut" && txt_key_menu.text == ""){
//
trace("no input selected revert to m");
txt_output.text = "no input selected revert to m";
//
txt_key_menu.text = so_keys.data.key_menu;
};
if(event.type=="focusOut"){
so_keys.data.key_menu = txt_key_menu.text;
so_keys.flush();
//
trace("shortcut is now set to: "+so_keys.data.key_menu);
txt_output.text ="shortcut is now set to: "+so_keys.data.key_menu;
//
};
}
function evnt_key_shortcut(event:KeyboardEvent){
if(String.fromCharCode(event.charCode) == so_keys.data.key_menu){
trace("the shortcut key has been pressed");
txt_output.text = "the shortcut key has been pressed";
};
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, evnt_key_shortcut);

Well… Drop by the gallery to check out the artwork, and animations (Yeah. Misspellings and all. I haven’t gotten around to that part yet). It’s come a long, and exhausting way!

So… back to work!

This monster belongs in Web/Game Dev: Tutorials & Sourcecode. Bookmark the permalink. Follow any flames or other attacks to it with the RSS feed for this abominable creature. Voice your rage or leave a /b/roback: /b/roback URL.

Post a /B/romment

Chill, /b/ro. Your email is probably not, but most likely impossibly going to be not shared. Required fields are marked, yo... *

*
*

Use these /b/ro HTML haxxx: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Beerme: