From 5d769c39899eb54b85aff6c88c5cda8f6875fe0f Mon Sep 17 00:00:00 2001 From: Charles Gould Date: Sat, 30 May 2020 22:28:58 -0500 Subject: [PATCH] Replace charCode with key --- server/src/main/resources/static/client.js | 27 +++++------------- server/src/main/resources/static/practice.js | 29 ++++++-------------- 2 files changed, 15 insertions(+), 41 deletions(-) diff --git a/server/src/main/resources/static/client.js b/server/src/main/resources/static/client.js index 70ab716..94d2352 100644 --- a/server/src/main/resources/static/client.js +++ b/server/src/main/resources/static/client.js @@ -216,6 +216,7 @@ const vm = new Vue({ this.myGuess = this.myGuess.substr(0, this.myGuess.length - 1); this.repaint(); } else if (e.key === 'Enter') { + e.preventDefault(); if (this.myGuess.length === this.wordLength) { client.publish({destination: '/app/guess', body: this.myGuess}) this.myGuess = ''; @@ -224,16 +225,10 @@ const vm = new Vue({ } }, onCanvasKeypress: function(e) { - let charCode = e.charCode; - if (isCharacter(charCode)) { - if (isCharacterLowercase(charCode)) { - charCode = charCode - 32; - } - const char = String.fromCharCode(charCode); - if (this.myGuess.length < this.wordLength) { - this.myGuess += char; - this.repaint(); - } + const key = e.key.toUpperCase(); + if (this.myGuess.length < this.wordLength && isCharacter(key)) { + this.myGuess += key; + this.repaint(); } }, onChatKeypress: function(e) { @@ -469,16 +464,8 @@ function doHttpGet(url, callback) { xhr.send(); } -function isCharacter(charCode) { - return isCharacterLowercase(charCode) || isCharacterUppercase(charCode); -} - -function isCharacterLowercase(charCode) { - return charCode >= 97 && charCode <= 122; -} - -function isCharacterUppercase(charCode) { - return charCode >= 65 && charCode <= 90; +function isCharacter(key) { + return key >= "A" && key <= "Z"; } function onChat(message) { diff --git a/server/src/main/resources/static/practice.js b/server/src/main/resources/static/practice.js index 6d8a0b3..df77dcb 100644 --- a/server/src/main/resources/static/practice.js +++ b/server/src/main/resources/static/practice.js @@ -61,11 +61,12 @@ function start() { function addKeydownListener() { document.addEventListener('keydown', function(e) { if (e.key === 'Backspace') { + e.preventDefault(); myGuess = myGuess.substr(0, myGuess.length - 1); repaint(); - e.preventDefault(); } else if (e.key === 'Enter') { + e.preventDefault(); if (myGuess.length === 5) { client.publish({destination: '/app/practiceGuess', body: myGuess}); myGuess = ''; @@ -78,16 +79,10 @@ function addKeydownListener() { // characters function addKeypressListener() { document.addEventListener('keypress', function(e) { - let charCode = e.charCode; - if (isCharacter(charCode)) { - if (isCharacterLowercase(charCode)) { - charCode = charCode - 32; - } - const char = String.fromCharCode(charCode); - if (myGuess.length < 5) { - myGuess += char; - repaint(); - } + const key = e.key.toUpperCase(); + if (myGuess.length < 5 && isCharacter(key)) { + myGuess += key; + repaint(); } }); } @@ -181,16 +176,8 @@ function drawHint(xOrigin, yOrigin, progress) { } } -function isCharacter(charCode) { - return isCharacterLowercase(charCode) || isCharacterUppercase(charCode); -} - -function isCharacterLowercase(charCode) { - return charCode >= 97 && charCode <= 122; -} - -function isCharacterUppercase(charCode) { - return charCode >= 65 && charCode <= 90; +function isCharacter(key) { + return key >= "A" && key <= "Z"; } function repaint() {