开发者

Site-wide navigation in jQuery, but avoiding Command- on the Mac

I have a function in jQuery that does something like the following to allow for sitewide keyboard navigation.

function listenForNavKeys(e) {

  // Alt+1 Navigates home
  if(e.which == 49) {
    window.location.replace(location.protocol + '//' + location.host + '/application/views/home.php');
  }
}

For an example of this in action, go to Facebook and press Alt+2 to navigate to your profile. On the Mac, this must be done with Ctrl+Alt+2. However, when I'm listening for the开发者_运维技巧 key 50, for example, this keyCode responds to Cmd+2 as well. This is not the desired behavior, since that's how many users switch between tabs in browsers. The browser functionality still works, but the page will navigate elsewhere while you navigate to another tab.

So how does a site like Facebook handle this event appropriately? What keyCode should I really be listening for?


Whoops. Figured this out far too quickly on my own. My apologies.

First, my code above is wrong. I meant // Alt+1 in the code. Second, I should be looking for if(e.altKey && e.which == 49). That will get you the correct Alt+# combo.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜