javascript 中特殊键(箭头等)的跨浏览器按键

发布于 2024-12-08 19:14:37 字数 516 浏览 0 评论 0原文

我正在构建一个终端的浏览器界面。我需要捕获字符(字母数字、点、斜杠……)和非字符按键(箭头、F1-F12……)。另外,如果用户按住某个键,那么重复按键会很好(应该重复调用该函数,直到释放键为止)。空格键、字符也是如此...

我希望它尽可能跨浏览器(jQuery keypress 在该帐户上失败)。我也尝试过使用 jquery.hotkeys.js 的分支,但如果我理解正确,我可以不能在单个函数中同时捕获特殊键和字符键(前者应使用 keydown,后者应使用 keydown)。

是否有一个 JS 库可以让我捕获字符和特殊键?

我希望我没有遗漏一些明显的东西。 :)

更新 澄清一下:我正在寻找可以向我隐藏浏览器实现细节的库。

I am building a browser interface to a terminal. I need to catch both character (alphanumeric, dot, slash,...) and non-character key presses (arrows, F1-F12,...). Also, if the user holds some key down, it would be nice to get repeated keypresses (the function should be called repeatedly until key is released). The same goes for space key, characters,...

I want this to be as cross-browser as possible (jQuery keypress fails on that account). I have also tried using fork of jquery.hotkeys.js, but if I understand correctly, I can't catch both special and character keys in a single function (one should use keydown for former and keydown for the latter).

Is there a JS library that would allow me to catch both character and special keys?

I hope I'm not missing something obvious. :)

UPDATE To clarify: I am looking for the library that would hide the browser implementation details from me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪操控生活 2024-12-15 19:14:37

onkeydown 正是您所需要的。它捕获所有按键,即使您按住一个按钮,它也会重复触发。

<input type='text' onkeydown='return myFunc(this,event)'>

<script>
   function myFunc(Sender,e){
     var key = e.which ? e.which : e.keyCode;

     if(key == someKey){ // cancel event and do something
        ev.returnValue = false;
        if(e.preventDefault) e.preventDefault();
        return false;
     }
   }
</script>

更新尝试使用 jQuery 进行测试

$(document).ready(function(){
$('#in').keydown(fn);
});

var cnt = 0;
function fn(e){
   var key = e.keyCode ? e.keyCode : e.which;
   cnt++;

   if(cnt == 10) {
  alert('event was fired 10 times. Last time with key: '+key);
      cnt = 0;
   }
}

The onkeydown it exactly what you need. It captures all keys, even if you are holding a button it is fired repeatedly.

<input type='text' onkeydown='return myFunc(this,event)'>

<script>
   function myFunc(Sender,e){
     var key = e.which ? e.which : e.keyCode;

     if(key == someKey){ // cancel event and do something
        ev.returnValue = false;
        if(e.preventDefault) e.preventDefault();
        return false;
     }
   }
</script>

UPDATE try and test this with jQuery

$(document).ready(function(){
$('#in').keydown(fn);
});

var cnt = 0;
function fn(e){
   var key = e.keyCode ? e.keyCode : e.which;
   cnt++;

   if(cnt == 10) {
  alert('event was fired 10 times. Last time with key: '+key);
      cnt = 0;
   }
}
网白 2024-12-15 19:14:37

DOM 3 事件规范包括按键事件,但是它仍然是一个工作草案,因此可能尚未得到广泛支持,但应该非常有帮助。

要将按键代码转换为字符,您可能会发现 Quriskmode 很有帮助。知道按下了哪个键以及哪些修饰符可以让您到达您想要的位置。请注意,将所有键盘映射到相同字符集时可能会遇到问题,因为有些键盘具有其他键盘没有的键(例如 Microsoft“windows”键和 Apple 命令键)。可能需要进行一些尝试和错误。

哦,您可能会发现这篇文章 JavaScript 疯狂:键盘事件 很有趣。

The DOM 3 Events spec includes key events, but it's still a working draft so likely not that widely supported yet but should be pretty helpful.

For turning key codes into characters, you might find Quriskmode helpful. Knowing which key was pressed and which modifiers should get you where you want to be. Note that you may have issues mapping all keyboards to the same character sets because some have keys that others don't (e.g. Microsoft "windows" key and Apple command key). A bit of trial and error might be required.

Oh, and you might find the article JavaScript Madness: Keyboard Events interesting.

泡沫很甜 2024-12-15 19:14:37

我最终使用了 keycode.js,但正在围绕 keydown、keypress 构建一个完整的事件管理系统和 keyup 事件,因为如果没有对应的字符,仅其中一个事件(keydown)不足以确定输入了哪个字符以及按下了哪个键。浏览器不兼容是这一挑战的额外好处。 :)

谢谢你们的回答,它帮助我充分理解了问题。

I ended up using keycode.js, but am building a whole event-managing system around keydown, keypress and keyup events, because just one of the events (keydown) is not enough to determine which character was entered and which key was pressed if there is no corresponding character. Browser incompatibilities are an added bonus to this challenge. :)

Thank you both for your answers, it has helped me understand the problem fully.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文