keycodes - same for uppercase and lowercase?

R

Robert Mark Bram

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
:)
 
R

RobG

Robert said:
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/co...keypress+keycode+which&hl=en#627d4ee7f722a3a4>
 
R

RobG

RobG said:
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);
}

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

Robert Mark Bram

Hi Rob,
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
:)
 
R

rf

Robert Mark Bram said:
Hi Rob,



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
 
R

RobG

Robert said:
Hi Rob,




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.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top