Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    74
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Chrome not firing window.addEventListener keypress

    On my site we have image galleries that pop up over top of the page in a higher z-index and position:fixed, and another div between the gallery and background with opacity set to about 85%. The image gallery div has a close button, and I was asked to make the gallery also close by pressing ESC, so I added this:

    igevt=function(evt){checkclosegal(evt)}
    window.addEventListener('keypress',igevt, false)

    and checkclosegal:

    function checkclosegal(evt)
    {

    if(evt.keyCode==27)
    {
    closebgeagal()
    window.removeEventListener('keypress',igevt, false)
    }

    }


    This works perfectly in Firefox and Opera, but Chrome and Safari don't fire the event (not worried about ie right now - I know it uses attachEvent). Could it have something to do with the gallery being in a higher z-index? Please help, thanks!

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    It's not so much an issue of the event not being captured as it is an issue of the Escape button working in chrome for keypress events. A simple debug like this will show the problem:

    Code:
    	igevt=function(evt){
    		alert(evt.keyCode)
    	}
    	window.addEventListener('keypress',igevt, false)
    Not sure if that is a bug or intended behavior as far as Chrome goes, but with the code above you can easily see how nothing happens when you press the Escape button, while most other buttons seem to work normally.
    The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

    How to ask smart questions?

  • #3
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    After a quick check, it turns out that if you use 'keydown' (Might work with keyup too, but haven't tested), Chrome will capture the event normally with the Escape key. Just change to keydown and it should work from that point on.
    The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

    How to ask smart questions?

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    74
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Thank you! That fixed it in Chrome & Safari.


  •  

    Tags for this Thread

    Posting Permissions

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