Welcome to Bucaro TecHelp!

Bucaro TecHelp
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Victims of Sandy Hook

Stop the Slaughter of Innocents. Congress is bought and paid for by gun lunatics and gun promotion groups. If you want to live in a safe America, help buy Congress back for America. Send a donation to Mayors Against Illegal Guns, 909 Third Avenue, 15th Floor New York, NY 10022

Working With the Keyboard in Java Script

Using Java Script you can determine which keyboard key the user pressed. You can use this information to block certain keys, to replace a key's character, or to perform an action based upon a specific key being pressed. In this article, I'll provide some basic code for working with the keyboard.

The Internet Explorer and Firefox browsers use slightly different syntax to capture keyboard events. If you want to determine which key the user pressed when the mouse pointer was anywhere inside the browser window, Internet Explorer places the key code in window.event.keyCode", but FireFox passes the key code as a parameter to the document's onkeypress function.

The code shown below displays the key code in a message box in both Internet Explorer and Firefox when the user presses a key anywhere in the browser window. (In other words the browser window has the focus).

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>

<script type="text/javascript">

function keyPressed(e)
{
  var key;

  if(e) key = e.which;
  else key = event.keyCode;

  alert(key);
}
document.onkeypress = keyPressed;
</script>

</head>
<body>

</body>
</html>

If you want to determine which key the user pressed inside an element such a text box, Internet Explorer returns the key code in event.keyCode, but Firefox returns the key code in event.charCode.

The code shown below displays the key code in a message box in both Internet Explorer and Firefox when the user presses a key inside a textbox.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>

<script type="text/javascript">

function keyPressed(event)
{
  var key;

  if(event.keyCode) key = event.keyCode;
  else key = event.charCode;

  alert(key);
}
</script>

</head>
<body>

<form>
  <input type="text" onkeypress="keyPressed(event)">
</form>

</body>
</html>

The key codes being returned are the ASCII codes representing the keys being pressed. Shown below is an incomplete list of ASCII codes.

symbolcodesymbolcode
A65a97
B66b98
C67c99
D68d100
E69e101
F70f102
G71g103
H72h104
I73i105
J74j106
K75k107
L76l108
M77m109
N78n110
O79o111
P80p112
Q81q113
R82r114
S83s115
T84t116
U85u117
V86v118
W87w119
X88x120
Y89y121
Z90z122

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro



Web Design Sections

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2016 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268