var keyEnum = { W_Key:0, A_Key:1, S_Key:2, D_Key:3 };
var keyArray = new Array(4);
function onKeyDown()
{
// Detect which key was pressed
if( key == 'w' )
keyArray[keyEnum.W_Key] = true;
// Repeat for each key you care about...
}
function onKeyUp()
{
// Detect which key was released
if( key == 'w' )
keyArray[keyEnum.W_Key] = false;
// Repeat for each key you care about...
}
function isKeyDown(key)
{
return keyArray[key];
}
window.onmousemove = function (e) {
if (!e) e = window.event;
if (e.shiftKey) {/*shift is down*/}
if (e.altKey) {/*alt is down*/}
if (e.ctrlKey) {/*ctrl is down*/}
if (e.metaKey) {/*cmd is down*/}
}
This are available on all browser generated event objects, such as those from keydown, keyup, and keypress, so you don't have to use mousemove.
/*
Tracks what keys are currently down on the keyboard
*/
function keyboard_module(onUpdate){
var kb = {};
var unicode_mapping = {};
document.onkeydown = function(e){
var unicode=e.charCode? e.charCode : e.keyCode
var key = getKey(unicode);
kb[key] = true;
if(onUpdate){
onUpdate(kb);
}
}
document.onkeyup = function(e){
var unicode=e.charCode? e.charCode : e.keyCode
var key = getKey(unicode);
delete kb[key];
if(onUpdate){
onUpdate(kb);
}
}
function getKey(unicode){
if(unicode_mapping[unicode]){
var key = unicode_mapping[unicode];
}else{
var key= unicode_mapping[unicode] = String.fromCharCode(unicode);
}
return key;
}
return kb;
}
function testing(kb){
console.log('These are the down keys', kb);
}
var keyboard = keyboard_module(testing);
....
//somewhere else in the code
if(keyboard['K']){/*do something special */}
kd.P.down(function () {
console.log('The "P" key is being held down!');
});
kd.P.up(function () {
console.clear();
});
// This update loop is the heartbeat of Keydrown
kd.run(function () {
kd.tick();
});
I've incorporated Keydrown into my client-side JavaScript for a proper pause animation in a Red Light Green Light game I'm writing. You can view the entire game 给你. (Note: If you're reading this in the future, the game should be code complete and playable :-D!)
我扫描了以上的答案,建议的 keydown/keyup方法只在特殊情况下有效。如果用户使用 alt 标签,或者使用一个按键手势打开一个新的浏览器窗口或标签,那么就会注册一个 keydown,这很好,因为在这一点上,不可能知道这个键是网络应用程序正在监视的东西,还是一个标准的浏览器或操作系统快捷方式。回到浏览器页面,它仍然会认为密钥被持有,尽管它在此期间被释放。或者,当用户使用鼠标切换到另一个选项卡或应用程序,然后在页面外释放某个键时,只需保留某个键即可。
window.onkeyup = up;
window.onkeydown = down;
function up(e) {
if (e.key === 'F5') return; // if you want this to work also on reload with F5.
window.onkeyup = null;
window.onkeyup = null;
if (e.key === 'Control') {
alert('Control key was released. You must have held it down while opening the file, so we will now load the file into the editor.');
}
}
function down() {
window.onkeyup = null;
window.onkeyup = null;
}
if (KeyPressing.isKeyPressed(13)){ //Pass the keyCode integer as parameter
console.log('The Enter key is being pressed!')
}else{
console.log('The Enter key is NOT being pressed!')
}
您甚至可以设置一个间隔来检查键是否被按下:
setInterval(() => {
if (KeyPressing.isKeyPressed(13)){
console.log('The Enter key is being pressed!')
}else{
console.log('The Enter key is NOT being pressed!')
}
}, 1000) //Update data every 1000ms (1 second)
KeyboardEvent object describes a single interaction between the user and a key (or combination of a key with modifier keys) on the keyboard
Shift 键 : 是一个布尔值,指示 shift 键是否被按下(true)
KeyboardEvent.ctrlKey: is a boolean value that indicates if the control key was pressed (true) or not (false)
AltKey : 是一个布尔值,指示 alt 键(Option 或 something on macOS)是否按下(true)
例如:
<html>
<head>
<title>shiftKey example</title>
<script type="text/javascript">
function showChar(e){
alert(
"Key Pressed: " + String.fromCharCode(e.charCode) + "\n"
+ "charCode: " + e.charCode + "\n"
+ "SHIFT key pressed: " + e.shiftKey + "\n"
+ "ALT key pressed: " + e.altKey + "\n"
+ "CTRL key pressed: " + e.ctrlKey + "\n"
);
}
</script>
</head>
<body onkeypress="showChar(event);">
<p>Press any character key, with or without holding down
the SHIFT key.<br />
You can also use the SHIFT key together with the ALT key.</p>
</body>
</html>