简体   繁体   English

捕获Web应用程序的所有关键事件的最佳方法?

[英]Best way to capture all key events for a web application?

I'm a litle distraught at the current state of key capturing for web applications. 对于Web应用程序的关键捕获当前状态,我感到非常愤怒。 It works great as long as you know your user is going to be typing in a specific place (eg an input field), but as soon as you want to do global shortcuts for an entire "application", it seems to fall apart. 只要您知道您的用户将要在特定位置(例如input字段)中键入内容,它就会很有效,但只要您想要为整个“应用程序”执行全局快捷方式,它就会崩溃。

I'm trying to find out if there is a better way to capture all the key events for a web page than the method I am currently using. 我试图找出是否有更好的方法来捕获网页的所有关键事件,而不是我目前使用的方法。

My current method is to use the JQuery Hotkeys plugin, bound to the document element, ie: 我当前的方法是使用绑定到document元素的JQuery Hotkeys插件,即:

$(document).bind("keyup", "delete", function() {});

That works great for most purposes, but for example on Firefox, if the user happens to absentmindedly move their mouse over the navigation bar, the delete key will sometimes result in the user going "back", and the key is never received by the handler so that I can stop propagation. 这对于大多数用途都很有用,但是例如在Firefox上,如果用户碰巧心不在焉地将鼠标移到导航栏上,则删除键有时会导致用户“返回”,并且处理程序从未接收到密钥这样我就可以停止传播了。

Is there a different element I should be binding to? 我应该绑定一个不同的元素吗? Is there a better plugin out there for this? 那里有更好的插件吗? Should I just avoid using any keys that are bound to things in common web browsers? 我应该避免使用常见Web浏览器中绑定的任何键吗?

As more and more web applications look to mimic their desktop counterparts, it seems like this is a basic feature that web developers will increasingly require. 随着越来越多的Web应用程序看起来模仿他们的桌面版本,这似乎是Web开发人员越来越需要的基本功能。

EDIT: I should point out that I am already using e.stopPropagation() and e.preventDefault() . 编辑:我应该指出我已经在使用e.stopPropagation()e.preventDefault() The main problem seems to be that sometimes the event is never even passed to the bound function. 主要问题似乎是有时事件甚至从未传递给绑定函数。 I am basically wondering if anyone has figured out a "higher" element to bind to other than document . 我基本上想知道是否有人想出一个“更高”的元素绑定到document以外的其他元素。 Or is there an alternative I have never even thought of? 还是有一种我从未想过的替代方案? Embedding an invisible Flash element on the page and then passing all keys from that to Javascript, for example (I don't think this would work). 例如,在页面上嵌入一个不可见的Flash元素,然后将所有键从那里传递给Javascript(我认为这不会起作用)。

I think, at this point, I am doing things the "standard, well-known way." 我认为,在这一点上,我做的事情是“标准的,众所周知的方式”。 I am trying to see if there is an outside-the-box way that isn't widely known that maybe someone on SO knows about :-). 我试图看看是否存在一种尚未被广泛知晓的开箱即用的方式,也许有人在SO上知道:-)。

If you are making a sophisticated web-app with customized keyboard controls, the first thing you should do is alert the user that you are making a sophisticated web-app with customized keyboard controls. 如果您正在使用自定义键盘控件制作复杂的Web应用程序,那么您应该做的第一件事是提醒用户您正在使用自定义键盘控件制作复杂的Web应用程序。 After that, tell them what the controls are and what they do. 之后, 告诉他们控件是什么以及他们做了什么。

Binding the keypress and keydown listeners to the document is the correct way to do it, but you have to remember to preventDefault and/or stopPropogation for keypresses that you want to override. keypresskeydown侦听器绑定到document是正确的方法,但是您必须记住要为要覆盖的按键preventDefault和/或stopPropogation Even if there is no default behavior, you will need to prevent them from cascading in case the user has rebound their default keyboard shortcuts. 即使没有默认行为,您也需要防止它们级联,以防用户反弹其默认键盘快捷键。

Also, you will only be able to receive keyboard input when the page has focus. 此外,您只能在页面具有焦点时接收键盘输入。

When you say Delete I assume you mean the Backspace key as Delete generally referrs to the key next to Insert , Home , End , Page Up and Page Down . 当你说删除我假设你的意思是退格键,因为删除通常是指插入主页结束向上翻页向下 翻页旁边的键。

Edit to add: 编辑添加:

Be very careful about which keys you choose to override. 要非常小心你选择覆盖哪些键。 If you're making an app to be used by people other than yourself, you have to worry about usability and accessibility. 如果您要将应用程序供自己以外的人使用,则必须担心可用性和可访问性。 Overriding the Tab , Space and Enter keys is risky, especially for people using screen-readers. 覆盖TabSpaceEnter键是有风险的,特别是对于使用屏幕阅读器的人。 Make sure to test the site blind and fix any issues that may arise with traversing the page via the keyboard. 确保测试网站盲点并修复通过键盘遍历页面可能出现的任何问题。

maybe you can use html-attribute ACCESSKEY and react onfocus. 也许你可以使用html属性ACCESSKEY并对onfocus做出反应。 ie: 即:

<input type="text" size="40" value="somefield" accesskey="F">

i think u might need to add a tabindex to tags like <div> 我想你可能需要在像<div>这样的标签上添加一个tabindex

<div id="foo" tabindex="1" accesskey="F">

You can't bind to events that happen where you have no control - eg the window chrome. 您无法绑定到无法控制的事件 - 例如窗口镶边。 The way most webapps deal with this is asking the user to confirm their decision to leave the page, using the onbeforeunload event: 大多数webapps处理此问题的方式是要求用户使用onbeforeunload事件确认他们离开页面的决定:

window.onbeforeunload = function (e) {
    var str = 'Are you sure you want to leave this page?';
    e = e || window.event;

    if (userHasSomeUnsavedWork) {
        e.returnValue = str;
        return str;
    }
}

绝对没有经过测试但是......试试'window'元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM