diff --git a/README.md b/README.md index 916b127..82653df 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/javascript/character-codes-from-keyboard-listeners.md b/javascript/character-codes-from-keyboard-listeners.md new file mode 100644 index 0000000..0e02684 --- /dev/null +++ b/javascript/character-codes-from-keyboard-listeners.md @@ -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.