Skip to content

Commit 5c6a835

Browse files
committed
Add Character Codes from Keyboard Listeners as a javascript til.
1 parent 6b0a5fa commit 5c6a835

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ smart people at [Hashrocket](http://hashrocket.com/).
3434

3535
- [Not So Random](go/not-so-random.md)
3636

37+
## javascript
38+
39+
- [Character Codes from Keyboard Listeners](javascript/character-codes-from-keyboard-listeners.md)
40+
3741
### postgres
3842

3943
- [Timestamp Functions](postgres/timestamp-functions.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Character Codes from Keyboard Listeners
2+
3+
If I create the following keyboard event listeners for `keydown`,
4+
`keypress`, and `keyup`:
5+
6+
```javascript
7+
window.addEventListener('keydown', function(e) { console.log("Keydown: " + e.charCode + ", " + e.keyCode); });
8+
window.addEventListener('keypress', function(e) { console.log("Keypress: " + e.charCode + ", " + e.keyCode); });
9+
window.addEventListener('keyup', function(e) { console.log("Keyup: " + e.charCode + ", " + e.keyCode); });
10+
```
11+
12+
and then I press `A`, my browser console will read the following:
13+
14+
```
15+
Keydown: 0, 65
16+
Keypress: 65, 65
17+
Keyup: 0, 65
18+
```
19+
20+
and if I then press `a`, my browser console will read:
21+
22+
```
23+
Keydown: 0, 65
24+
Keypress: 97, 97
25+
Keyup: 0, 65
26+
```
27+
28+
The `keypress` event seems to be the way to go. Regardless, there seems to
29+
be quite a bit of [incompatibility and lack of
30+
support](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Browser_compatibility)
31+
across browsers for various aspects of the keyboard events.

0 commit comments

Comments
 (0)