accesskey VS navigator keyboard shortcut

733 views Asked by At

It seems to be a recurrent tricky question but I still don't have a fix nor workaround for it:

Is there a way to intercept accesskeys before keyboard shortcuts?

I need to use accesskeys on my favorites navigator IE and also FF, but I don't wan't to consider existing shortkeys as reserved. Otherwise it doesn't leave a lot of possibilities (currently I only use numbers).

Note: by accesskey, I mean the accesskey html attribute.

Any idea ? Flash apps can do it, so what about JavaScript ?


For example here is an accessible control:

<button onclick="alert('Hello');" accesskey="F">Alt+F to activate</button>

When I try to activate it I just open File menu :'(

2

There are 2 answers

2
sleske On

This will, obviously, depend on the browser. For the "big three":

  • Firefox: menu shortcuts use Alt, "accesskey" shortcuts use ShiftAlt, so there is no conflict.
  • Google Chrome: both types of shortcuts use Alt, and menu shortcuts take precedence, but, if there is a conflict, "accesskey" shortcuts use ShiftAlt, so they are still accessible. See my answer to Disable Alt functions (shortcuts) in Google Chrome for an example.
  • Internet Explorer: both types of shortcuts use Alt, and "accesskey" shortcuts take precedence - just what you want :-).

In short: No, there is no way to override the browser behavior in Javascript (and arguably that would be a security vulnerability), but all browser have ways to handle these conflicts.

1
Tango Bravo On

John Resig (the creator of jQuery) has been working on a "hotkey" addon for jQuery which should do what you are looking for.

https://github.com/jeresig/jquery.hotkeys

I know this isn't a 'jQuery question', and adding a framework for something 'small' isn't ideal, but it should offer a solution. On the other hand, you can always look over how he did it for inspiration.