...

View Full Version : Chrome not firing window.addEventListener keypress



Raphael
08-20-2010, 03:40 PM
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!

Eldarrion
08-20-2010, 03:51 PM
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:



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.

Eldarrion
08-20-2010, 03:53 PM
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.

Raphael
08-20-2010, 04:32 PM
Thank you! That fixed it in Chrome & Safari. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum