[英]Javascript onkeydown/up
I would like to know how I use onkeydown
and onkeyup
event in javscript correctly with an event listener. 我想知道如何在javscript中通过事件监听器正确使用
onkeydown
和onkeyup
事件。 I want the event to listen when you just press in the website, not in a text field. 我希望该事件在您只按网站而不是文本字段时进行收听。
I don't know exactly how the script would look like but hopefully you understand what I want to do. 我不确切知道脚本的外观,但希望您理解我想要做什么。 I figured the code would look something like this:
我认为代码看起来像这样:
document.addEventListener('keydown', 'keyup', function(event) {
if(event.keyCode == 65){
gas1();
}});
Sorry for being so unclear, trying my best to explain 很抱歉,不清楚,请尽我所能解释
Look an example I prepared in jsfiddle: 看一下我在jsfiddle中准备的示例:
document.addEventListener('keydown', function(event) {
console.log(event.key);
});
Simply add a listener to the whole document and check for the "key" in the event object you receive in the callback 只需在整个文档中添加一个侦听器,然后在回调中收到的事件对象中检查“键”
Try Implementing this 尝试实施
document.onkeydown = function(event) {
if(event.keyCode == 65){
gas1();
}
}
document.onkeyup = function(event) {
if(event.keyCode == 65){
gas1();
}
}
But if you want to call a function gas1() only once on keypress just use keydown only. 但是,如果您只想在按键时调用一次gas1()函数,则仅使用keydown。
You can't aggregate two events on a single document.addEventListener
. 您不能在一个
document.addEventListener
上聚合两个事件。 The first parameter is the name of the event, and the second a listener function called when it's fired. 第一个参数是事件的名称,第二个参数是触发事件时调用的侦听器函数。
Create two handlers, one for keyup
and one for keydown
, or use just keypress
instead for this. 创建两个处理程序,一个用于
keyup
,一个用于keydown
,或者为此仅使用keypress
。
Reference: http://www.w3schools.com/jsref/met_document_addeventlistener.asp 参考: http : //www.w3schools.com/jsref/met_document_addeventlistener.asp
You can use below code for checking KeyUp and KeyDown press on HTML page. 您可以使用以下代码检查HTML页面上的KeyUp和KeyDown按下情况。
Note :Arrow keys are only triggered by onkeydown. 注意 :箭头键仅由onkeydown触发。
$(document).ready(function() { document.onkeydown = function(event) { if(event.keyCode=="38") console.log("Up key pressed"); else if(event.keyCode=="40") console.log("Down key pressed"); }; });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Above code is tested on system working fine. 上面的代码在系统正常工作时进行了测试。
Note :Try testing while your focus should be in Result area to print output on console. 注意 :尝试将焦点放在结果区域中时进行测试,以便在控制台上打印输出。
function defw(){
for(var i =0; i < 10; i+= 1) {
var div = document.createElement('div');
document.getElementsByTagName('section')[0].appendChild(div);
}
}
document.addEventListener('keydown', function(event) {
var ekey = event.keyCode
if( 69 === ekey ) {
defw();
}
});
This example is to make 10 DIVS
calling the function defw()
这个例子是让10个
DIVS
调用函数defw()
1. SAVE the event.keyCode in a variable 1.将event.keyCode保存在变量中
2. THe value 69 is the keyboard letter e in your KEYBOARD 2.值69是键盘上的键盘字母e
HERE IS THE LINK codepen.io 链接在这里codepen.io
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.