1.

Solve : changing from page with keys?

Answer»

Hi there. Does SOMEBODY knows, how I go to another SITE, by pressing an arrow of the keyboard. I would like to have that if I press the arrow that points to the left -on your keyboard!- (I can't say it correct in English, so forgive me) that it goes to another page (example: page.html). Actually it is a sort of unvisible link, but you can use it, if you press that key. Does anybody knows how to do this?

greetz


blackberry  Usually Alt+Left goes back through your BROWSER history and Alt+Right goes forward.  Left scrolls left in a wide page, and right scrolls right.  I do not think you can easily override these settings in your browser, unless you use something like AutoHotKey which is free and enables you to reprogram your entire keyboard in a very sophisticated way. Quote

Usually Alt+Left goes back through your browser history and Alt+Right goes forward.  Left scrolls left in a wide page, and right scrolls right.  I do not think you can easily override these settings in your browser, unless you use something like AutoHotKey which is free and enables you to reprogram your entire keyboard in a very sophisticated way.

I actually didn't meant that, because for that you should visited all the other pages first, (even in right orther). I woulded like to have a way, that you can use these keys, if it is the first time you visit the siteIt would not generally be desireable for a web page to have the ability to reprogram the way a user's keyboard works.  There is one possibility though: you could use javascript to poll for keypress events, and respond accordingly.  That's beyond what I normally do with javascript though, so unless someone else here can help you, you may be better off heading over to a dedicated javascript programmers' forum.This one covers most angles, and avoids explicit messings about with character codes.

- Always use quotes, even with numbers, and always use upper-case letters.

Code: [Select]document.onkeypress = FUNCTION(e)
{
    switch( String.fromCharCode( (e||event).keyCode  ).toUpperCase() )
    {
        case "A":
            alert("A") // or whatever..
            break;

        case "5":
            alert("5")
            break;
    }
}First off all, a thanx for this script. Second, do you know how I let the browser go to another page (and not showing a message like a) if i press the RIGHT ARROW?


thanx


Blackberry  Instead of alert("a"); try window.location="http://www.whatever.com/";

Don't know how to trap right and left cursors keys though.  Jude?For some reason, the arrow keys don't fire the onkeypress event. They do fire the onkeydown event though - so that's the first change.

I have USED the easy approach above (turning the keycode into a character) because it's easier to see what's going on, and to add new cases. I thought this would have to change with the arrows, but it seems that the keycodes they produce do actually correspond to characters. I have put these characters into a map so it's easier to deal with. Then everything else is the same as before.

Code: [Select]document.onkeydown= function(e)
{
    var arrowChars = {up:"&",right:"'",down:"(",left:"%"};

    switch( String.fromCharCode( (e||event).keyCode  ).toUpperCase() )
    {
      case arrowChars.right:
        alert("right");
        // location.href = "blah.htm";
        break;

      case arrowChars.left:
        alert("left");
        // location.href = "doodah.htm";
        break;
    }
}


Discussion

No Comment Found