Replace charCode with key
This commit is contained in:
parent
a38c19b9c9
commit
5d769c3989
@ -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,16 +225,10 @@ 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;
|
this.repaint();
|
||||||
}
|
|
||||||
const char = String.fromCharCode(charCode);
|
|
||||||
if (this.myGuess.length < this.wordLength) {
|
|
||||||
this.myGuess += char;
|
|
||||||
this.repaint();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onChatKeypress: function(e) {
|
onChatKeypress: function(e) {
|
||||||
@ -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) {
|
||||||
|
@ -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,16 +79,10 @@ 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;
|
repaint();
|
||||||
}
|
|
||||||
const char = String.fromCharCode(charCode);
|
|
||||||
if (myGuess.length < 5) {
|
|
||||||
myGuess += char;
|
|
||||||
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() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user