capturing Ctrl+S in IE browser

G

Greg

I want to be able to capture the user pressing Ctrl+S. I know that
the IE browser has a key binding for Ctrl+S but is there any way that
I can be notified of this key press anyway. I have found tons of
examples of capturing keys in IE but *none* of them work for capturing
Ctrl+S, Ctrl+F, Ctrl+P,etc or any key combination that has a binding
in IE. Does anyone know how to do this?
 
M

Martin Honnen

Greg said:
I want to be able to capture the user pressing Ctrl+S. I know that
the IE browser has a key binding for Ctrl+S but is there any way that
I can be notified of this key press anyway. I have found tons of
examples of capturing keys in IE but *none* of them work for capturing
Ctrl+S, Ctrl+F, Ctrl+P,etc or any key combination that has a binding
in IE. Does anyone know how to do this?

The following shows that you get event handlers fired for such combinations:

document.onkeydown = document.onkeypress = function (evt) {
if (!evt && window.event) {
evt = window.event;
}
var keyCode = evt.keyCode ? evt.keyCode :
evt.charCode ? evt.charCode : evt.which;
if (keyCode) {
if (evt.ctrlKey) {
var p = document.createElement('p');
p.appendChild(document.createTextNode(evt.type + ': ' +
keyCode + ' (' + String.fromCharCode(keyCode) + ')'));
document.body.appendChild(p);
}
}
return true;
}

It doesn't look however as if you could cancel the default action
associated with Ctrl-P for instance.
 
G

Greg

Thanks, that worked great.

Martin Honnen said:
The following shows that you get event handlers fired for such combinations:

document.onkeydown = document.onkeypress = function (evt) {
if (!evt && window.event) {
evt = window.event;
}
var keyCode = evt.keyCode ? evt.keyCode :
evt.charCode ? evt.charCode : evt.which;
if (keyCode) {
if (evt.ctrlKey) {
var p = document.createElement('p');
p.appendChild(document.createTextNode(evt.type + ': ' +
keyCode + ' (' + String.fromCharCode(keyCode) + ')'));
document.body.appendChild(p);
}
}
return true;
}

It doesn't look however as if you could cancel the default action
associated with Ctrl-P for instance.
 
R

rh

Martin said:
The following shows that you get event handlers fired for such combinations:
It doesn't look however as if you could cancel the default action
associated with Ctrl-P for instance.

With a bit of touch-up to your code, it may:

document.onkeydown = document.onkeypress = function (evt) {
evt = evt || window.event;
var keyCode = evt.keyCode || evt.which || 0;
if (keyCode && evt.ctrlKey) {
try { evt.keyCode = 0;} catch(e) {}
var p = document.createElement('p');
var str = evt.type + ': ' +
keyCode + ' (' + String.fromCharCode(keyCode) + ')';
p.appendChild(document.createTextNode(str));
document.body.appendChild(p);
return false;
}
return true;
}

The requirement to nullify the keyCode, as I recall, is a
long-standing IE peculiarity (the OP could remove the try/catch for
his stated requirement).

Your code, by the way, provides a fine demonstration of the
differences in the browsers key event handling.

../rh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top