PDA

View Full Version : Shortcut key



peterwong
20 Oct 2009, 12:14 AM
I would like to know how to detect shortcut key (e.g. using KeyBoardListener to detect combination of keys such as ctrl+f) at the webdesktop. Thanks.

sven
20 Oct 2009, 5:01 AM
If you want this application wide i suggest to use the GXT Document class assigned with a KeyNav.

takayser
21 Oct 2009, 6:25 AM
just tried that out, works great. Sample code following (press ctrl+shift+l). Sven, is this the correct way to use it (no overhead and things like this)?


Document.get().el().addEventsSunk(Event.KEYEVENTS);

Document.get().addListener(Events.BrowserEvent, new Listener<ComponentEvent>() {

private boolean ctrl = false;
private boolean shift = false;

public void handleEvent(ComponentEvent be) {
if (be.isShiftKey()) {
shift = be.getEventTypeInt() == Event.ONKEYDOWN || be.getEventTypeInt() == Event.ONKEYPRESS;
}

if (be.isControlKey()) {
ctrl = be.getEventTypeInt() == Event.ONKEYDOWN || be.getEventTypeInt() == Event.ONKEYPRESS;
}

int keyCode = be.getKeyCode();

if (ctrl && shift && be.getEventTypeInt() == Event.ONKEYDOWN) {
if (keyCode == 76) { // 76 == "l"
Info.display("Login/Logout Shortcut", "");
}
}
}
});

sven
21 Oct 2009, 6:26 AM
I would not do it like this, but yes, that works. And yes that approach has some overhead, because keyevents means three events, keydown, keyup, keypress.

takayser
21 Oct 2009, 6:28 AM
:) yah thanks, I know that it works. Why wouldn't you do it like this?

sven
21 Oct 2009, 6:32 AM
Because of the question you asked me. It is too slow ;)



new KeyNav<ComponentEvent>(Document.get()){
public void onKeyPress(ComponentEvent ce){
//dowhateveryouwant
}
};

takayser
21 Oct 2009, 6:37 AM
i see, will try that later. Thx!!

peterwong
21 Oct 2009, 11:39 PM
Thanks for the code!