Keypress on a hyperlink

X

Xandax

Hiya all.

I'm trying to capture which key is pressed when a hyperlink is active,
which I'd thought would be simpel.
However - IE returns undefined and FireFox returns 0 when I try to
alert the keycode value, which means the event fires, but I apparently
can't get the keycode which strikes me as odd.

The code I use looks something like this (simplified):
<a href="somelink" onkeypress="return trapEnterNote(1, event)"><img
src="someimage"></a>

Then in my function I do this:

function trapEnterNote(intID, e)
{
var keycode;
if (window.event) keycode = window.event.keycode;
else if (e) keycode = e.which;
else return true
alert(keycode);
if (keycode == 13)
{
function trapEnterNote(intID, e)
{
alert('Whiskey Tango Foxtrot');
var keycode;
if (window.event) keycode = window.event.keycode;
else if (e) keycode = e.which;
else return true
alert(keycode);
return false;
}

This returns "undefined" and 0 as mentioned in the "alert(keycode);".

Is it not possible to get which key was pressed on a hyperlink, is it
only for input fields? Or is it me that does something wrong, and if so
.... any hints/help?

Regards
Xandax
 
X

Xandax

Oh - FYI,but it is somekind of copy/paste error which made my function
TrapEnter appear twice. It is not twice in my code.
 
R

RobG

Xandax said:
Hiya all.

I'm trying to capture which key is pressed when a hyperlink is active,
which I'd thought would be simpel.
However - IE returns undefined and FireFox returns 0 when I try to
alert the keycode value, which means the event fires, but I apparently
can't get the keycode which strikes me as odd.

The code I use looks something like this (simplified):
<a href="somelink" onkeypress="return trapEnterNote(1, event)"><img
src="someimage"></a>

Then in my function I do this:

function trapEnterNote(intID, e)
{
var keycode;
if (window.event) keycode = window.event.keycode;

You are after keyCode, not keycode.

Try the concise version below:

function trapEnterNote(intID, e)
{
var e = e || window.event;
var keycode = e.which || e.keyCode;
if (keycode == 13) alert('Whiskey Tango Foxtrot');
}


[...]
Is it not possible to get which key was pressed on a hyperlink, is it
only for input fields? Or is it me that does something wrong, and if so
... any hints/help?

'A' elements have onkeypress events too:

<URL:http://www.w3.org/TR/html4/struct/links.html#edef-A>
 
X

Xandax

Danny skrev:
You get nothing because onkeypress is an string-containing r/w element
event handler, like an text field or a textarea or document, as far as I
known, a link element doesn't have a keypress event, what you can do is
engage it for its parent or grandparent which may have it, like:


<a href="..." onclick="document.onkeypress=soso;">


function soso(ev) {
asciiVal=(window.external) ? event.keyCode :
ev.keyCode;

alert(asciiVal);

}

IE uses event on the called function, mozilla uses the 1st arguement.

Danny

As written in my reply from RobG and my acknowlegdement of the post,
the problem is solved. Hyperlinks have infact keypressed events, my
problem was that I'd used a noncapital c in the keycode where it should
have been keyCode.
Once that was changed, all worked as I wished it to do :)


Xandax
 
M

Michael Winter

On 02/10/2005 07:31, Danny wrote:

[snip]
function soso(ev) {
asciiVal=(window.external) ? event.keyCode : ev.keyCode;

Exactly what relationship do you think the global external property has
with the event model of any particular host? Do not infer behaviour from
unrelated object model features.

var global = this;

function myFunction(event) {
var keyCode;

if((event = event || global.event)) {
keyCode = ('number' == typeof event.keyCode)
? event.keyCode
: event.which;

if(keyCode) {
/* ... */
}
}
}

[snip]

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top