如何在javascript中通过按键触发时停止重复某个功能? [英] How to stop repeating of a function when triggered by keypress in javascript?

查看:67
本文介绍了如何在javascript中通过按键触发时停止重复某个功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码可以检测到某些按键何时被按下然后触发一个功能,但是在按下任何按键后触发后该功能会重复。我试图在函数结束时重置数组,但这会阻止所有键首先触发该函数。



 var key = []; 
onkeydown = onkeyup = function(e){
e = e ||事件;
map [e.keyCode] = e.type =='keydown';
if(key [17]&& key [16]&& key [65]){// CTRL + SHIFT + A
alert('Enabled');
enableControls();
key [];
返回false;
}
}





如何使用任何按下的键后停止重复的功能功能已经运行?非常感谢任何帮助!



我尝试过:



我试图用key [];重置函数末尾的数组,但这会阻止所有键首先触发该函数。

解决方案

进行2次更改:

1.更改

 map [e.keyCode] 



to

 key [e.keyCode] 



2.更改

 key []; 



to

 key = []; 


I've got a piece of code that detects when certain keys are pressed then triggers a function, but the function repeats after triggered, with any key pressed. I've tried to reset the array at the end of the function, but this stops all keys from triggering the function in the first place.

var key = [];
onkeydown = onkeyup = function(e){
    e = e || event;
    map[e.keyCode] = e.type == 'keydown';
    if(key[17] && key[16] && key[65]){ //CTRL+SHIFT+A
        alert('Enabled');
        enableControls();
        key[];
        return false;
    }
 }



How can I stop the function repeating with any keys pressed after the function has run? Any help is greatly appreciated!

What I have tried:

I've tried to reset the array at the end of the function with "key[];", but this stops all keys from triggering the function in the first place.

解决方案

Make 2 changes:
1. Change

map[e.keyCode]


to

key[e.keyCode]


2. Change

key[];


to

key = [];


这篇关于如何在javascript中通过按键触发时停止重复某个功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆