Replace charCode with key

This commit is contained in:
Charles Gould 2020-05-30 22:28:58 -05:00
parent a38c19b9c9
commit 5d769c3989
2 changed files with 15 additions and 41 deletions

View File

@ -216,6 +216,7 @@ const vm = new Vue({
this.myGuess = this.myGuess.substr(0, this.myGuess.length - 1); this.myGuess = this.myGuess.substr(0, this.myGuess.length - 1);
this.repaint(); this.repaint();
} else if (e.key === 'Enter') { } else if (e.key === 'Enter') {
e.preventDefault();
if (this.myGuess.length === this.wordLength) { if (this.myGuess.length === this.wordLength) {
client.publish({destination: '/app/guess', body: this.myGuess}) client.publish({destination: '/app/guess', body: this.myGuess})
this.myGuess = ''; this.myGuess = '';
@ -224,17 +225,11 @@ const vm = new Vue({
} }
}, },
onCanvasKeypress: function(e) { onCanvasKeypress: function(e) {
let charCode = e.charCode; const key = e.key.toUpperCase();
if (isCharacter(charCode)) { if (this.myGuess.length < this.wordLength && isCharacter(key)) {
if (isCharacterLowercase(charCode)) { this.myGuess += key;
charCode = charCode - 32;
}
const char = String.fromCharCode(charCode);
if (this.myGuess.length < this.wordLength) {
this.myGuess += char;
this.repaint(); this.repaint();
} }
}
}, },
onChatKeypress: function(e) { onChatKeypress: function(e) {
const messageInput = e.target; const messageInput = e.target;
@ -469,16 +464,8 @@ function doHttpGet(url, callback) {
xhr.send(); xhr.send();
} }
function isCharacter(charCode) { function isCharacter(key) {
return isCharacterLowercase(charCode) || isCharacterUppercase(charCode); return key >= "A" && key <= "Z";
}
function isCharacterLowercase(charCode) {
return charCode >= 97 && charCode <= 122;
}
function isCharacterUppercase(charCode) {
return charCode >= 65 && charCode <= 90;
} }
function onChat(message) { function onChat(message) {

View File

@ -61,11 +61,12 @@ function start() {
function addKeydownListener() { function addKeydownListener() {
document.addEventListener('keydown', function(e) { document.addEventListener('keydown', function(e) {
if (e.key === 'Backspace') { if (e.key === 'Backspace') {
e.preventDefault();
myGuess = myGuess.substr(0, myGuess.length - 1); myGuess = myGuess.substr(0, myGuess.length - 1);
repaint(); repaint();
e.preventDefault();
} }
else if (e.key === 'Enter') { else if (e.key === 'Enter') {
e.preventDefault();
if (myGuess.length === 5) { if (myGuess.length === 5) {
client.publish({destination: '/app/practiceGuess', body: myGuess}); client.publish({destination: '/app/practiceGuess', body: myGuess});
myGuess = ''; myGuess = '';
@ -78,17 +79,11 @@ function addKeydownListener() {
// characters // characters
function addKeypressListener() { function addKeypressListener() {
document.addEventListener('keypress', function(e) { document.addEventListener('keypress', function(e) {
let charCode = e.charCode; const key = e.key.toUpperCase();
if (isCharacter(charCode)) { if (myGuess.length < 5 && isCharacter(key)) {
if (isCharacterLowercase(charCode)) { myGuess += key;
charCode = charCode - 32;
}
const char = String.fromCharCode(charCode);
if (myGuess.length < 5) {
myGuess += char;
repaint(); repaint();
} }
}
}); });
} }
@ -181,16 +176,8 @@ function drawHint(xOrigin, yOrigin, progress) {
} }
} }
function isCharacter(charCode) { function isCharacter(key) {
return isCharacterLowercase(charCode) || isCharacterUppercase(charCode); return key >= "A" && key <= "Z";
}
function isCharacterLowercase(charCode) {
return charCode >= 97 && charCode <= 122;
}
function isCharacterUppercase(charCode) {
return charCode >= 65 && charCode <= 90;
} }
function repaint() { function repaint() {