Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
72
rated 0 times [  77] [ 5]  / answers: 1 / hits: 16040  / 11 Years ago, tue, april 9, 2013, 12:00:00

I have a script that is supposed to open a section of a web page, and save changes on Ctrl + n and Ctrl + s respectively. I got it working in IE, but it doesn't seem to work in Firefox and Chrome. Any ideas?


My override function.


function prevent(e)
{
try{e.stopPropagation();}catch(ex){}
try{e.preventDefault()}catch(ex){}
try{if (e.preventDefault)
e.preventDefault();
else {
e.cancelBubble = true;
e.returnValue = false;
e.keyCode = 0;
}} catch(ex){}
}

More From » html

 Answers
52

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.


[#79010] Monday, April 8, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brandt

Total Points: 43
Total Questions: 90
Total Answers: 111

Location: Aruba
Member since Fri, Jun 24, 2022
2 Years ago
;