PDA

View Full Version : How to handle Key Event for combined keys?



babbarkrishan
2 Feb 2010, 2:19 AM
I would like to handle key events specially combined keys like CTRL + ALT + F1 etc. on whole page loaded initially.

Let me know how to perform this? (in GWT 1.7)

Thanks in advance
Krishan Babbar

micgala
2 Feb 2010, 2:37 AM
Hi.

Try this (untested):



textField.addListener(Events.SpecialKey, new Listener<FieldEvent>() {
public void handleEvent(FieldEvent be) {
int F1_KEY = 112;
int keyCode = be.getKeyCode();
if (keyCode == F1_KEY && be.isAltKey() && be.isControlKey()) {
//It will enter here only if F1 + alt + ctrl was hit
}
}
});
Regards,
Michel.

Edited: sorry, that is only for fields, not Panels... not sure how to have a global shortcut...

babbarkrishan
3 Feb 2010, 2:26 AM
Thanks Michel,

but its not working with textfield also.
Below is my code

public void onModuleLoad() {
TextField<String> myTextField = new TextField<String>();
myTextField.addListener(Events.SpecialKey, new Listener<FieldEvent>() {
public void handleEvent(FieldEvent be) {
int F1_KEY = 112;
int keyCode = be.getKeyCode();
//Window.alert("keyCode: " + keyCode);

if (keyCode == F1_KEY && be.isAltKey() && be.isControlKey()) {
//It will enter here only if F1 + alt + ctrl was hit
Window.alert("Krishan " + keyCode);
}
}
});
VerticalPanel vp = new VerticalPanel();
vp.add(myTextField);

RootPanel.get().add(vp);
}

babbarkrishan
3 Feb 2010, 11:23 PM
Thanks Michel,

This is not working even with text fields too. below is my code but its not working:

TextField<String> nameTextField = new TextField<String>();
nameTextField.addListener(Events.SpecialKey, new Listener<FieldEvent>() {
public void handleEvent(FieldEvent be) {
int F1_KEY = 112;
int keyCode = be.getKeyCode();
//Window.alert("keyCode: " + keyCode);

if (keyCode == F1_KEY && be.isAltKey() && be.isControlKey()) {
//It will enter here only if F1 + alt + ctrl was hit
Window.alert("Krishan " + keyCode);
}
}
});
VerticalPanel vp = new VerticalPanel();
vp.add(nameTextField);

RootPanel.get().add(vp);


Please help.

Thanks