Simple keycode object collection for Javascript

By on

I've only recently gotten into Javascript, thanks to the <canvas> element. I'm currently building a 2D tile-based render engine for a game. (What better way to learn something new than to build a game, right?)

Ofcourse, this game also has to respond to the keyboard. I got tired of looking up keycodes real quick and decided to just create an associative array out of them, for the hell of it. My IDE even autocompletes them. Here's the list:

// A var storing all useful keys for easy access
var key = {
    'Backspace': 8,
    'Tab': 9,
    'Enter': 13,
    'Shift': 16,
    'Ctrl': 17,
    'Alt': 18,
    'Pause': 19,
    'Capslock': 20,
    'Esc': 27,
    'Pageup': 33,
    'Pagedown': 34,
    'End': 35,
    'Home': 36,
    'Leftarrow': 37,
    'Uparrow': 38,
    'Rightarrow': 39,
    'Downarrow': 40,
    'Insert': 45,
    'Delete': 46,
    '0': 48,
    '1': 49,
    '2': 50,
    '3': 51,
    '4': 52,
    '5': 53,
    '6': 54,
    '7': 55,
    '8': 56,
    '9': 57,
    'a': 65,
    'b': 66,
    'c': 67,
    'd': 68,
    'e': 69,
    'f': 70,
    'g': 71,
    'h': 72,
    'i': 73,
    'j': 74,
    'k': 75,
    'l': 76,
    'm': 77,
    'n': 78,
    'o': 79,
    'p': 80,
    'q': 81,
    'r': 82,
    's': 83,
    't': 84,
    'u': 85,
    'v': 86,
    'w': 87,
    'x': 88,
    'y': 89,
    'z': 90,
    '0numpad': 96,
    '1numpad': 97,
    '2numpad': 98,
    '3numpad': 99,
    '4numpad': 100,
    '5numpad': 101,
    '6numpad': 102,
    '7numpad': 103,
    '8numpad': 104,
    '9numpad': 105,
    'Multiply': 106,
    'Plus': 107,
    'Minut': 109,
    'Dot': 110,
    'Slash1': 111,
    'F1': 112,
    'F2': 113,
    'F3': 114,
    'F4': 115,
    'F5': 116,
    'F6': 117,
    'F7': 118,
    'F8': 119,
    'F9': 120,
    'F10': 121,
    'F11': 122,
    'F12': 123,
    'equal': 187,
    'Coma': 188,
    'Slash': 191,
    'Backslash': 220

Now you can use things like "key.Uparrow" in your code in stead of it's corresponding number. For example:

// Bind the onKeyDown function to the onkeydown event.
window.onkeydown = onKeyDown;

 *This function is called whenever a key is pressed on the keyboard
 *@param    key     {int}   The key
function onKeyDown(keypress) {

    // Select the correct key and execute its functions
    switch (keypress.keyCode) {

        case key.Uparrow:
            // Do  something

        case key.Rightarrow:
            // Do  something

        case key.Downarrow:
            // Do  something

        case key.Leftarrow:
            // Do something



Woohoo! Just what I was looking for! This will be so much nicer!




That's it! After her death, he left town for good.

Flirt App Chatten mit Singles iDates

Zufaellig bin ich auf eurem Portal gelandet und muss sagen, dass mir diese vom Design und den Informationen richtig gut gefaelt.

Very rapidly this website will be famous amid all blogging and site-building visitors, due to it's fastidious articles or reviews

plenty of fish dating site

I don't know if it's just me or if perhaps everybody else encountering issues with your website. It seems like some of the text in your content are running off the screen. Can somebody else please provide feedback and let me know if this is happening to them too? This may be a issue with my browser because I've had this happen previously. Thank you

ps4 games

What a data of un-ambiguity and preserveness of valuable familiarity regarding unpredicted feelings.

quest bars cheap

I pay a visit day-to-day some web pages and blogs to read articles or reviews, except this blog offers feature based content.

quest bars cheap coupon twitter

Hi to all, for the reason that I am really eager of reading this weblog's post to be updated regularly. It consists of good information.

quest bars cheap

This website truly has all the info I wanted concerning this subject and didn't know who to ask.

What's up, this weekend is good designed for me, since this occasion i am reading this fantastic educational paragraph here at my residence.

plenty of fish vs free trial

What's up mates, fastidious post and pleasant arguments commented here, I am genuinely enjoying by these.

I'm gone to say to my little brother, that he should also visit this website on regular basis to get updated from hottest reports.

sling tv

Please let me know if you're looking for a article author for your site. You have some really great posts and I believe I would be a good asset. If you ever want to take some of the load off, I'd really like to write some articles for your blog in exchange for a link back to mine. Please blast me an email if interested. Many thanks!

sling tv

An impressive share! I have just forwarded this onto a coworker who has been conducting a little research on this. And he in fact ordered me lunch because I discovered it for him... lol. So allow me to reword this.... Thank YOU for the meal!! But yeah, thanx for spending the time to talk about this topic here on your web site.

sling tv

hello!,I really like your writing so a lot! proportion we keep in touch more approximately your article on AOL? I need a specialist in this space to solve my problem. Maybe that is you! Looking ahead to peer you.

sling tv

I must thank you for the efforts you've put in penning this website.

I'm hoping to check out the same high-grade blog posts by you in the future as well. In truth, your creative writing abilities has motivated me to get my own site now ; )

Jelle De Loecker