• About WordPress
    • WordPress.org
    • Documentation
    • Learn WordPress
    • Support
    • Feedback
  • Log In
Skip to content

MonoFlauta

Indie Game Developer

Menu
  • Blog
  • Portfolio
    • Games
      • Happy Ghost Studio
      • Etermax
      • Silky Smooth Studio
      • Fun+Mono
    • Frameworks & Tools
      • Framework Goat
      • Twitch Mono Integration
      • Framework Mono
      • Narwhal Engine
    • Others
  • Store
  • About Me

Framework Mono – Managers/InputManager.as

cropped-icono.jpgThe 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

Powered by WordPress MonoFlauta 2025 | All Rights Reserved.

Manage Cookie Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}