accesskey VS navigator keyboard shortcut
Asked Answered
Y

2

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

Yukoyukon answered 2/2, 2012 at 15:37 Comment(3)
NOTE You won't meet the problem with firefox since accesskeys are activated with Shift+Alt+[key] and menu with Alt+[key].Yukoyukon
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 doableHildredhildreth
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.Gawen
R
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.

Rammish answered 9/2, 2012 at 14:36 Comment(1)
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."Yukoyukon
A
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.

Alarmist answered 5/8, 2015 at 9:22 Comment(2)
And why is in "big three" IE, not Opera? :o)Suffragette
@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.Alarmist

© 2022 - 2024 — McMap. All rights reserved.