Overriding Browser's Keyboard Shortcuts
There's an excellent coverage of this here: http://unixpapa.com/js/key.html
As for whether this is something that should be done, stackoverflow's question editor override's quite a few keys without disrupting too much (hover over the toolbar buttons).
Overriding shortcut keys in Firefox and Chrome
I have seen the same issue. Some browsers will not allow you to capture certain shortcuts. Look at this https://stackoverflow.com/a/7296303/1366887
Some key combinations are resticted in Chrome 4, but not in Chrome 3. Look here: https://groups.google.com/a/chromium.org/forum/?fromgroups=#!topic/chromium-bugs/Ntc1byZXHfU
Here is the Javascript:
$(window).keydown(function(event) {
if(event.ctrlKey && event.keyCode == 84) {
console.log("Hey! Ctrl+T event captured!");
event.preventDefault();
}
if(event.ctrlKey && event.keyCode == 83) {
console.log("Hey! Ctrl+S event captured!");
event.preventDefault();
}
});
I have used this numerous times, and it has worked greatly.
Here is another rescource you can take a look at: http://unixpapa.com/js/key.html
Without Jquery:
onkeydown = function(e){
if(e.ctrlKey && e.keyCode == 'S'.charCodeAt(0)){
e.preventDefault();
//your saving code
}
}
Here is a JSFIDDLE of it working.
Override the browser shortcuts by my own custom shortcuts
This will let you do this:
e.ctrlKey && String.fromCharCode(kc).toUpperCase()
checks for Ctrl + A to be pressed.
$(document).on('keydown', function(e) { var kc = e.which || e.keyCode;
if (e.ctrlKey && String.fromCharCode(kc).toUpperCase() == "A") { e.preventDefault(); console.log(String.fromCharCode(kc).toUpperCase()); window.location.href = 'your url here'; }});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>adfasfasdfsda
Is overriding common browser shortcuts like ctrl+s etc. acceptable practice for accessibility?
I don't think overriding default shortcuts is a good idea. I'll give you an example with Discord. I have been using discord for a while and there's a shortcut to mark all servers as read which is shift + esc
but the shift + esc on Chrome, opens Chrome's in-built task manager. So now whenever someone will try to use your shortcut not only will the browser execute the function you have attached with the shortcut but also execute the shortcut the browser has in-built attached to it.
How can I override the browser keyboard shortcuts in my GWT/GXT app?
Its better to find key combinations rather than trying to override the keyboard shortcuts which is not a standard approach.
How does Google Sheets override browser shortcuts?
The general idea is to intercept the event and call event.preventDefault()
on it if it corresponds to a shortcut that you want to override. For example, the following code will prevent you from navigating to your leftmost tab if you press control-1
:
document.body.addEventListener('keydown', (event) => { // keyCode 49 corresponds to the "1" key if(event.keyCode==49 && event.ctrlKey) { event.preventDefault(); console.log('default action prevented, doing custom action instead'); }});
body { background-color: yellow;}
click me first
Related Topics
Can a Site Invoke a Browser Extension
How Persistent Is Localstorage
Jquery Clone() Not Cloning Event Bindings, Even with On()
Dynamically Loading a Typescript Class (Reflection for Typescript)
Javascript:For Loop with Timeout
Browser-Independent Way to Detect When Image Has Been Loaded
The .Replace() Method Does Change the String in Place
Prevent Long Running JavaScript from Locking Up Browser
How to Run Node Js Code from Npm Inside of Swift
How to Detect Emoji Using JavaScript
How Would You Overload the [] Operator in JavaScript
Firebase.Database Is Not a Function
How to Use Ranges in a Switch Case Statement Using JavaScript
Difference Between Date(Datestring) and New Date(Datestring)
Jquery or JavaScript to Find Memory Usage of Page