跨浏览器键盘事件处理程序
我借用了一个按键处理程序,它曾经可以跨浏览器工作,但现在不再在 webkit 浏览器上工作。任何人都可以提供如何修复的建议吗?
这是处理程序:
function handleKeys(e) {
//credit: http://santrajan.blogspot.com/2007/03/cross-browser-keyboard-handler.html
var character;
var evt = (e) ? e : window.event; //IE reports window.event not arg
if (evt.type == "keydown") {
character = evt.keycode;
if (character < 16 || // non printables
(character > 16 && character < 32) || // avoid shift
(character > 32 && character < 41) || // navigation keys
character == 46) { // Delete Key (Add to these if you need)
handleNonChar(character); // function to handle non Characters
nonChar = true;
} else
nonChar = false;
} else { // This is keypress
if (nonChar) return; // Already Handled on keydown
character = (evt.charCode) ?
evt.charCode : evt.keyCode;
if (character > 31 && character < 256) // safari and opera
handleChar(character); //
}
}
处理程序在页面中被调用,如下所示:
<script>
document.onkeydown = function(e) {handleKeys(e)}
document.onkeypress = function(e) {handleKeys(e)}
var nonChar = false;
</script>
谢谢!
蒂姆
I have a keypress handler that I borrowed that used to work across browsers but now no longer works on webkit browsers. Can anyone offer suggestions how to fix?
Here is the handler:
function handleKeys(e) {
//credit: http://santrajan.blogspot.com/2007/03/cross-browser-keyboard-handler.html
var character;
var evt = (e) ? e : window.event; //IE reports window.event not arg
if (evt.type == "keydown") {
character = evt.keycode;
if (character < 16 || // non printables
(character > 16 && character < 32) || // avoid shift
(character > 32 && character < 41) || // navigation keys
character == 46) { // Delete Key (Add to these if you need)
handleNonChar(character); // function to handle non Characters
nonChar = true;
} else
nonChar = false;
} else { // This is keypress
if (nonChar) return; // Already Handled on keydown
character = (evt.charCode) ?
evt.charCode : evt.keyCode;
if (character > 31 && character < 256) // safari and opera
handleChar(character); //
}
}
The handler is called in the pages like this:
<script>
document.onkeydown = function(e) {handleKeys(e)}
document.onkeypress = function(e) {handleKeys(e)}
var nonChar = false;
</script>
Thanks!
Tim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您配对一个 JS 库来规范 charCode/keyCode 差异,并将其与专用于键盘输入的插件结合使用,您应该会很好地理解。
我使用了 jQuery 和 热键插件。但是,您也可以使用快捷键键盘处理,而不使用 jQuery(HotKeys 插件所基于的)在)。
我非常喜欢这些提供的键盘快捷键功能 - 但应该注意的是,某些键盘组合可能会触发(或者用户可能期望它们触发)特定浏览器中的某些操作。
例如,您可以捕获
[CTRL] + [A]
键盘组合,但如果您阻止它选择所有文本 - 我可以看到用户非常恼火。If you pair up a JS Library that will normalize the charCode/keyCode differences, and use it in conjunction with a plugin dedicated to keyboard input you should make out quite well.
I've used jQuery with HotKeys Plugin. However you can also use the shortcut keyboard handling without jQuery (which the HotKeys Plugin is based on).
I like the keyboard shortcut functionality provided by these quite much - however it should be noted that certain keyboard combinations may trigger (or the user may expect them to trigger) certain actions in a particular browser.
E.g. You can capture a
[CTRL] + [A]
keyboard combination, but if you block this from selecting all the text - I can see the user being quite aggravated.