Re: Is it possible to stop the browser from interpreting control-key presses?
First I'll tell you the last thing you want to hear: you shouldn't
override the browser's built-in shortcuts, etc etc.
Alright, now that that's out of the way: yes you can. Here's a snippet
of code I used:
private static HandlerRegistration keyboardShortcuts;
public static void enableKeyShorcuts() {
EntryPoint.sinkEvents(Event.ONKEYDOWN); // "EntryPoint" here is my,
well, entry point keyboardShortcuts =
Event.addNativePreviewHandler(new NativePreviewHandler() { public
void onPreviewNativeEvent(NativePreviewEvent event) { if
(Event.ONKEYDOWN == event.getTypeInt() &&
event.getNativeEvent().getCtrlKey() &&
event.getNativeEvent().getShiftKey()) { // you don't want this if
you just want to capture Ctrl-? keys
GWT.log("KeyboardShortcutManager event.cancel()");
event.cancel(); int keyCode =
event.getNativeEvent().getKeyCode();
EventRouter.routeEvent(keyCode); // this does something for each key
combination that I care about } } });}
/*** Disables key shortcuts*/public static void disableKeyShorcuts()
{ EntryPoint.unsinkEvents(Event.ONKEYDOWN); if (null!
=keyboardShortcuts) { keyboardShortcuts.removeHandler(); }}
Note that this code cancels all Ctrl-Shift-SomeKey events, regardless
of whether I actually do something with the specific key combination I
catch. A better version would pass the event to the event router where
it would only cancel those events for which I want to override the
native behavior.
Vid
On Nov 10, 10:09 am, kstokes <dada...@gmail.com> wrote:
> I am working on an application which needs control of the keyboard so
> the user can enter control-keys such as control-j and control-w.
>
> In order to be able to stop the browser from interpreting keys like
> backspace, I used
>
> Event.addNativePreviewHandler()
>
> To redirect all key up, key down, and keypress events to my handler.
> I use NativeEvent.preventDefault() and NativePreviewEvent.consume() to
> keep the event from being passed on.
>
> In general, I followed the example from the gwt doc pages:http://code.google.com/webtoolkit/doc/latest/DevGuideUiDom.html
>
> Now keys like backspace and tab are successfully redirected to my
> handler, and the browser does not interpret them.
>
> However, control keys such as Control-J, Control-W, Control-R
> still are interpreted by the browser instead of my handler.
>
> Is it possible to make a GWT app which can capture these also?
--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home