document.onkeyup = function(event) {
//ctrl+r to refresh page, but also to set lettersGuessed by player to ''.
//the 'r' was being recorded as a guess.
if (event.keyCode == 73 && event.ctrlKey) {
startGame();
}
//allows only A-Z on keyboard to be registered as answers
else if (event.keyCode >= 65 && event.keyCode <= 90) {
var key = event.key;
console.log(computerChoice);
if (guesses != 0) {
lettersGuessed += key + ', ';
if (key == computerChoice) {
wins++;
display.innerHTML = "You WIN! The computer chose " + computerChoice +
"<br/> To play again press any button.";
startGame();
}
else if (key != computerChoice && guesses != 0) {
guesses--;
displayResults('Keep Guessing!', key, 'Not Telling!' , guesses);
}
}
else if (guesses == 0) {
losses++;
display.innerHTML = "You LOSE! The computer chose: " + computerChoice +
"<br/> To play again press any button.";
startGame();
}
}
//this runs when ctrl+r is hit. shouldn't this not run? for some reason its reading the ctrl part
//when I press ctrl+r.
else {
alert("Only A-Z allowed.");
}
}
此功能是运行我的通灵游戏的主要代码。玩家有10个生命来猜测随机字母。在if语句中我设置了ctrl + r的键代码来设置播放器字母Guessed ='',我知道这也是刷新页面而我不介意。我必须这样做是因为我的其他if语句将'r'记录为来自用户的猜测。但是,现在按下ctrl会激活底部的else语句,提示“只允许A-Z”。
由于我的初始if语句,底部的else语句不应该运行吗?在警报响起之前,我正在给ctrl + r做些什么。
我正在尝试做的是当ctrl + r被击中时没有专门发出警报。
答案 0 :(得分:0)
在浏览器中按ctrl + r
默认值进行页面重新加载。要进行完整性检查,请尝试keyCode
之类的任何其他i
。
我也建议使用event.which
作为后备选项:
event.keyCode || event.which == 73 && event.ctrlKey