Prof. Monster's EventManager - mapping Keyboard and MouseInput


Hello Blenderheads,

I extended the KeyMapper to work with MouseEvents as well. This allows one mapping for both Keyboard input and Mouse input. Joystick input might follow later.

Now it is possible to use simple text files as mapping configuration file with following structure:

event1, event2 … = task1, task2 …

e.g.:

AKEY = taskA
MOUSE_ABS_X = mouseX
KX_MOUSE_BUT_LEFT, SPACEKEY = taskOnSpace, anotherTask

Possible events are:

  • all values from GameType such as AKEY. Results can be (True, False)

  • KX_MOUSE_BUT_LEFT (True, False)

  • KX_MOUSE_BUT_MIDDLE (True, False)

  • KX_MOUSE_BUT_RIGHT (True, False)

  • MOUSE_ABS_X - mouse x-position (-1.0 … 0.0 … +1.0)

  • MOUSE_ABS_Y - mouse y-position (-1.0 … 0.0 … +1.0)

  • MOUSE_REL_X - difference in mouse x-position (-1.0 … 0.0 … +1.0)

  • MOUSE_REL_Y - difference in mouse y-position (-1.0 … 0.0 … +1.0)
    Short guide:

  • append or link eventMapper from the demo file

  • write your own mapping file

  • have a dummy object that has a Python controller in Script mode called mappings (do not connect anything to it) assign the name of your mapping file. E.g myMapping

  • add an delay sensor that triggers after one frame -> python controller in module mode: EventManager.mappingFromCont. Now your configuration overwrites the default configuration

  • on each of your objects that requires input add

  • an always sensor (no pulses) -> python controller in module mode: EventManager.addListener.

  • to this controller connect property sensors for each mapping you want to use:
    [LIST]

  • in Prop: mappingName (e.g. “forward”)

  • in Value: the name of the property to be mapped to (e.g. “up”)

  • Now everytime the input can be mapped to “forward” the property “up” is set. If you press “W” and this is mapped to “forward”, “up” is set to 1 otherwise 0.

  • Attention: These sensors should never trigger the controller (the always sensor is doing this). So be careful when choosing mapping and property names.

  • now add any logic that checks the property up.
    [/LIST]
    To read the configuration from internal file use EventManager.mappingFromCont. It requires an Python script controller named mappings.

To read the configuration from external file use EventManager.mappingFromFile. It requires a property named mappingFile containing the filename.


BGE callable functions:

  • EventManager.mappingFromCont
    [LIST]

  • Reads the mapping from the internal textfile referenced by the controller “mappings”.

  • EventManager.mappingFromFile

  • Reads the mapping from external textfile named by property “mappingFile”.

  • EventManager.processInput

  • Requires at least one sensor (Mouse or Keyboard).

  • Processes the input from the sensors. Updates the registered properties of the listener objects.

  • The updated value is True/False for key events (incl. mouse keys).

  • The updated value is -1.0 … 0.0 … +1.0 for mouse events.

  • EventManager.addListener

  • Requires at least one property sensor

  • Registers this object as listener

  • The property sensor’s
    [LIST]

  • propName - defines the task

  • value - defines the property name that should receive the mapping result.

  • Should by triggered by an Always sensor (no Pulses).
    [/LIST]

  • EventManager.setCenterMode

  • Can be called by any object by any sensor.

  • Switches the mouse input to center mode
    [LIST]

  • In center mode the mouse cursor is reset to the middle of the screen, everytime the eventManager evaluates the mouse input

  • Center mode is good when the mouse should not left the window area (e.g. fps mouselook)

[/LIST]

  • EventManager.setFreeMode

  • Can be called by any object.

  • Switches the mouse input to free mode.
    [LIST]

  • In free mode the mouse cursor remains untouched as moved by the mouse.

  • Free mode is good when the mouse should point into the window area (e.g. cut and paste).

  • In free mode the mouse cursor might leave the windows area.

  • Free mode is the default.

[/LIST]

  • EventManager.printListeners

  • Prints a list of tasks and the mapped listener objects with properties to the console.

  • If a listener object is not in the list, it is not mapped.

  • EventManager.printEvents

  • Prints a list of the current input events to task mappings to the console.

  • This list can be used as input for a mappings file.

  • If an event or task is not there it is not mapped.

[/LIST]The demo file contains an object called eventManager. You can simply link or append this object to your file. The object brings the necessary modules as script with it. Add an internal text file named “mappings”. Store your own configuration in this file.

[LIST]

  • The eventManager needs at least one keyboard sensor for key mapping.
  • The eventManager needs at least one mouse sensor for mouse mapping. Unfortunatly to correctly react on mouse key input one mouse sensor per mouse mode is required.

[/LIST]Than you need your objects previously listening to keyboard or mouse sensors to switch to property sensors. Let the objects register themselfs as listener. See OBCube or OBTest.


Attachments

EventManagerDemo1.0.blend (233 KB)

Good Script