capturing Ctrl+S in IE browser

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Greg

    #1

    capturing Ctrl+S in IE browser

    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?
  • Martin Honnen

    #2
    Re: capturing Ctrl+S in IE browser



    Greg wrote:
    [color=blue]
    > 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?[/color]

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

    document.onkeyd own = document.onkeyp ress = 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.create Element('p');
    p.appendChild(d ocument.createT extNode(evt.typ e + ': ' +
    keyCode + ' (' + String.fromChar Code(keyCode) + ')'));
    document.body.a ppendChild(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/

    Comment

    • Greg

      #3
      Re: capturing Ctrl+S in IE browser

      Thanks, that worked great.

      Martin Honnen <mahotrash@yaho o.de> wrote in message news:<4108d433$ 1@olaf.komtel.n et>...[color=blue]
      > Greg wrote:
      >[color=green]
      > > 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?[/color]
      >
      > The following shows that you get event handlers fired for such combinations:
      >
      > document.onkeyd own = document.onkeyp ress = 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.create Element('p');
      > p.appendChild(d ocument.createT extNode(evt.typ e + ': ' +
      > keyCode + ' (' + String.fromChar Code(keyCode) + ')'));
      > document.body.a ppendChild(p);
      > }
      > }
      > return true;
      > }
      >
      > It doesn't look however as if you could cancel the default action
      > associated with Ctrl-P for instance.[/color]

      Comment

      • rh

        #4
        Re: capturing Ctrl+S in IE browser

        Martin Honnen wrote:
        [color=blue]
        > Greg wrote:
        >[color=green]
        > > 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?[/color]
        >
        > The following shows that you get event handlers fired for such combinations:
        >[/color]
        <..>[color=blue]
        > It doesn't look however as if you could cancel the default action
        > associated with Ctrl-P for instance.[/color]

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

        document.onkeyd own = document.onkeyp ress = 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.create Element('p');
        var str = evt.type + ': ' +
        keyCode + ' (' + String.fromChar Code(keyCode) + ')';
        p.appendChild(d ocument.createT extNode(str));
        document.body.a ppendChild(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

        Comment

        Working...