Skip to content

Key Mapping

Rotciv Ocnarb edited this page Nov 4, 2018 · 1 revision

Currently there are two ways of handling Input from the player, de default one, using InputProcessor inherited methods (All GameObjects and State have those implemented and already working, you don't need to set the InputProcessor). But that method fails a little bit when you want to support multiple different inputs for the same action (for example, player jump with the arrow keys, W key, or a Controller Button). That's why i created a KeyMapping mechanism, that can handle that. Besides all InputProcessor methods, all Objects and States also have those:

inputIn(Device device, String mapName);

and

inputOut(Device device, String mapName)

Those methods are fired whenever a keyMapping is fired, and it tells you the name of the map, and wich device is firing it (Keyboard and Controller).

To make a use of KeyMappings, you can simply use the stateManager (located in the State class) to register the inputs:

		manager.registerKey("Jump", Device.KEYBOARD, Keys.UP);
		manager.registerKey("Jump", Device.KEYBOARD, Keys.W);
		manager.registerKey("Jump", Device.CONTROLLER, XBoxController.BUTTON_A);

That way the method inputIn() and inputOut() will be called whenever one of those three input are pressed, and you only have to code once!

	public void inputIn(Device device, String mapName) {
		if(mapName.equals("Jump")) {
			//Jump the player!
		}
	}

Clone this wiki locally