Home arrow HTML arrow Page 3 - More on the Keyboard for a Web Page Calculator
HTML

More on the Keyboard for a Web Page Calculator


In this fifth part of a six-part series on building a web page calculator, we shall see how to use the keyboard with the calculator. The event involved is the onkeypress event, which we saw in the last part of the series. For an element to respond to the event, it has to have focus. With some browsers, if all of the elements that have to respond to the keyboard have the same function, and are in the same group (on the calculator), you only have to give one of the elements focus. This is the case with my browser.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
April 01, 2009
TABLE OF CONTENTS:
  1. · More on the Keyboard for a Web Page Calculator
  2. · The processKey(e) Function
  3. · Details of the processKey(e) Code segments
  4. · The Second if (else if) block
  5. · The Fourth if (else if) block

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
More on the Keyboard for a Web Page Calculator - Details of the processKey(e) Code segments
(Page 3 of 5 )

The first Code Segment for Internet Explorer

The first code segment is:


keynum = e.keyCode;

keychar = String.fromCharCode(keynum)


The processKey(e) function has the event object as an argument. With Internet Explorer the expression


e.keyCode


where e represents the event object, returns the key code of the key pressed on the keyboard. The second line in this segment is entirely JavaScript; it is the same for many browsers. This line converts the key code into the corresponding character, such as ‘p’, ‘f’, ‘3’, ‘5’, etc.

The first Code Segment for Netscape, Firefox, and Opera

The first code segment for these browsers is:


keynum = e.which;

keychar = String.fromCharCode(keynum);


As I said, the processKey(e) function has the event object as an argument. With Netscape, Firefox and Opera, the expression


e.which


where e represents the event object, returns the key code of the key pressed on the keyboard. The second line in this segment is entirely JavaScript; it is the same for the many browsers.

The processKey(e) if-else-if blocks

Each of these blocks creates the ID of the calculator button corresponding to the key that was pressed on the keyboard. It uses this ID to call the relevant function. It then gives focus to the button on the calculator that corresponds to the key that was pressed on the keyboard.

Recall that when you have if-else-if blocks, only one block is executed when the function is called.


The First if-block

The first if-block is:


if (/d/.test(keychar))

{

//construct the digit button ID

ID = "B" + keychar;

//call the showNum() function with the ID

showNum(ID);

//give the button focus

document.getElementById(ID).focus();

}


The “if” line uses a regular expression to verify that the character is a digit. If it is, it creates the ID of the corresponding calculator button by forming a string with "B" in front of the digit. The ID of each digit button begins with "B" followed by the digit; the ID is made up of those two characters. The block then goes on to call the showNum(ID) function using the ID created. Remember that the main purpose of the showNum(ID) function is to show the digit pressed on the display.

We can press a digit key on the keyboard. This has to be equivalent to clicking the corresponding button on the calculator. So this corresponding button on the calculator has to receive focus. The last line on the block gives the focus.


blog comments powered by Disqus
HTML ARTICLES

- HTML5 Boilerplate: Working with jQuery and M...
- HTML5 Boilerplate Introduction
- New API Platform for HTML5
- BBC Adopts HTML 5, Mozilla Addresses Issues
- Advanced Sticky Footers in HTML and CSS
- HTML and CSS Sticky Footers
- Strategy Analytics Predicts HTML5 Phones to ...
- HTML5 Guidelines for Web Developers
- Learning HTML5 Game Programming
- More Engaging CSS3 and HTML Background Effec...
- Engaging HTML and CSS3 Background Effects
- More Web Columns with CSS3 and HTML
- Columns with CSS3 and HTML
- Creating Inline-Block HTML Elements with CSS
- Drag and Drop in HTML5: Parsing Local Files

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials