Home arrow HTML arrow Page 4 - 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 - The Second if (else if) block
(Page 4 of 5 )

The second if (else if) block is:


else if (/./.test(keychar))

{

//construct the digit button ID

ID = "BPOINT";

//call the showNum() function with the ID

showNum(ID);

//give the button focus

document.getElementById(ID).focus();

}


The “if else” line uses a regular expression to verify that the character is the decimal point. If it is, it creates the ID of the corresponding calculator button by forming the string "BPOINT." That is, it assigns “BPOINT” to the local ID variable. 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 the decimal point 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 of the block gives the focus.

The Third if (else if) block

The third if (else if) block is:


else if (/+|-|*|/|=/.test(keychar))

{

//construct the digit button ID

if (keychar == '+')

ID = "BP";

if (keychar == '-')

ID = "BMI";

if (keychar == '*')

ID = "BX";

if (keychar == '/')

ID = "BD";

if (keychar == '=')

ID = "BEQ";

//call the showNum() function with the ID

operator(ID);

//give the button focus

document.getElementById(ID).focus();

}


The “if else” line uses a regular expression to verify that an operator character on the keyboard was pressed. If it was, the block then has five options, each of which is in an “if” statement. 

The first optional “if” statement checks to see if the ‘+’ key on the keyboard was pressed. If it was, it assigns the ID (“BP”) of the corresponding ‘+’ calculator button to the local ID variable.

The second optional “if” statement checks to see if the ‘-’ key on the keyboard was pressed. If it was, it assigns the ID (“BMI”) of the corresponding ‘-’ calculator button to the local ID variable.

The third “if” statement checks to see if the ‘*’ key on the keyboard was pressed. If it was, it assigns the ID (“BX”) of the corresponding ‘*’ calculator button to the local ID variable.

The fourth “if” statement checks to see if the ‘/’ key on the keyboard was pressed. If it was, it assigns the ID (“BD”) of the corresponding ‘/’ calculator button to the local ID variable.

The fifth “if” statement checks to see if the ‘=’ key on the keyboard was pressed. If it was, it assigns the ID (“BEQ”) of the corresponding ‘=’ calculator button to the local ID variable.

We can press an operator key on the keyboard. This has to be equivalent to clicking the corresponding button on the calculator. When you click any button on a web page, that button receives focus. So the corresponding button on the calculator has to receive focus when you press a button on the keyboard. The last line of the block gives the button 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 5 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials