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.
More on the Keyboard for a Web Page Calculator (Page 1 of 5 )
Modifying the Initialization Function
The initialization function is the showCalc() function. When the web page is opened, and you click the Use Calculator button, this function is called. The function opens the calculator and does any initialization necessary. The only initialization it has to do is give focus to the zero digit button. So add the following line at the bottom of the showCalc() function.
Handling Keyboard Presses
When you press any key on the keyboard, the function processKey(e) is called. This function is added to do all the processing of keyboard issues.