0
\$\begingroup\$

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).

From this page http://users.csc.calpoly.edu/~foaad/enchant/guide/playerInput.html

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?

\$\endgroup\$
  • \$\begingroup\$ Bind a keycode to an action. The page you linked to shows how. \$\endgroup\$ – Vaughan Hilts Aug 21 '14 at 23:12
  • \$\begingroup\$ 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? \$\endgroup\$ – That Umbrella Guy Aug 21 '14 at 23:16
  • \$\begingroup\$ I don't think you understand. I'll post an answer for you. \$\endgroup\$ – Vaughan Hilts Aug 21 '14 at 23:57
2
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.