Prevent action event by default not working ...?

I am trying to add shortcuts to my website to make keyboard navigation easier. However, I had a small problem with my attempt to use the Alt + X keyboard shortcut. The event runs just fine and returns false as it should, but the File browser menu appears independently. I also tried the preventDefault method, but no changes.

Cut version of the script:

 document.documentElement.onkeydown = function(e) { e = e || window.event; switch( e.keyCode || e.which) { // some cases here - most notably: case 116: // F5 key if( activeFrame) { activeFrame.contentWindow.location.reload(); // reloads an iframe if one is active return false; } break; // more cases... case 88: // X key if( e.altKey) { // do something return false; } } } 

As noted above, overriding the default action for the F5 key works just fine - the browser reloads the page only if there is no iframe. I do not quite understand how to prevent the menu from appearing when you press Alt + X.

+7
source share
3 answers

I actually had a web application that worked perfectly with CTRL keys, but then decided that I was smart and used the accesskey attribute, and ran into this exact problem with IE.

The problem with switching to CTRL shortcuts is that many of them are more standard / useful in many applications (for example: cut, copy, paste, select all).

Ctrl + Alt is quite safe, but requires more work with the user part.

I try to just try to stick with ALT shortcuts. IE does not stubbornly insist on appeal.

Successfully canceled CTRL + A / CTRL + F demo: http://jsfiddle.net/egJyT/

This answer seems to imply that it is not possible to disable menu shortcuts without putting IE in kiosk mode.

+3
source

use stopPropagation(e); instead of the preventDefault method

 function stopPropagation(e) { e = e || event;/* get IE event ( not passed ) */ e.stopPropagation? e.stopPropagation() : e.cancelBubble = true; } 

Link Link

Another SO question that mentions that preventDefault has problems in IE.

UPDATE

Try using the code below MSDN Link

 event.returnValue=false; 

And at some point from the keystroke detection

Some common caveats:

  • Macs are generally less reliable than Windows, and some keys cannot be detected.
  • Explorer does not trigger a keystroke event to delete, end, enter, exit, function keys, home, insert, UP / Down page, and tabs.
  • Onkeypress, Safari gives strange KeyCode values ​​in the range of 63200 for delete, termination, function keys, home and pageUp.Down. The onkeydown and -up values ​​are normal.
  • Alt, Cmd, Ctrl, and Shift cannot be detected on Macs other than Opera. However, you can always use the altKey, ctrlKey, and shiftKey properties.
+4
source

Remember that if you manage to prevent the browser from blocking the key combination, you can make the page unusable for some users. Many screen readers have reserved almost any key you can imagine to control the screen reader, and if your page was accessible with a screen reader before adding the key code shortcut, it could be completely inaccessible to users, who need screen readers after adding it.

Read this article on access keys (a bit dated but probably still relevant) and this article on reserved key combinations before investing too much time in this problem.

+1
source

All Articles