Cross browser key handler?

Discussion in 'Javascript' started by Ciaran, May 21, 2007.

  1. Ciaran

    Ciaran Guest

    Hi has anyone got a function that can detect a user pressing a key
    that works cross browser?
    I just want to redirect my visitor if they hit escape ... you'd think
    it would be easy!
    Thanks,
    Ciarán
    Ciaran, May 21, 2007
    #1
    1. Advertising

  2. Ciaran

    Ciaran Guest

    On May 21, 6:11 pm, Randy Webb <> wrote:
    > Ciaran said the following on 5/21/2007 12:43 PM:
    >
    > > Hi has anyone got a function that can detect a user pressing a key
    > > that works cross browser?
    > > I just want to redirect my visitor if they hit escape ... you'd think
    > > it would be easy!

    >
    > As easy as posting the same question 6 times?
    >
    > --
    > Randy
    > Chance Favors The Prepared Mind
    > comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
    > Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/



    Very funny. That was a server error.
    Ciaran, May 21, 2007
    #2
    1. Advertising

  3. Ciaran

    Ciaran Guest

    On May 21, 5:43 pm, Ciaran <> wrote:
    > Hi has anyone got a function that can detect a user pressing a key
    > that works cross browser?
    > I just want to redirect my visitor if they hit escape ... you'd think
    > it would be easy!
    > Thanks,
    > Ciarán


    Anyone?
    Ciaran, May 23, 2007
    #3
  4. On May 21, 10:43 am, Ciaran <> wrote:
    > Hi has anyone got a function that can detect a user pressing a key
    > that works cross browser?
    > I just want to redirect my visitor if they hit escape


    try:

    document.body.onkeypress=
    function(e){
    if((e||event).keyCode==27)
    location.href="http://google.com";
    }

    untested
    scripts.contact, May 23, 2007
    #4
  5. Ciaran

    Ciaran Guest

    On May 23, 11:35 pm, "scripts.contact" <>
    wrote:
    > On May 21, 10:43 am, Ciaran <> wrote:
    >
    > > Hi has anyone got a function that can detect a user pressing a key
    > > that works cross browser?
    > > I just want to redirect my visitor if they hit escape

    >
    > try:
    >
    > document.body.onkeypress=
    > function(e){
    > if((e||event).keyCode==27)
    > location.href="http://google.com";
    >
    > }
    >
    > untested



    Thanks for the reply! .... but it doesnt seem to be working. I have
    this function working in IE but FF is ignoring it ...

    if (document.layers) { document.captureEvents(Event.KEYPRESS); }
    document.onkeypress = getKey;
    function getKey(keyStroke) {
    var keyCode = (document.layers) ? keyStroke.which : event.keyCode;
    if(keyCode==27) location.href='quit.html';
    }
    Ciaran, May 24, 2007
    #5
  6. On May 23, 5:33 pm, Ciaran <> wrote:
    > > document.body.onkeypress=
    > > function(e){
    > > if((e||event).keyCode==27)
    > > location.href="http://google.com";
    > > }

    >
    > Thanks for the reply! .... but it doesnt seem to be working. I have
    > this function working in IE but FF is ignoring it ...
    >


    Try this:

    document.onkeypress=changeHREF
    function changeHREF(ev){
    ev=ev||event;
    if(ev.keyCode==27){
    location.href="http://google.com"
    return false
    }
    }

    works in o9,f2,i6
    scripts.contact, May 24, 2007
    #6
  7. Ciaran

    Ciaran Guest

    On May 24, 12:52 am, "scripts.contact" <>
    wrote:
    > On May 23, 5:33 pm, Ciaran <> wrote:
    >
    > > > document.body.onkeypress=
    > > > function(e){
    > > > if((e||event).keyCode==27)
    > > > location.href="http://google.com";
    > > > }

    >
    > > Thanks for the reply! .... but it doesnt seem to be working. I have
    > > this function working in IE but FF is ignoring it ...

    >
    > Try this:
    >
    > document.onkeypress=changeHREF
    > function changeHREF(ev){
    > ev=ev||event;
    > if(ev.keyCode==27){
    > location.href="http://google.com"
    > return false
    > }
    > }
    >
    > works in o9,f2,i6


    Wow! perfect! and such an elegant solution! I searched the net for a
    key handler like this and everything I found was either 2 pages long
    or only worked in ie. Thanks a lot!
    Ciarán
    Ciaran, May 24, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M P
    Replies:
    1
    Views:
    453
  2. cjl
    Replies:
    1
    Views:
    128
  3. Matt Kruse
    Replies:
    0
    Views:
    121
    Matt Kruse
    Aug 15, 2006
  4. Rancid Buttchutney

    Cross-browser unified key codes?

    Rancid Buttchutney, Nov 8, 2006, in forum: Javascript
    Replies:
    1
    Views:
    82
    Matt Kruse
    Nov 8, 2006
  5. Ciaran

    simple cross browser key handler

    Ciaran, May 21, 2007, in forum: Javascript
    Replies:
    0
    Views:
    87
    Ciaran
    May 21, 2007
Loading...

Share This Page