# Checking whether a specific key was pressed in enchantJS

I am using enchantJS and would like to use the letters and numbers as well as numpad on a keyboard to do different things (eg: hotkeys).

By default, enchant.js provides input listeners for six buttons: UP, DOWN, LEFT, RIGHT, A, and B. By default, the directions are bound to the arrow keys. Any of the six buttons may also be bound to any key with an ASCII value. We’ll address that later.

So enchant provides the ability to bind keys to different input such as up, down, left, right...but how can I simply check whether the D or X key was pressed, and if so, perform certain actions based on that event?

• Bind a keycode to an action. The page you linked to shows how. – Vaughan Hilts Aug 21 '14 at 23:12
• I am planning to use 0-9, A-Z, as well as treating numpad separately. Enchant only provides 6 buttons. Is there an extension that will provide support for full keyboard input? – MxLDevs Aug 21 '14 at 23:16
• I don't think you understand. I'll post an answer for you. – Vaughan Hilts Aug 21 '14 at 23:57

EnchantJS can bind actions to keys. Let's define both so we can get a deeper understand before we begin.

Actions. These are things you call "left" and "right". There is no actual "left" and "right" key. There are arrow keys, but these concepts of left and right are misleading. Let's think of this in a different context.

Think of the action "Jump" in a platformer game. When you want the player to jump, they need to press "JUMP".

Keys This is where keys come into play. What is a key? It's the thing on your keyboard you press -- a keycode. You assign these to actions.

Putting it all together. So, assign a keycode to an action.

game.keybind(32, 'jump');


By using this as a keybinding, I have declared 32 is jump. Now in my code, I can use:

if (game.input.jump)


To determine if I need to jump.

If you need to know what a specific keycode is, try this web page: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes