By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,567 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,567 IT Pros & Developers. It's quick & easy.

keycodes - same for uppercase and lowercase?

P: n/a
Hi All,

This page:
http://www.lookuptables.com/
shows that the decimal ASCII code for 'A' is 65 and 'a' is 97.

Yet I find that the following code in Firefox and IE show 65 for a and
A.

<html>
<head>
<script type="text/javascript">
function keyPressed(event) {
if (event == null) {
event = window.event;
}
var keycode;
if (window.event) {
keycode = window.event.keyCode;
} else if (event) {
keycode = event.which;
}
alert(keycode);
}
</script>
</head>
<body>
<form>
<input type="text" onkeyup="keyPressed(event);"/>
</form>
</body>
</html>

Why is this?

Rob
:)

Sep 26 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Robert Mark Bram wrote:
Hi All,

This page:
http://www.lookuptables.com/
shows that the decimal ASCII code for 'A' is 65 and 'a' is 97.

Yet I find that the following code in Firefox and IE show 65 for a and
A.

<html>
<head>
<script type="text/javascript">
function keyPressed(event) {
if (event == null) {
event = window.event;
}
This test seems redundant. If 'event' is null, then you have the IE
event model and so use window.event. The local variable 'event' is not
used so there seems little point in using it.

But that could be just plain picky :-)

[...]

Why is this?


See of this recent post helps any ("Associating keys to a function"):

<URL:http://groups.google.co.uk/group/comp.lang.javascript/browse_frm/thread/833757766c208858/627d4ee7f722a3a4?tvc=1&q=modifier+keys+onkeypress+ keycode+which&hl=en#627d4ee7f722a3a4>
--
Rob
Sep 26 '05 #2

P: n/a
RobG wrote:
Robert Mark Bram wrote:
Hi All,

This page:
http://www.lookuptables.com/
shows that the decimal ASCII code for 'A' is 65 and 'a' is 97.

Yet I find that the following code in Firefox and IE show 65 for a and
A.

<html>
<head>
<script type="text/javascript">
function keyPressed(event) {
if (event == null) {
event = window.event;
}

This test seems redundant. If 'event' is null, then you have the IE
event model and so use window.event. The local variable 'event' is not
used so there seems little point in using it.


I'll correct that. Better to use feature detection for the feature you
are checking for, not some other feature that you think infers the
feature you'd like to use.

i.e. if you want to check for event.keyCode, then check for that, don't
check for window.event and presume that means support for event.keyCode
(although it will likely work nearly always...).

function keyPressed( e )
{
var e = e || window.event;
var keycode='Key pressed: ';
if (e.keyCode) {
keycode += e.keyCode;
} else if (e.which){
keycode += e.which;
} else {
return;
}
keycode += '\nAlt key pressed? ' + e.altKey;
keycode += '\nShift key pressed? ' + e.shiftKey;
keycode += '\nCtrl key pressed? ' + e.ctrlKey;

alert(keycode);
}

[...]

Why is this?


I'll guess that it's because ASCII copied the ancient Bell 7-bit
teleprinter codes and modern keyboards continue the same scheme.
--
Rob
Sep 26 '05 #3

P: n/a
Hi Rob,
Why is this?
I'll guess that it's because ASCII copied the ancient Bell 7-bit
teleprinter codes and modern keyboards continue the same scheme.


Not sure I understand this answer. Why can we type an 'a' or 'A' into a
text area and it knows whether we are typing an upper or lower case
letter, but JavaScript can't? Aren't they working from the same event
model i.e. an event object and keycode provided by the browser?

Rob
:)

Sep 26 '05 #4

P: n/a
rf

"Robert Mark Bram" <re********@optusnet.com.au> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi Rob,
Why is this?

I'll guess that it's because ASCII copied the ancient Bell 7-bit
teleprinter codes and modern keyboards continue the same scheme.


Not sure I understand this answer. Why can we type an 'a' or 'A' into a
text area and it knows whether we are typing an upper or lower case
letter, but JavaScript can't? Aren't they working from the same event
model i.e. an event object and keycode provided by the browser?


You are missing the point. You are hooking the keyup event. This is a low
level keyboard event, there has been no translation yet to upper/lower case.

There is only one A key. The difference between an a and an A is the state
of the shift key.

Look at event.shiftKey. If set then the shift key is pressed. You might also
need to look at
Sep 26 '05 #5

P: n/a
Robert Mark Bram wrote:
Hi Rob,

Why is this?


I'll guess that it's because ASCII copied the ancient Bell 7-bit
teleprinter codes and modern keyboards continue the same scheme.

Not sure I understand this answer. Why can we type an 'a' or 'A' into a
text area and it knows whether we are typing an upper or lower case
letter, but JavaScript can't? Aren't they working from the same event
model i.e. an event object and keycode provided by the browser?


RF was on the trail but seems to have posted prematurely...

The keycode for the A key is the code for the key, not the letter. If
the shift key is pressed at the same time, the computer says 'Oh, shift
and A are pressed simultaneously, let's pass that to the application'.
The application then decides what to do with it - usually make it a
capital 'A'.

If Ctrl+A are pressed, then most applications will select everything in
the window in focus, and so on.

Some keys are not passed to the JavaScript interface - F1 is nearly
always used for help and is used by the browser, it isn't passed through
to the script engine.

--
Rob
Sep 26 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.