Adding Event Handlers on the body element...

230 views
Skip to first unread message

seth....@gmail.com

unread,
Aug 6, 2010, 8:45:57 PM8/6/10
to Google Web Toolkit
Hello,

I'm using GWT for 2 years now and i did many great things with it.
However, i'm still looking for how to implements KeyboardHandlers on
the BodyElement.
The only way i found is writing something like this in the HTML file :

<body onkeypress="return keyPress(event);" onkeydown="return
keyDown(event);" onkeyup="return keyUp(event);">

And something like that in the EntryPoint

public void onModuleLoad() {
// publish();
}
private native final void publish() /*-{
$wnd["keyPress"] = function(event) {
@webapp.client.Application::keyPress(Lcom/google/gwt/dom/client/
NativeEvent;)(event);
}
$wnd["keyUp"] = function(event) {
@webapp.client.Application::keyUp(Lcom/google/gwt/dom/client/
NativeEvent;)(event);
}
$wnd["keyDown"] = function(event) {
@webapp.client.Application::keyDown(Lcom/google/gwt/dom/client/
NativeEvent;)(event);
}
}-*/;

But i'm really not satisfied with that solution.
Any ideas please ?

Carlos Aguayo

unread,
Sep 9, 2010, 7:11:55 PM9/9/10
to Google Web Toolkit
You can try something like this:

Event.addNativePreviewHandler(new NativePreviewHandler() {
@Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
int KEY_F8 = 119;
if ("keyup".equals(event.getNativeEvent().getType()) &&
event.getNativeEvent().getKeyCode() == KEY_F8) {
// do something onkeyup && f8
}
}
});


On Aug 6, 8:45 pm, "seth.ho...@gmail.com" <seth.ho...@gmail.com>
wrote:
> Hello,
>
> I'm using GWT for 2 years now and i did many great things with it.
> However, i'm still looking for how to implements KeyboardHandlers on
> the BodyElement.
> The only way i found is writing something like this in the HTML file :
>
>   <bodyonkeypress="return keyPress(event);" onkeydown="returnkeyDown(event);" onkeyup="return keyUp(event);">

lineman78

unread,
Sep 9, 2010, 8:33:52 PM9/9/10
to Google Web Toolkit
Similarly you should probably also check the type. and you could
emulate the normal handler style of the rest of the GWT architecture.

Event.addNativePreviewHandler(new NativePreviewHandler()
{
@Override
public void onPreviewNativeEvent(NativePreviewEvent event)
{
NativeEvent ne = event.getNativeEvent();
if
(KeyDownEvent.getType().getName().equals(ne.getType()))
System.out.println("Yipeee!");
}
});
Reply all
Reply to author
Forward
0 new messages