A lightweight class for simultaneous key events, such as pressing “shift +”, or “command s”. There are a few great solutions created by others in the community, but they are typically intended for games, and implement extra features which aren’t needed in a typical UI. So here is a lightweight package, approx less than 100 lines. It uses Rob Penner’s “Signals” package for event handling, which is awesome btw.

You can listen for strict combinations ( only the specified keys ) or ignore if the user entered an extra key or two.

Example AS3:

var ryu:MultiKey = new MultiKey( [82, 89, 85] );
    ryu.entered.add( ryuCharacter.jumpKick );	

var listener:MultiKeyMaster = new MultiKeyMaster( stage )
    listener.addMultiKeys( ryu );

UPDATE: You can listen for Javascript key events, in order to bypass the need for users to click your SWF before receiving key events. To use Javascript key events add the javascript includes to your HTML, set multikeymaster.id to your SWF id, and do not pass a target to the MultiKeyMaster constructor.

Example Javascript:

<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.hotkeys.js"></script>
<script type="text/javascript" src="jquery.hotkeys.multikeymaster.js"></script>
<script type="text/javascript">
	var swfid = 'SWF';
	multikeymaster.id = swfid;
	swfobject.embedSWF("MultiKeyMaster.swf", swfid);
</script>

Checkout the interactive MultiKeyMaster DEMO featuring Ryu!

Or download the SWC, source & demo files.

NOTE There’s a bug with Flash Player and the command-key on a Mac. KEY_UP events do not dispatch for keys released while the command-key is down. The best workaround I’ve found & implemented is removing all KEY_DOWN references when the command-key is released, otherwise you end up with false positives. In my experience it is most confusing & problematic when you press the + key, and suddenly an event fires intended for a “command +” key combination. Hopefully Adobe will fix this. Please vote for it in the Flash Player Bug Reporting System.