Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser key code list? [closed]

I know this is probably going to be in vain but I want to see if there is an answer. I'm making an HTML5 game and I'm trying to get keyboard input. Please tell me someone knows something google doesn't. Hopefully at least it'll stress how much keyboard events and keycodes need to be made more cross-browser.

Please tell me there is some kind of object in the javascript api that lists each key's keycode. And if not, why hasn't it been done yet, just being able to grab a key's code would make our jobs so much easier. No more testing the inconsistencies per browser/os.

And if not that (this is probably more in vain) is there a way to redefine the default keycodes to our own custom keycodes?

I don't know why more work hasn't been done to make this more convenient?

like image 211
Isaiah Avatar asked Oct 08 '11 02:10

Isaiah


People also ask

What is e keycode === 13?

Keycode 13 is the Enter key.

Why is my keycode deprecated?

KeyCode was deprecated because in practice it was “inconsistent across platforms and even the same implementation on different operating systems or using different localizations.” The new recommendation is to use key or code .

What can I use instead of charCode?

Note: The charCode property is deprecated. Use the key property instead. The charCode property returns the Unicode character code of the key that triggered the onkeypress event.


2 Answers

I find this page to be very helpful. Also quirks mode has a good article on it. Fact of the matter is that there's browser inconsistencies and a javascript framework like jQuery help with that.

like image 184
aziz punjani Avatar answered Nov 03 '22 01:11

aziz punjani


JavaScript does not include a built-in hash of keycode-to-keyname pairings.

It's largely unnecessary because JS uses the same keycodes as are reported by the Operating System. Adding a hash would only hinder the performance unnecessarily.

The program doesn't need to know what the name of the key was, it just needs to know what action to take.

Instead of writing code like:

if (e.keyCode === customKeyList.UP)
{
  doUp();
}

You can simply set your own hash of actions:

//before the event
actions = {
  '38': function () {
    //do UP stuff here
  },
  '40': function () {
    //do DOWN stuff here
  }
};
//during the event
if (actions[e.keyCode]) {
  actions[e.keyCode]();
}

At no point in that example is it necessary for the computer to know the name of the key, but for convenience it may be useful to write it as:

actions[keys.UP] = function () {...};
actions[keys.DOWN] = function () {...};

but you'll need to define your own keycode-to-keyname pairing object.


I recently discovered that jQuery UI has a list of some commonly used keycodes ($.ui.keyCode):

keyCode: {
    ALT: 18,
    BACKSPACE: 8,
    CAPS_LOCK: 20,
    COMMA: 188,
    COMMAND: 91,
    COMMAND_LEFT: 91, // COMMAND
    COMMAND_RIGHT: 93,
    CONTROL: 17,
    DELETE: 46,
    DOWN: 40,
    END: 35,
    ENTER: 13,
    ESCAPE: 27,
    HOME: 36,
    INSERT: 45,
    LEFT: 37,
    MENU: 93, // COMMAND_RIGHT
    NUMPAD_ADD: 107,
    NUMPAD_DECIMAL: 110,
    NUMPAD_DIVIDE: 111,
    NUMPAD_ENTER: 108,
    NUMPAD_MULTIPLY: 106,
    NUMPAD_SUBTRACT: 109,
    PAGE_DOWN: 34,
    PAGE_UP: 33,
    PERIOD: 190,
    RIGHT: 39,
    SHIFT: 16,
    SPACE: 32,
    TAB: 9,
    UP: 38,
    WINDOWS: 91 // COMMAND
}
like image 26
zzzzBov Avatar answered Nov 03 '22 01:11

zzzzBov