View Full Version : Is it possible to capture the event when the user presses the enter key
nagate
10-19-2008, 06:09 PM
In GWT there's a way to capture the event when the user presses the enter key.
TextBox.addKeyboardListener(new KeyboardListener() {
public void onKeyPress(Widget sender, char keyCode, int modifiers) {
if (keyCode == KeyboardListener.KEY_ENTER)
//do stuff
}
});
I am using GXT's TextField class and can't seem to find a way to capture the enter key event if I add the KeyListener.
Thanks.
gslender
10-19-2008, 08:36 PM
try this...
KeyListener keyListener = new KeyListener() {
public void componentKeyUp(ComponentEvent event) {
if (event.getKeyCode() == 13 && loginBtn.isEnabled()) {
onSubmit();
}
// System.out.println(event.getKeyCode());
validate();
}
};
userTxtFld = new TextField<String>();
userTxtFld.setMinLength(1);
userTxtFld.setFieldLabel("Username");
userTxtFld.addKeyListener(keyListener);
nagate
10-20-2008, 01:27 PM
That worked, thanks.
c.mallwitz
11-17-2008, 08:47 AM
KeyListener keyListener = new KeyListener() {
public void componentKeyUp(ComponentEvent event) {
if (event.getKeyCode() == 13 && loginBtn.isEnabled()) {
onSubmit();
}
// System.out.println(event.getKeyCode());
validate();
}
};
userTxtFld = new TextField<String>();
userTxtFld.setMinLength(1);
userTxtFld.setFieldLabel("Username");
userTxtFld.addKeyListener(keyListener);
This is working for me but I have a bunch of FormPanels with a few Fields each. Adding a key listener to all of them is a bit tedious and I therefore was trying to add a KeyEvent listener to the form itself. But this doesn't seem to work. Any ideas? Here is my code:
public class SubmitOnEnterFormListener implements Listener<KeyEvent>
{
private FormPanel form;
private SubmitOnEnterFormListener(FormPanel form)
{
this.form = form;
}
public void handleEvent(KeyEvent event)
{
if (event.getKeyCode() == KeyboardListener.KEY_ENTER)
{
form.submit();
}
}
public static void attach(FormPanel form)
{
SubmitOnEnterFormListener l = new SubmitOnEnterFormListener(form);
form.addListener(Events.KeyPress, l);
}
}
and
SubmitOnEnterFormListener.attach(formPanel);
EagleEye666666
11-17-2008, 09:14 AM
This is working for me but I have a bunch of FormPanels with a few Fields each. Adding a key listener to all of them is a bit tedious and I therefore was trying to add a KeyEvent listener to the form itself. But this doesn't seem to work. Any ideas? Here is my
so maybe add the listener to the root component where you are adding your FormPanels?
I did like this for a LoginDialog, I don't know how it is looking for the other components.
With Dialog it was easy going, cuz it already offers a method which i just had to override...
protected void onKeyPress(WindowEvent we) {
if (we.getKeyCode()==13) {
if (signInForm.isValid())
onSubmit();
}
}
protected void onSubmit(){
signInForm.submit();
other forms....
}
I think basicly it should be possible to add a Keylistener to the root component... where u are adding your FormPanels...iam not sure
c.mallwitz
11-17-2008, 11:07 AM
With Dialog it was easy going, cuz it already offers a method which i just had to override...
protected void onKeyPress(WindowEvent we) {
if (we.getKeyCode()==13) {
if (signInForm.isValid())
onSubmit();
}
}
protected void onSubmit(){
signInForm.submit();
other forms....
}
I had a look at Dialog (and Window) and I can't even workout who is firing onKeyPress() in order for your code to work. My FormPanels are sitting in all kinds of LayoutContainers but not Dialog (or Window).
One approach I could see is creating some kind of helper that gets the children of a (completely initialized) FormPanel and adds an appropriate KeyListener to all of them but it seems a bit akward...
Christian
EagleEye666666
11-17-2008, 11:43 AM
I had a look at Dialog (and Window) and I can't even workout who is firing onKeyPress() in order for your code to work.
From the Window it is coming (default impl):
protected void onKeyPress(WindowEvent we) {
int keyCode = we.getKeyCode();
if (onEsc && keyCode == KeyboardListener.KEY_ESCAPE) {
hide();
}
}
iam overriding it, uhm :D iam loosing the esc key i see :D lol :) << good to know that :D (does note: do check API before override sth :D)
My FormPanels are sitting in all kinds of LayoutContainers but not Dialog (or Window).
Christian
hmm I think you can also just add a normal Listener via addListener(), but iam not sure if it does work. I remember my try was not working before ... to add it like this to the diaglog component... before i found onKeyPress()
c.mallwitz
11-17-2008, 01:13 PM
From the Window it is coming (default impl):
protected void onKeyPress(WindowEvent we) {
int keyCode = we.getKeyCode();
if (onEsc && keyCode == KeyboardListener.KEY_ESCAPE) {
hide();
}
}
But who is calling Window.onKeyPress() if keys are pressed in the Dialog?
Christian
gslender
11-17-2008, 02:49 PM
remember you need to sink events when the widget has not yet registered to receive that event etc...
c.mallwitz
11-17-2008, 03:12 PM
remember you need to sink events when the widget has not yet registered to receive that event etc...
I need to do what?
My sample FormPanel is fully initialized and rendered with TextFields and Buttons. When I click the buttons all the right things are triggered in the client code. Do I need to do something for the FormPanel to receive the key events that are automatically available on the text fields?
Christian
gslender
11-17-2008, 03:41 PM
sinkEvents(Event.KEYEVENTS);
most of the source has these in various places... search through and you'll see how its used to ensure widgets get events... this is a GWT thing really, not just a GXT thing
EagleEye666666
11-18-2008, 03:16 AM
sinkEvents(Event.KEYEVENTS);
most of the source has these in various places... search through and you'll see how its used to ensure widgets get events... this is a GWT thing really, not just a GXT thing
thanks for pointing this out, i did not know that yet. useful info :)
kolli
11-18-2008, 07:38 AM
well can we not use the keyNav and then override the method for what ever key we want to override???
c.mallwitz
11-19-2008, 05:09 AM
Here is my helper class to make ENTER submit a FormPanel - Note: you need to supply an implementation FormPanel.submit()
public class FormPanelUtil
{
public static void addSubmitOnEnterListener(final FormPanel form)
{
form.sinkEvents(Event.ONKEYPRESS);
form.addListener(Event.ONKEYPRESS, new Listener<ComponentEvent>() {
public void handleEvent(ComponentEvent event) {
if (event.getKeyCode() == KeyboardListener.KEY_ENTER) {
form.submit();
}
}
});
}
}
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.