1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Character Codes from Keyboard Listeners as a javascript til.

This commit is contained in:
jbranchaud
2015-05-02 09:42:45 -05:00
parent 6b0a5fa1f9
commit 5c6a8358fd
2 changed files with 35 additions and 0 deletions

View File

@@ -34,6 +34,10 @@ smart people at [Hashrocket](http://hashrocket.com/).
- [Not So Random](go/not-so-random.md)
## javascript
- [Character Codes from Keyboard Listeners](javascript/character-codes-from-keyboard-listeners.md)
### postgres
- [Timestamp Functions](postgres/timestamp-functions.md)

View File

@@ -0,0 +1,31 @@
# Character Codes from Keyboard Listeners
If I create the following keyboard event listeners for `keydown`,
`keypress`, and `keyup`:
```javascript
window.addEventListener('keydown', function(e) { console.log("Keydown: " + e.charCode + ", " + e.keyCode); });
window.addEventListener('keypress', function(e) { console.log("Keypress: " + e.charCode + ", " + e.keyCode); });
window.addEventListener('keyup', function(e) { console.log("Keyup: " + e.charCode + ", " + e.keyCode); });
```
and then I press `A`, my browser console will read the following:
```
Keydown: 0, 65
Keypress: 65, 65
Keyup: 0, 65
```
and if I then press `a`, my browser console will read:
```
Keydown: 0, 65
Keypress: 97, 97
Keyup: 0, 65
```
The `keypress` event seems to be the way to go. Regardless, there seems to
be quite a bit of [incompatibility and lack of
support](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Browser_compatibility)
across browsers for various aspects of the keyboard events.