8

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 :'(

sinsedrix
  • 4,336
  • 4
  • 29
  • 53
  • 2
    **NOTE** You won't meet the problem with firefox since accesskeys are activated with *Shift+Alt+[key]* and menu with *Alt+[key]*. – sinsedrix Feb 15 '12 at 10:40
  • If you use keydown handlers, you can block the default behavior. With access keys, my experience (from around 2 years ago) is that you need to make sure they don't conflict with menu options, which is not very doable – Ruan Mendes Feb 15 '12 at 20:03
  • 2
    I hope the answer is no. I don't want any old web-page to be able to hi-jack my keyboard commands. When I strike *Alt+F* I want the File menu. – Umbrella Feb 16 '12 at 05:21

2 Answers2

1

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.

Tango Bravo
  • 3,221
  • 3
  • 22
  • 44
  • It could be a jQuery solution. I read about this plugin but it doesn't fit, as the addendum says default behaviour will also be fired: "Others, (IE) either let you handle built-in short-cuts, but will add their functionality after your code has executed." – sinsedrix Feb 09 '12 at 14:55
0

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.

Community
  • 1
  • 1
sleske
  • 81,358
  • 34
  • 189
  • 227
  • And why is in "big three" IE, not Opera? :o) – Legionar Aug 05 '15 at 09:46
  • @Legionar: To be honest, because I did not have an installation of Opera handy (just like with Konqueror, Safari, Android Browser...) :-). Feel free to edit the answer if you have information on other browsers. – sleske Aug 05 '15 at 12:14