# Which code module should map physical keys to abstract keys?

How do you bridge the gap between the library's low-level event system and your engine's high-level event system? (I'm not necessarily talking about key events, but also about quit events.)

At the top level of my event system, I send out KeyPressedEvents, KeyRelesedEvents and others of this kind. These high-level events only contain the abstract values of the keys (they don't say that Space way pressed, but that the JumpKey was pressed, for example).

Whose responsibility should it be to map the "JumpKey" to an actual key on the keyboard?

• What do you mean by 'who'? – Kylotan Dec 4 '11 at 22:34
• I believe Paul Manta is asking, in an OOP context, how to translate physical key/button presses to ingame events (e.g. how to make space = jump). Specifically: which object(s) should have that responsibility, and roughly how would they work? This exact issue has already been answered in Keyboard input system handling. – doppelgreener Dec 5 '11 at 0:04
• I'm tempted to edit the title to "Which code module should map physical keys to abstract keys?" but I'll let paul do that to be sure we're understanding him correctly. – jhocking Mar 21 '12 at 12:41

You probably have a message pump somewhere in your gameloop where you receive the keypress messages from your system. That's usually where you'd use a class like 'keymapper' or something, which holds a map of keys and actions/events. So, you'd look at all keys changed, lookup their mapped action/event and fire that. For example, releasing 'w' might resolve to 'w is no longer pressed' which in turn might be looked up to be something like '-forward' or 'playerForwardMovementDecreaseEvent'.

If you'd like another perspective on this, check out this blog post about "Wiring physical devices to abstract inputs". It covers a lightweight mapping framework in close detail and demonstrates a fundamentally different approach to the problem.

It basically works as follows: Input from all devices is gathered in a structure that can be queried from all systems that want to subscribe to input. A system can then define "Actions" (single keystrokes or chords) for which the abstract device structure is queried. If the specified action has been performed on the device, the ()-operator of the action returns true, indicating that the "input condition" the system has been waiting for is fulfilled.

Note: This system relies on C++ templates to avoid run time type identification. If you are not coding in C++ you can probably just use a regular dispatch mechanism provided by your language as performance will probably not be your primary concern then.