SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    event handling / onkeypress

    Hello,


    Code JavaScript:
    document.onkeypress=function(event){
     
    if(event.ctrlKey){
      if(event.altKey){
        alert('Hello Wolrd');
      }
    }
     
    }

    Firefox displays alert box once (which is what I expected). When testing in Opera, alert box is displayed twice. When testing in Chrome, alert box is not even displayed once.

    I have read this article but I must admit I can't really make sense of it.

    So, I'm here to seek help and good advice about how to handle events properly.


  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Response to the different keyboard events varies between browsers. After some experimentation I determined that to monitor for Ctrl + Alt + another key, the following seems to work:

    Code:
    <script type="text/javascript">
    
    document.onkeyup = function( e )
    {      
      var evt = e || window.event;
      
      if( evt.ctrlKey && evt.altKey )
      {
        alert( 'Ctrl + Alt pressed with scancode ' + evt.keyCode );
      }
      
    }
     
    </script>
    Probably best to avoid using these combinations as they could conflict with current or future native functionality.
    Tab-indentation is a crime against humanity.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by rhgiant View Post
    Firefox displays alert box once (which is what I expected). When testing in Opera, alert box is displayed twice. When testing in Chrome, alert box is not even displayed once.
    The event compatibility table shows for the keypress event that firefox and opera incorrectly fire that event when they shouldn't.

    It would be the onkeydown event that you want to use, if you want both ctrl and alt to trigger your alert.
    Also, in Internet Explorer it's not the function event where it gets the information from, but from window.event instead, so it can be useful to make it clear that that's what is happening.

    Code javascript:
    document.onkeydown = function (evt) {
        evt = evt || window.event;
        if (evt.ctrlKey && evt.altKey) {
            alert('Hello World');
        }
    };
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks



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
  •