HTML and CSS Reference
In-Depth Information
{
keyCode = window.event.keyCode;
window.event.preventDefault();
}
else
{
keyCode = event.keyCode;
event.preventDefault();
}
The preventDefault function does what it sounds like: it prevents any default action, such as special
shortcut actions associated with particular keys. The only keys of interest in this application are the three
keys d, h, and n. The following switch statement determines which key is pressed and invokes the
correct function: deal , playerdone , or newgame . A switch statement compares the value in the
parentheses with the values after the term case and starts executing the statements with the first one
that matches. The break; statement causes execution to jump out of the switch statement. The
default clause is what it sounds like. It is not necessary, but if it is present, the statement or statements
following default: are executed if nothing matches the case values provided.
switch(keyCode)d
{
case 68: //d
deal();
break;
case 72: //h
playerdone();
break;
case 78: //n
newgame();
break;
default:
alert("Press d, h, or n.);
}
Recall that you can determine the key code of any key by modifying the whole switch statement to have
just the following line in the default case:
alert(" You just pressed keycode "+keyCode);
and doing the experiment of pressing on the key and writing down what number shows up.
Caution: If, like I sometimes do, you move among different windows on your computer, you may find
that when you return to the blackjack game and press a key, the program does not respond. You will
need to click the mouse on the window holding the blackjack document. This lets the operating
system restore the focus on the blackjack document so the listening for the key press can take
place.
 
Search WWH ::




Custom Search