473,441 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

Safari, key[down|up] with cursor keys

I'm writing some stuff where I wish to allow the cursor keys to control
elements in a page. This has not been a problem except with Safari
which appears to duplicate the keydown and keyup events which are fired
when the cursor keys are pressed. I.e. pressing and releasing say, K,
results in one keydown event followed by one keyup event. Press any of
the cursor keys results in two keydown events followed by two keyup
events.
A page demonstrating the problem is at
http://megaflexdestiny.net/bits/safarieventprob.html

Has anyone else come accross this bizarre behaviour and have an
explanation or solution for it?

thanks,

mike

Jul 28 '05 #1
3 4480
<in********************@hotmail.com> wrote in message news:11**********************@g14g2000cwa.googlegr oups.com...
I'm writing some stuff where I wish to allow the cursor keys to control
elements in a page. This has not been a problem except with Safari
which appears to duplicate the keydown and keyup events which are fired
when the cursor keys are pressed. I.e. pressing and releasing say, K,
results in one keydown event followed by one keyup event. Press any of
the cursor keys results in two keydown events followed by two keyup
events.
A page demonstrating the problem is at
http://megaflexdestiny.net/bits/safarieventprob.html

Has anyone else come accross this bizarre behaviour and have an
explanation or solution for it?

thanks,

mike

The keyboard generates two codes per press for these keys, the first is always 0. For convenience some browsers may
suppress the first code.
I can't test this mod, but unless someone knows a better way, it may be worth a try

function catchkey(e) {

while(e.keyCode==0)
;
debug(e.type+":"+e.keyCode);
}
--
Stephen Chalmers
547265617375726520627572696564206174204F2E532E2072 65663A205451323437393134

Jul 28 '05 #2
<in********************@hotmail.com> wrote:
I'm writing some stuff where I wish to allow the cursor keys to control
elements in a page. This has not been a problem except with Safari
which appears to duplicate the keydown and keyup events which are fired
when the cursor keys are pressed. I.e. pressing and releasing say, K,
results in one keydown event followed by one keyup event. Press any of
the cursor keys results in two keydown events followed by two keyup
events.


Mike,

This is apparently a Safari bug which you can easily get around.

function go() {
document.addEventListener("keydown",catchkey,false );
document.addEventListener("keyup",catchkey,false);
}

W3C DOM supports two propagation schemes: bubbling and capture. Bubbling
means that an event propagates from the element on which it occured down
the document tree to document root (this is called the bubbling phase -
keyword: BUBBLING_PHASE), then from the document root back to the
element (this is called the capture phase - keyword: CAPTURE_PHASE).

When you specify "false" as third argument for the addEventListener
method, it means that event is propagating by bubbling, thus the event
is doing a round robin. As a consequence, you can catch the event a
first time when it is in the BUBBLING_PHASE, and a second time when it
is in the CAPTURE_PHASE.

In your case, you need to catch the event only once, in BUBBLING_PHASE,
and prevent its further propagation by using method stopPropagation like
this:

function catchkey(e) {
debug(e.type+":"+e.keyCode);

e.stopPropagation();
// Note: for IE, you would use this declaration:
// window.cancelBubble = true;
}

hih,

JJS.

--
Anti-spam : <http://public.xdi.org/=jj.solari>
Jul 29 '05 #3

J.J.SOLARI wrote:
[snipped]
In your case, you need to catch the event only once, in BUBBLING_PHASE,
and prevent its further propagation by using method stopPropagation like
this:

function catchkey(e) {
debug(e.type+":"+e.keyCode);

e.stopPropagation();
// Note: for IE, you would use this declaration:
// window.cancelBubble = true;
}


Well I can't say that I think I understood everything you said but the
stopPropagation method has indeed fixed the problem so thanks for that.
Now I just have to iron out the other Safari only kinks :)

mike.

Jul 31 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Darren Oakey | last post by:
ok - the problem - I made a simple breakout game out of a form, just painting the background - and using keydown for left and right arrow keys to control the bat - worked fine. I then moved all...
1
by: Mervin Williams | last post by:
I have an application that uses a windows forms datagrid. How do I select rows using the up-arrow and down-arrow keys? Mervin Williams
3
by: orekinbck | last post by:
Hi How do I catch a SHIFT TAB in the key down event of a text box? Thanks Bill
2
by: ooooscar | last post by:
Is there any way to get a key press or key down from all the components of the form. A mean if a hava a 2 text bo and a press a key into the first i get a textbox1_keydown event, and if i press a...
1
by: abprules | last post by:
Here's what I'm trying to do: I want to type in a combo box (search for a Contact) and if that contact is not in the list, pop up a message box asking the user if they want to add the contact. I've...
1
by: Ben | last post by:
Hi I would like to complete a task whenever F4 is pressed by the user anywhere on a form. The problem is the Key Down event does not fire for the form if the user is, for example within a...
1
by: JDeats | last post by:
If you create a new C# Windows Application project in Visual Studio.NET 2003 or 2005 and proceed to. 1. Make default form size 800x600 2. Drop a CheckBoxList control and dock it to the left 3....
3
by: pgill | last post by:
I have a form that displays customer information in a list. I have a text box to search for a specific customer in the list. What I would like to do is that as the user types the customer name it...
1
by: karaballo | last post by:
Hi All, How to trap arrow key down event on MDIChild form? Form alone works fine without MDI container. I think, ToolStrip stole these arrows key down events. How I can redirect arrow key down...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.