1

I use the following code (as shortcut) to redirect users on my feedback page.

<a href='/feedback/' accesskey='f'>feedback</a>

But this code does not work in Google Chrome because when user presses Alt + F it will open the Google Chrome menu bar.

How do I disable those 'shortcuts'?

It can be jQuery, javascript...

Note: I have written a javascript code that redirects, but it firstly opens the Chrome menu bar then does its job.

Idrizi.A
  • 9,819
  • 11
  • 47
  • 88

5 Answers5

2

There are certain special keys that are reserved and Alt+F is one of them but it will really vary between browsers and operating systems. Here's a good article.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
2

Browsers that allow this theoretically expose a security vulnerability. If it's possible to override "system" behaviors, you could hijack the users browser. It would be able to pop up a fake "File" menu that simulates the real one and makes the user think they are interacting with their own local machine instead of a web site.

Because of this it's impossible in most modern browsers.

recursive
  • 83,943
  • 34
  • 151
  • 241
2

In Google Chrome (tested in V44), the modifier key to access accesskey keyboard shortcuts is Alt.

However, if the key conflicts with a browser shortcut, it is accessed with AltShift. So the solution you posted works, but only using AltShift.

Example:

  • the first link is accessed with AltShift+f (conflict with menu shortcut)
  • the second link is accessed with Alt+a (no conflict)

<h1>Links with accesskey attribute</h1>

<a href='http://www.example.org/' accesskey='f'>
Link to www.example.org (accesskey: f)
</a>
<p/>
<a href='http://apache.org/' accesskey='a'>
Link to apache.org (accesskey: a)
</a>

Incidentally, I prefer the solution Firefox uses, which is to always use AltShift for these shortcuts. This is more consistent for users than Chrome's behavior (though the Chome devs may have had their reasons).


Note: The above is valid on Windows and Linux. AFAIK, Chrome on Mac OS X uses different shortcuts.

sleske
  • 81,358
  • 34
  • 189
  • 227
  • Oh wow. I never realized that Alt+Shift+(key) was _not_ the primary method for accesskeys. Because on [Wikipedia](https://en.wikipedia.org/wiki/Michael_Holm) they label all their accesskeys with [alt-shift-(key)], regardless of a potential conflict. – WoodrowShigeru Jun 28 '16 at 18:45
  • Alt+Shift+F does not ever work in Chrome for Linux, Ubuntu 18.04.1 LTS. And there is no solution for this. – WoodrowShigeru Jan 10 '19 at 14:56
  • @WoodrowShigeru: Strange. I just tested with Chrome 65 on Debian, and Alt+Shift+F does work for me in the testcase above. – sleske Jan 10 '19 at 15:36
  • I have Chrome 71 and this testcase doesn't work. Maybe it's a bug the Google devs have added. – WoodrowShigeru Jan 10 '19 at 17:36
  • @WoodrowShigeru: Just re-tested with Chrome 71 - still works for me. Note, though, that there seems to be some problem with keyboard focus handling. I had to click into the example page, e.g. on the heading, for the key shortcuts to work. Also, if you trigger the menu with Alt-F, you need to click again to correct the keyboard focus. Maybe that's what caused your problem? If not, feel free to ask a new question :-). – sleske Jan 11 '19 at 14:02
  • Sadly, it doesn't work for me. The Alt+T accesskey does work, but "F" is troublesome. I've even filed an issue on the Chromium Issue Tracker … but it really seems to be a problem exclusive to my machine. Sorry to bother you. – WoodrowShigeru Jan 11 '19 at 23:24
1

As others have stated, it's probably not a good idea.

But if you're a madlad and you like doing what you want, this will actually work:

document.addEventListener('keydown', (e) => {
  e.preventDefault();
  if (e.altKey) {
    console.log('yay');
  }
})

e.preventDefault() needs to be placed at the top of the event handler. This prevents the browser's default behavior (ie., opening menus).

wle8300.com
  • 2,588
  • 1
  • 23
  • 29
0

I just found a way how to disable browser functions when user presses Alt + other button. Javascript can disable this shortcuts by writing at the end of the function return false

Example

function function1(){/* code goes here */; return false}
Idrizi.A
  • 9,819
  • 11
  • 47
  • 88