How to use KeyboardListener and ClickListener (CTRL+ click or SHIFT+click)

143 views
Skip to first unread message

kyoune

unread,
Jul 24, 2007, 10:33:20 PM7/24/07
to Google Web Toolkit
Dear All,
Im quite new to this GWT.But I want to do something like when I press
the CTRL key, then I can select multiple tree items. If I press SHIFT
then do selection, it willl select the whole item in the middle.
My problem is I cant even detect if I press on CTRL key. However, if i
press on other key like Tab, it can detect.

Is anyone have any idea on how to do this?

My idea is like following:
//define tree
Tree tree = new Tree();
TreeItem root = new TreeItem("root");
tree.addItem(root);

TreeItem one= new TreeItem("1");
root.addITem(one);

TreeItem two= new TreeItem("2");
root.addItem(two);

TreeItem six= new TreeItem("6");
root.addItem(six);

TreeItem one_1= new TreeItem("11");
one.addItem(one_1);

TreeItem one_1_1= new TreeItem("111");
one_1.addItem(one_1_1);

TreeItem one_1_1_1= new TreeItem("1111");
one_1_1.addITem(one_1_1_1);

//add keyboardlistener
tree.addKeyboardListener(new KeyboardListener(){
public void onKeyDown(Widget sender, char keyCode, int modifiers) {
// TODO Auto-generated method stub
}

public void onKeyPress(Widget sender, char keyCode, int modifiers) {
if(Character.isDigit(keycode)){
int key = (int) keycode;

//I've been trying to use KEY_CTRL and
MODIFIER_CTRL but both didnt work.
if(key==KeyboardListener.KEY_CTRL ||
key==KeyboardListener.MODIFIER_CTRL || key==KeyboardListener.KEY_SHIFT
|| key==KeyboardListener.MODIFIER_SHIFT){

}
}

public void onKeyUp(Widget sender, char keyCode, int modifiers) {
// TODO Auto-generated method stub
}
});

kyoune

unread,
Jul 24, 2007, 10:36:29 PM7/24/07
to Google Web Toolkit

/*---------------------------------- PROBLEM -
START---------------------------------*/


public void onKeyPress(Widget sender, char keyCode, int modifiers) {
if(Character.isDigit(keycode)){
int key = (int) keycode;

//I've been trying to use KEY_CTRL and
MODIFIER_CTRL but both didnt work.
if(key==KeyboardListener.KEY_CTRL
|| key==KeyboardListener.MODIFIER_CTRL
|| key==KeyboardListener.KEY_SHIFT
|| key==KeyboardListener.MODIFIER_SHIFT){

tree.addClickListener(new ClickListener(){
//get the Id of the selected tree...
});
}
}
}

/*---------------------------------- PROBLEM - END
---------------------------------*/


public void onKeyUp(Widget sender, char keyCode, int modifiers) {
// TODO Auto-generated method stub
}
});


Thank you in advance.

Crazor

unread,
Jul 25, 2007, 3:37:39 AM7/25/07
to Google Web Toolkit
My guess is that you won't get any modifier keys by onKeyPress,
because they are no key presses. For shortcuts like ctr+e, you would
get an "e pressed" and would take a look at the modifiers supplied to
your handler. Just guessin', though.

As for your problem: Looking at the documentation, I found
http://code.google.com/webtoolkit/documentation/com.google.gwt.user.client.DOM.html#eventGetCtrlKey(com.google.gwt.user.client.Event)
This will get you the depressed state of control given an event.
So maybe try extending Tree and overriding the onBrowserEvent method
and inspect the browser event yourself, looking for modifier keys and
firing new event handlers.

krispy

unread,
Jul 25, 2007, 10:22:59 AM7/25/07
to Google Web Toolkit
kyoune,

Listen for the onKeyUp and onKeyDown events - check the character like
you do in onKeyPress.

public void onKeyDown(Widget w, char key, int mods)
{
if(key == KeyboardListener.KEY_CTRL) enableMultSelect();
}

public void onKeyUp(Widget w, char key, int mods)
{
if(key == KeyboardListener.KEY_CTRL) disableMultSelect();
}

Have a ClickListener wait for click events, then when you get a click
event, check to see if the multiSelect property is enabled/disabled
and do your work from there. If you're still not getting keyboard
events on the tree, then maybe the tree doesn't have keyboard focus.
You can try to get focus on the element or you can look into the
EventPreview class and use DOM.addEventPreview() - you'll get all
keyboard events in the application that way (because you'll see them
at the topmost level before they're propagated). HTH!

-krispy

Reply all
Reply to author
Forward
0 new messages