Hi there,
So, I made a little demo script to get you started.
You’ll want to listen for the keyup event, as otherwise someone could just keep a key pressed and rack up a high score.
Have a look at this and see if you can adapt it to your needs (you’ll want to look at setTimeout to limit the time the user has to press a key, as well as the [URL=“http://api.jquery.com/off/”].off() method to remove the event listener after a certain period of time).
Let us know how you get on.
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>jQuery on keyUp</title>
</head>
<body>
<p>Total keypresses: <span id="totalKeypresses">0</span><p>
<p id="result"></p>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
var totalKeypresses = 0;
var keyCodes = {
48: "0",
49: "1",
50: "2",
51: "3",
52: "4",
53: "5",
54: "6",
55: "7",
56: "8",
57: "9",
58: "0",
65: "a",
66: "b",
67: "c",
68: "d",
69: "e",
70: "f",
71: "g",
72: "h",
73: "i",
74: "j",
75: "k",
76: "l",
77: "m",
78: "n",
79: "o",
80: "p",
81: "q",
82: "r",
83: "s",
84: "t",
85: "u",
86: "v",
87: "w",
88: "x",
89: "y",
90: "z"
}
$("body").on("keyup", function(e){
$("#result").text("Key recognized: " + keyCodes[e.keyCode]);
totalKeypresses++;
$("#totalKeypresses").text(totalKeypresses);
});
</script>
</body>
</html>