Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-20-2010, 02:40 PM   PM User | #1
Raphael
New Coder

 
Join Date: Mar 2009
Posts: 70
Thanks: 3
Thanked 3 Times in 3 Posts
Raphael is an unknown quantity at this point
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!
Raphael is offline   Reply With Quote
Old 08-20-2010, 02:51 PM   PM User | #2
Eldarrion
Regular Coder

 
Join Date: Feb 2009
Location: Wheeling, IL
Posts: 358
Thanks: 5
Thanked 62 Times in 60 Posts
Eldarrion is on a distinguished road
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?
Eldarrion is offline   Reply With Quote
Old 08-20-2010, 02:53 PM   PM User | #3
Eldarrion
Regular Coder

 
Join Date: Feb 2009
Location: Wheeling, IL
Posts: 358
Thanks: 5
Thanked 62 Times in 60 Posts
Eldarrion is on a distinguished road
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?
Eldarrion is offline   Reply With Quote
Old 08-20-2010, 03:32 PM   PM User | #4
Raphael
New Coder

 
Join Date: Mar 2009
Posts: 70
Thanks: 3
Thanked 3 Times in 3 Posts
Raphael is an unknown quantity at this point
Thank you! That fixed it in Chrome & Safari.
Raphael is offline   Reply With Quote
Reply

Bookmarks

Tags
addeventlistener, chrome, keypress

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:31 AM.


Advertisement
Log in to turn off these ads.