Javascript key recognition

Need technical advice on coding your web pages? Covers HTML, JavaScript, CSS, and some server side technologies. Also the issue of some webpages not displayed well.

Moderator: Don_HH2K

Javascript key recognition

Postby Anonymosity » Tue 26 Oct, 2010 5:41 pm

I have a Javascript file that detects key codes, but it is set to react to Windows keyboard keys. How would I adapt it to work with a Mac keyboard?
Code: Select all
   // Hotkeys
   document.addEventListener('keydown', function(e){
      if(e.shiftKey && !e.ctrlKey && e.altKey){
         switch(e.keyCode){
            // Edit styles with Alt+Shift+E
            case 69: editStyles(); break;
            // Unblock elements with Alt+Shift+U
            case 85: unblockEle(); break;
             // Block element with Alt+Shift+B
            case 66: blockEle(); break;
            // Unblock latest element with Alt+Shift+L
            case 76: unblockEle(true); break;
             // Block elements (don't use nth-child) with Alt+Shift+W
            case 87: blockEle(true); break;
         }
      }
   }, false);

The Alt key is the problem. I guess the Alt/Option key on a Mac keyboard is not the same as the Alt key for Windows. What name would I give to have that key recognized?
UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; rv:1.9.2.11) Gecko Firefox/3.6.11
Anonymosity
silver member
silver member
 
Posts: 359
Joined: Sat 21 Jun, 2008 2:56 am

Return to Web Design and Page Coding

Who is online

Registered users: Google [Bot]