SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Keystroke

  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Keystroke

    hi,

    I am looking to show an alert box eveytime a key is pressed.

    obviously the alert box should show the key which has been pressed

    any help would be much appreciated.

    Thanks

    Sinky

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this.
    Code:
    function keyHandler(e) {
      var key0;
      if(e && e.which) {
        key0 = e.which;
      } else {
        if(!e && window.event) e=window.event;
        if(e && e.keyCode) {
          key0 = e.keyCode;
        }
      }
      var key = String.fromCharCode(key0);
      alert('You pressed '+key+'!!!')
    }
    
    window.onload=function(){
      document.onkeypress=keyHandler;
    }
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •