【发布时间】:2011-10-18 00:30:38
【问题描述】:
我试图弄清楚用户何时使用 JavaScript 在 HTML 页面中按下 control 键。
在下面的代码中,“CTRL UP”将按预期出现,但“CTRL DOWN”只有在我也按下另一个键时才会出现,例如移位.
<html>
<head>
<script type="text/javascript">
window.addEventListener("keyup", function(event) {
// Bind to both command (for Mac) and control (for Win/Linux)
if (event.ctrlKey) {
console.log("CTRL UP");
}
}, false);
window.addEventListener("keydown", function(event) {
// Bind to both command (for Mac) and control (for Win/Linux)
if (event.ctrlKey) {
console.log("CTRL DOWN");
}
}, false);
</script>
</head>
<body>
Ctrl Demo
</body>
</html>
当 ctrl 被自己按下并按住时,有什么方法可以让“CTRL DOWN”键事件按预期工作?
注意:我正在尝试让它在 Google Chrome 扩展程序中工作,因此使用 Chrome 特定的 API 或技巧来完成这项工作是完全可以的。我在 Ubuntu 上使用 Google Chrome 15.0.874.83 beta。
【问题讨论】:
-
如果有一些程序正在运行,在 OS 级别监听这个键,那么它不会在 js 中触发。
标签: javascript google-chrome google-chrome-extension dom-events