By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,888 Members | 1,540 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,888 IT Pros & Developers. It's quick & easy.

capturing Ctrl+S in IE browser

P: n/a
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?
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a


Greg wrote:
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #2

P: n/a
Thanks, that worked great.

Martin Honnen <ma*******@yahoo.de> wrote in message news:<41********@olaf.komtel.net>...
Greg wrote:
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.

Jul 23 '05 #3

P: n/a
rh
Martin Honnen wrote:
Greg wrote:
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:

<..> 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
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.