Is there a way to determine whether the user is using a text-entering control?
I want to add keyboard shortcuts to my HTML + GWT app, but I don't want the actions to be triggered if they're typing in a text area or using the keyboard to operate a select menu.
Is there a way to check whether an element is actively consuming keyboard events, so that I can avoid doing so? Gmail accomplishes this nicely, but perhaps not using GWT.
One solution might be to add an event handler to every single control on my app and call stopPropogation on the key press events that are generated there, but that would endanger any other eventhandlers that I do want to fire while the user is typing.
Update: restatement of problem:
开发者_高级运维- I want to attach a KeyPressHandler to the dom that responds to key presses to navigate around the site. "Hit h to open help" or "Press m to go to the menu," that kind of thing. But I don't want this KeyPressHandler to activate if the user is just typing in a textbox, right? I wouldn't want to navigate away from the thing they're typing. So, I need some way of filtering out those events, from any place in my app where a user might be typing.
Here is an alternative. It checks if the initial target element was textarea
or input
and when that is the case it will not trigger the help. For any other element it will fire the help and cancel the event.
Event.addNativePreviewHandler(new NativePreviewHandler() {
@Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
if (event.getTypeInt() == Event.ONKEYPRESS) {
final String tn = Element.as(event.getNativeEvent().getEventTarget()).getTagName();
if (!"textarea".equalsIgnoreCase(tn) && !"input".equalsIgnoreCase(tn)) {
event.cancel(); // this will prevent the event from being fired.
// add your help handling here
if (event.getNativeEvent().getCharCode() == 'h' || event.getNativeEvent().getCharCode() == 'H')
Window.alert("help");
}
}
}
});
I have found one solution that is a little bit janky:
Event.addNativePreviewHandler(new NativePreviewHandler()
{
@Override
public void onPreviewNativeEvent(NativePreviewEvent event)
{
if (event.getTypeInt() == Event.ONKEYPRESS && (event.getNativeEvent().getCharCode() == 'h' || event.getNativeEvent().getCharCode() == 'H'))
{
String targetType = event.getNativeEvent().getEventTarget().toString();
if (targetType.contains("HTMLHtmlElement"))
{
helpTab.simulateClick();
}
}
}
});
This seems bad because:
- Every single event goes through this handler
- I'm relying on debug strings that don't seem very stable. Is there something more like
target instanceof HtmlElement
available?
I'll use this answer for now, but I'll be happy to award the checkmark to a better solution.
AFAIK there is no direct way to check if element has focus. But you can install FocusHandler that is triggered when element receives focus:
textBox.addFocusHandler(new FocusHandler() {
@Override
public void onFocus(FocusEvent event) {
// your element is now focused
}
});
Just disable your keypresshandler for a text input onFocus. Reenable onBlur. See documentation for FocusHandler and BlurHandler.
精彩评论