获取javascript中的按键

Getting keypresses in javascript

本文关键字:javascript 获取      更新时间:2024-07-18

我需要一些帮助来按下键盘。我想建立一个函数来返回当前按下的键。这是怎么做到的?

非常感谢。

是的,使用以下可以做到,甚至可以做得更多

http://www.openjs.com/scripts/events/keyboard_shortcuts/

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="./pages/scripts/jsl.js"></script>
<script type="text/javascript" src="./pages/scripts/common.js"></script>
<script type="text/javascript" src="./pages/scripts/shortcuts_v1.js"></script>
<script type="text/javascript">
function init() {
    shortcut("Shift+F1",function(event) {
        alert("Help Me!");
        event.preventDefault();
    });
    shortcut("Ctrl+S",function(event) {
        alert("Saved!");
        event.preventDefault();
    });
    shortcut("Right",function(event) {
        alert("Right");
        event.preventDefault();     
    });
}
addEvent(window,'load',init);
//ie workaround
document.onfocusout = function(e){
    if( e === undefined ){//ie
        var evt = event;//ie uses event
        if( evt.toElement == null ){//check where focus was lost to
            document.getElementById('status').innerHTML += "document.onfocusout->lost<br>";         
        }
    }
};
//ie workaround
document.onfocusin = function(e){
    if( e === undefined ){//ie
        var evt = event;//ie uses event
        if( evt.toElement == null ){//check where focus was lost to
            document.getElementById('status').innerHTML += "document.onfocusout->gained<br>";       
        }
    }
};
window.onblur = function(e){
 if( e !== undefined ){//ie will have an undefined e here, so this screens them out
     document.getElementById('status').innerHTML += "window.onblur->lost<br>";
 }
};
window.onfocus = function(e){
 if( e !== undefined ){//ie will have an undefined e here, so this screens them out
     document.getElementById('status').innerHTML += "window.onblur->gained<br>";
 }
};
</script>
</head>
<body>
<div id="status"></div>
</body>
</html>

http://www.w3schools.com/tags/ref_keyboardshortcuts.asphttp://www.openjs.com/scripts/events/keyboard_shortcuts/

http://selfcontained.us/2009/09/16/getting-keycode-values-in-javascript/

请参阅这些链接。你是想返回所有的键盘快捷键,还是想返回一些基本功能。。。。??