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.
Welcome to Bucaro TecHelp!

Welcome to 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 Advertise on Bucaro TecHelp Advertise Here RSS News Feeds News Feeds

HTML5 Solutions: Essential Techniques for HTML5 Developers

Essential Techniques for HTML5 Developers

HTML5 brings the biggest changes to HTML in years. Web designers now have new techniques, from displaying video and audio natively in HTML, to creating realtime graphics on a web page without a plugin.

This book provides a collection of solutions to all of the most common HTML5 problems. Every solution contains sample code that is production-ready and can be applied to any project.

Click Here

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.

symbol	code

a	97
b	98
c	99
d	100
e	101
f	102
g	103
h	104
i	105
j	106
k	107
l	108
m	109
n	110
o	111
p	112
q	113
r	114
s	115
t	116
u	117
v	118
w	119
x	120
y	121
z	122

A	65
B	66
C	67
D	68
E	69
F	70
G	71
H	72
I	73
J	74
K	75
L	76
M	77
N	78
O	79
P	80
Q	81
R	82
S	83
T	84
U	85
V	86
W	87
X	88
Y	89
Z	90

space	32
!	33
"	34
#	35
$	36
%	37
&	38
'	39
(	40
)	41
*	42
+	43
,	44
-	45
.	46
/	47
0	48
1	49
2	50
3	51
4	52
5	53
6	54
7	55
8	56
9	57
:	58
;	59
<	60
=	61
>	62
?	63
@	64

RSS Feed RSS Feed



Web Design Sections

Eloquent JavaScript: A Modern Introduction to Programming

Eloquent JavaScript

Eloquent JavaScript Eloquent JavaScript goes beyond the cut-and-paste scripts of the recipe books and teaches you to write code that's elegant and effective. You'll start with the basics of programming, and learn to use variables, control structures, functions, and data structures. Then you'll dive into the real JavaScript artistry: higher-order functions, closures, and object-oriented programming.

Reader Anthony says, "This book is not your typical Javascript book. Others have a utilitarian approach. In stark contrast, Eloquent JavaScript does not merely provide you a checklist of things to learn but rather paints a panorama of the possibilities that programming provides. Javascript is merely the tool used to introduce these to the reader.

Click here for more information.


[Site User Agreement] [Advertise on This site] [Search This Site] [Contact Form]
Copyright©2001-2011 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268