[英]How can I change background colour of a button on key press?
I've been working on JavaScript on-screen keyboard kind of thing, just for experimentation.我一直在研究 JavaScript 屏幕键盘之类的东西,只是为了实验。 I wanted to see if I could detect which key had been pressed, and make the corresponding on-screen keyboard button change colour, similar to many online touch typing courses have.
我想看看我是否可以检测到按下了哪个键,并使相应的屏幕键盘按钮改变颜色,类似于许多在线触摸打字课程。 I have tried many variations of the
onkeydown
command, but no luck.我尝试了
onkeydown
命令的许多变体,但没有成功。
//doesn't seem to do anything.
document.getElementById("a").style.backgroundColor="#004f40";
Button's id is simply it's value eg/ the A key is id="a"
.按钮的 id 只是它的值,例如/ A 键是
id="a"
。
Could anyone give me any ideas on how to do this?谁能给我任何关于如何做到这一点的想法?
Here is an example that sets the color first in CSS and uses javascript addEventListener to listen for a click event and changes the color of the button when clicked, it also removes the attached event listener.这是一个示例,它首先在 CSS 中设置颜色并使用 javascript addEventListener来侦听单击事件并在单击时更改按钮的颜色,它还删除了附加的事件侦听器。
CSS CSS
#a {
background-color: yellow;
}
HTML HTML
<button id="a">My Button</div>
Javascript Javascript
document.getElementById("a").addEventListener("click", function onClick() {
this.removeEventListener("click", onClick);
this.style.backgroundColor = "#004f40";
}, false);
This example uses the mouse click event, but you will need to look at key events instead of a mouse one, it could be one of many;此示例使用鼠标单击事件,但您需要查看按键事件而不是鼠标事件,它可能是多个事件之一; eg keydown , keypress , or keyup .
例如keydown 、 keypress或keyup 。
Update : Here is one possible solution using key events.更新:这是使用关键事件的一种可能的解决方案。
CSS CSS
button {
background-color: yellow;
}
Javascript Javascript
var start = 97,
end = 122,
button;
while (start <= end) {
button = document.createElement("button");
button.id = button.textContent = String.fromCharCode(start);
document.body.appendChild(button);
start += 1;
}
document.addEventListener("keypress", function onKeypress(evt) {
var element = document.getElementById(String.fromCharCode(evt.charCode || evt.char));
if (element) {
document.addEventListener("keyup", function onKeyup() {
document.removeEventListener("keyup", onKeyup);
element.style.backgroundColor = "yellow";
}, false);
element.style.backgroundColor = "#004f40";
}
}, false);
Note: this example is by no means perfect, it it just an example of how to use events.注意:这个例子并不完美,它只是一个如何使用事件的例子。
Update : here is another example that uses all 3 events to de-bounce the keyboard when multiple keys are pressed and released.更新:这是另一个例子,当按下和释放多个键时,它使用所有 3 个事件来消除键盘的弹跳。 (Compare it in use with above.)
(将其与上面的使用进行比较。)
CSS CSS
button {
background-color: yellow;
}
button:active {
background-color: #004f40;
}
Javascript Javascript
var start = 97,
end = 122,
button;
while (start <= end) {
button = document.createElement("button");
button.id = button.textContent = String.fromCharCode(start);
document.body.appendChild(button);
start += 1;
}
var keydown,
keypress = [];
document.addEventListener("keydown", function onKeydown(e1) {
keydown = e1;
}, false);
document.addEventListener("keypress", function onKeypress(e2) {
var record = {
"char": e2.char || e2.charCode,
"key": keydown.key || keydown.keyCode || keyDown.which,
"shiftKey": keydown.shiftKey,
"metaKey": keydown.metaKey,
"altKey": keydown.altKey,
"ctrlKey": keydown.ctrlKey
},
element = document.getElementById(String.fromCharCode(e2.charCode || e2.char));
if (element) {
element.style.backgroundColor = "#004f40";
keypress.push(record);
}
}, false);
document.addEventListener("keyup", function onKeyup(e3) {
var key = e3.key || e3.keyCode || e3.which;
keypress.forEach(function (record) {
if (record.key === key && record.shiftKey === e3.shiftKey && record.metaKey === e3.metaKey && record.altKey === e3.altKey && record.ctrlKey === e3.ctrlKey) {
document.getElementById(String.fromCharCode(record.char)).style.backgroundColor = "yellow";
}
});
}, false);
Note: even this is not perfect as it depends on millisecond timing to match keydown and keypress events.注意:即使这也不是完美的,因为它依赖于毫秒时间来匹配 keydown 和 keypress 事件。
Or alternatively, you could use jQuery :或者,您可以使用jQuery :
$(".keyboardButton").mousedown(function(){
$(this).css("background":"#Color-when-pressed");
}
$(".keyboardButton").mouseup(function(){
$(this).css("background":"#Color-when-released");
}
Of course, replace the colors respectively.当然,分别更换颜色。
Get jQuery获取 jQuery
Or pure CSS:或纯 CSS:
.keyboardButton:active {
background:#Color-when-pressed;
}
.keyboardButton {
background:#Color-when-released;
}
It might be the best, since you won't have to write code for every single button you have, and you probably already have CSS classes for them.这可能是最好的,因为您不必为您拥有的每个按钮编写代码,而且您可能已经为它们准备了 CSS 类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.