Input Mapper Module - Configure device input mapping in a very easy and powerful way.

Hi guys, I recently had a project where I needed to create a first-person walk demo within the Blender Game Engine. I started off searching for already made solutions, but eventually decided to write it myself from scratch. I’m relatively new to Blender’s game engine, but I have come up with a really handy input module that I thought people might find useful.

Basically, it is a key mapper of sorts, except it also includes mouse and joystick input. You just ask it for a specific input and it returns you a result based on that input. The input you give it is just a string value, for example: “key.w”. You can keep it simple, or move to some more complex strings like: “key.w, key.i”, which will react to either the ‘w’ or ‘i’ key’s, or “joy.forward + joy.button.trigger”, which will only activate if you are holding both forward and the trigger button together, or “joy.hat.up.pressed” which will only react once as soon as the joystick hat switch up direction has been pressed. These can also be combined, “key.w, mouse.up + mouse.button.left”, which with react to either the ‘w’ key, or if you are moving the mouse up while holding the left mouse button.

Once you ask the input mapper with the magic string, it returns you back a number between 0 and 1, 0 meaning inactive. Values in between are usually from analog controls like joysticks. From there, you can perform any actions you wish. For example: if I had two controls, forward and back, assigned each one the result of a magic string, you now have the amount of forward and the amount of back to move your character. For controls like jump, you just need to see if the value is greater than 0 and you’ll know if the user intends to jump or not.

The module also adds some functionality not supported by the standard joystick sensor api, unlike the rest of the sensors where you get an event as soon as a button has been activated, another event while it remains active, and a final event when the button has been released, the joystick sensor does not do this, so my input mapper mimics this task internally so it is now possible to check those states without having to keep track of it yourself. It also supports multiple joysticks and multiple hat switches.

Also, in case you are unsure what button index a controller has, or perhaps which joystick axis, it also comes with a very handy poll() function that prints out in the console log exactly what inputs it has detected and in the format that you can use as your magic string. All you have to do is call the function during your update, and then press or move your input devices so they register as being changed.

I have very detailed instructions within the actual script file that should better explain how to use the module at its fullest. I hope you enjoy it.

Feel free to take a look, modify, and share it. You can find the source here:

Thanks,

  • Lochemage