473,405 Members | 2,349 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,405 software developers and data experts.

IE event and keyCode/charCode

I'm trying to figure out what key the user pressed using a Danish
keyboard layout. charCodeAt returns the correct number, but
event.keyCode returns a wrong number, when using one of the keys that
are different on a Danish keyboard layout. "-" returns 45 and 189
respectively. Etc. (The input-field in the sample code below displays
the right character).

I've tried setting the lang-parameter to 'da', but it doesn't make a
difference.

sample code:

<input type="text"
onkeyup="alert(this.value.charCodeAt(this.value.le ngth-1));alert(window.event.keyCode);">

Am I missing something, or is the event.keyCode in IE hardcoded to an
english keyboard layout? Is there a way to automatically convert the
event.keyCode so it corresponds with the users keyboard?

Thomas
Oct 20 '05 #1
4 21212
Thomas Christensen wrote:
I'm trying to figure out what key the user pressed using a Danish
keyboard layout. charCodeAt returns the correct number, but
event.keyCode returns a wrong number, when using one of the keys that
are different on a Danish keyboard layout. "-" returns 45 and 189
respectively. Etc. (The input-field in the sample code below displays
the right character).
This maybe a silly question, but are you trying to get the key that was
pressed, or the character that was entered?

Try some of these links:

<URL:http://developer.mozilla.org/en/docs/keyCode>
<URL:http://developer.mozilla.org/en/docs/DOM:event.charCode>

<URL:http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/keycode.asp>

the keyCode maps to the Unicode character of the keyboard key - that is,
the actual key that was pressed. e.g. pressing 'u' gives a charCode of
117 (lower case u) and a keyCode of 85 (the U key).

Hold down the shift key, and you get 85 for both, which is the code for
an upper case U character and the U key.

charCode takes into account the whether modifier keys (shift, control,
etc.) are pressed, keyCode doesn't.

I've tried setting the lang-parameter to 'da', but it doesn't make a
difference.

sample code:

<input type="text"
onkeyup="alert(this.value.charCodeAt(this.value.le ngth-1));alert(window.event.keyCode);">
Am I missing something, or is the event.keyCode in IE hardcoded to an
english keyboard layout? Is there a way to automatically convert the
event.keyCode so it corresponds with the users keyboard?


Play with this:

<input type="text" onkeyup="
document.getElementById('zz').innerHTML =
'<pre>charCodeAt: ' + this.value.charCodeAt(this.value.length-1)
+ '<br>keyCode :' + event.keyCode + '</pre>;
">

<div id="zz"></div>

--
Rob
Oct 20 '05 #2

Thomas Christensen wrote:
I'm trying to figure out what key the user pressed using a Danish
keyboard layout. charCodeAt returns the correct number, but
event.keyCode returns a wrong number, when using one of the keys that
are different on a Danish keyboard layout. "-" returns 45 and 189
respectively. Etc. (The input-field in the sample code below displays
the right character).

I've tried setting the lang-parameter to 'da', but it doesn't make a
difference.

sample code:

<input type="text"
onkeyup="alert(this.value.charCodeAt(this.value.le ngth-1));alert(window.event.keyCode);">

Am I missing something, or is the event.keyCode in IE hardcoded to an
english keyboard layout? Is there a way to automatically convert the
event.keyCode so it corresponds with the users keyboard?

Thomas


I am not sure if this answers your question, but I note that you are
using "onkeyup". Try using "onkeypress", as the keyCode for this event
captures the selected character (taking account of the shiftKey etc),
whereas "onkeydown" and "onkeyup" only seem to capture raw information
about the physical key you pushed on the keyboard.

Julian

Oct 20 '05 #3
Thomas Christensen wrote:
I'm trying to figure out what key the user pressed using a Danish
keyboard layout.
<input type="text"
onkeyup="alert(this.value.charCodeAt(this.value.le ngth-1));alert(window.event.keyCode);">

Am I missing something, or is the event.keyCode in IE hardcoded to an
english keyboard layout? Is there a way to automatically convert the
event.keyCode so it corresponds with the users keyboard?


The keyCode is not hardcoded to an english layout.
For IE in keyDown and keyUp events the keyCode is taken from the keyCode
constants.

http://msdn.microsoft.com/library/de...econstants.asp
http://www.w3.org/TR/1999/WD-DOM-Lev...23/events.html

In keyPress it is the Unicode character (codepoint?).
So maybe you can change your onkeyup to onkeypress.

You can play a little to see what works here
http://www.w3.org/2002/09/tests/keys.html

Als keep in mind that Mozilla/Firefox works different with these events.
Oct 20 '05 #4
Julian Turner wrote:
I am not sure if this answers your question, but I note that you are
using "onkeyup". Try using "onkeypress", as the keyCode for this event
captures the selected character (taking account of the shiftKey etc),
whereas "onkeydown" and "onkeyup" only seem to capture raw information
about the physical key you pushed on the keyboard.

Julian


It works, but onkeypress occurs before onkeyup and before the content
of the event source (ie. the input field gets updated). I can work
around that though. Thanks!

Oct 20 '05 #5

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

Similar topics

3
by: yzzzzz | last post by:
Hi I have: <div onkeypress="go(event)">...</div> and: function go(event) { alert(event.keyCode); }
2
by: Pavils Jurjans | last post by:
Hello, I wanted to propose a small class that would help to overcome the feature that's missing in MSIE. I'd like to get some feedback from people and, perhaps, improvements in code/other ideas:...
2
by: Marcia Gulesian | last post by:
The following code suppresses the 'enter' key, when run in I.E. 5.5 or later (Windows) but not when run in Safari (Mac) <body onkeypress="javascript:keysuppress(event)" > function...
3
by: Lachlan Hunt | last post by:
Hi, I've been looking up lots of documentation and trying to work out a cross-platform method of capturing a keyboard event and working out which key was pressed. From what I can find, there...
1
by: Perttu Pulkkinen | last post by:
I have different functions that receive window.event as parameter. Functions are used like this: <input type="text" id="x" onkeypress="return onKeyCurrencyCheck(ev, 'x')" onblur...
6
by: rich_poppleton | last post by:
Help.... I've got a textarea where people type in a description. However for certain reasons we need to stop them typing !$*^ . I have a solution this which works fine in IE: function...
3
by: Jeff | last post by:
I have had this function work perfectly in IE and am trying to get it to work in Firefox. I have seen plenty of questions and answers on the web for how to get and check the key pressed but nothing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.