The InputManager.as script will let you register keys in order to be able to modify controls during runtime or just register keys in order to have an easy way to change them in the future. Note: This script needs a public static instance inside Main.as of Mono.as.
Initial set up InputManager.as
In order to use it, first you will need a public static instance of Mono.as in your Main class. Once you have it, you will be able to use it without any problem. You can create a new var with an instance of InputManager.as or you can just the one that is inside Mono and will let you have acces from anywhere. For example, in case you want to do the second option (recommended), you should do the following:
package { import Mono.Managers.InputManager; import Mono.Mono; import flash.display.Sprite; public class Main extends Sprite { public static var mono:Mono; public function Main() { mono = new Mono(stage); mono.inputManager = new InputManager(); } } }
Now you will be able to access from anywhere by accessing to mono first.
Detecting pressed keys, were pressed keys and realeased keys
If you just want to know if keys are pressed, were pressed or are just released, you can simple do it using the InputManager.as without needing to create the listeners by your own. In order to do that, you will just need to do one of the followings depending on what you want to do (assuming you created the instance inside Mono.as):
if(Main.mono.inputManager.getKeyPressed(Keyboard.A)) //if A is pressed { //Do something }
if(Main.mono.inputManager.getKeyReleased(Keyboard.A)) //if A was just released { //Do something }
if(Main.mono.inputManager.getKeyWasPressed(Keyboard.A)) //if A was released { //Do something }
As you can see, you will only need to call the wanted function and pass as parámeter the key you want to detect.
Registering keys and detecting if they are pressed, were pressed or released by their name
Another feature this class has, is that it will let you register keys by names and then detect them by their names. It is recommended to save const with the name of the key, for example “jump”, and call it from there.
To register keys you will only need to do the following:
Main.mono.inputManager.addRelationKey(Keyboard.SPACE, "jump");
Now, we can do the same as we did with getKeyPressed, getKeyReleased and getKeyWasPressed but using the relation key:
if(Main.mono.inputManager.getKeyPressedByName("jump")) //If jump key is pressed { //Do something }
if(Main.mono.inputManager.getKeyReleasedByName("jump")) //If jump key was just released { //Do something }
if(Main.mono.inputManager.getKeyWasPressedByName("jump")) //If jump key was pressed { //Do something }
Note that now, you can easily change controls by just changing the relation key during runtime. For example, I could change the relation key of jump and use the W instead by doing the following:
Main.mono.inputManager.addRelationKey(Keyboard.W, "jump");
Or you could just use the recording feature by doing the following:
Main.mono.inputManager.recordKeyOnRelease("jump");
This will record the next key released. That one will be set as relation key with the name you passed, in this case “jump”. It should be very usefull when letting the user to config his controls.
Public Functions
- addRelationKey(code:int, name:String):void
- getKeyPressed(code:int):Boolean
- getKeyWasPressed(code:int):Boolean
- getKeyReleased(code:int):Boolean
- getKeyPressedByName(name:String):Boolean
- getKeyWasPressedByName(name:String):Boolean
- getKeyReleasedByName(name:String):Boolean
- recordKeyOnRelease(name:String):void
Private Functions
- evKeyDown(e:KeyboardEvent):void
- evKeyUp(e:KeyboardEvent):void
- onReleaseForRecorded(e:KeyboardEvent):void
Private Vars
- _keys:Array
- _keysByName:Dictionary
- _recordingKey:String