I am using ShortcutListener with ENTER key and find next problem: when I add listener to TextField, ENTER key pressing not working at all other text fields at window.
Below present my usage of shortcut listener.
TextField field = new TextField();
// ...
field.addShortcutListener(new FocusShortcut(field, KeyCode.ENTER, null) {
@Override
public void handleAction(Object sender, Object target) {
// do something
}
});
What I am doing wrong ? Or is it a bug ? Help me, please.
Hi again!
Nobody is able to comment on this topic?
full example here:
package test;
import com.vaadin.Application;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.ui.*;
import com.vaadin.ui.AbstractField.FocusShortcut;
import java.io.ByteArrayInputStream;
public class ShortcutTestApp extends Application {
@Override
public void init() {
Window window = new Window("app");
setMainWindow(window);
VerticalLayout content = new VerticalLayout();
window.setContent(content);
TextField field = new TextField();
field.setCaption("field with shortcut action");
content.addComponent(field);
TextField field2 = new TextField();
field2.setCaption("field without shortcut action");
content.addComponent(field2);
field.addShortcutListener(new FocusShortcut(field, KeyCode.ENTER, null) {
@Override
public void handleAction(Object sender, Object target) {
getMainWindow().showNotification("Shortcut handle action");
}
});
String template = "<form>"
+ "<fieldset>"
+ "<legend>text area from custom layout</legend>"
+ "<textarea rows=\"5\" cols=\"27\" name=\"question\">"
+ "</textarea>"
+ "</fieldset>"
+ "</form>";
try {
CustomLayout custom = new CustomLayout(new ByteArrayInputStream(template.getBytes()));
content.addComponent(custom);
} catch (Throwable er) {
System.out.println(er.getMessage());
}
}
}
problem 1: shortcut listener attached only for first field, but work at all other.
problem 2: at text area (from custom layout) cursor does not moves to next row after enter key pressing
The Problem with the ShortcutListener is, that it is attached to the Window, the TextField is in.
Therefore it listens to all keystrokes the window receives.
And yes, the ShortcutListener also reacts to the keystrokes in the TextArea.
But you can create a new row by pressing Shift+Enter.
I still have the same problem. I have more than one TextField, each field was added a new instance of ShorcutListener, also I added each text Field to a different Panels, but I notice that the same ShorcutListener is handled the action.
[font=Courier New]
fPanel = new Panel();
fTextField = new TextField();
fPanel.addComponent(fTextField);
@Override
public void handleAction(final Object sender, final Object target) {
final TextField field = (TextField) target;
System.out.println(hashCode() + " value: " + field.getValue()
+ " " + field.getDebugId());
}
});
[/font]
Another problem: Is that in the same Portlet I have another TextArea, this field only have the TextChangeListener, and when I focus on the this field and type the ENTER key the ShorcutListener handler the action.
I can confirm that wrapping the form containing text areas with a panel does not prevent the ENTER key from being captured incorrectly by an OK button elsewhere in the dialog configured with ENTER as its click shortcut. These are usability basics that should be fixed ASAP in an otherwise great framework.
Hi,
that’s still a bug . I am using Vaadin 7.1.0.
There is just one context, that’s the Window. Wrapping around panels etc. do not create a context for the ShortCurtListener…
And I want to avoid using instanceof or workarounds in my code (inside handleAction() to achieve my targets.
This is still a problem… In TextField ShortcutListener is handled normaly, but after simple change to TextArea it doesn’t react at all. I’m using vaadin 7.2
I have the similar issue with my
vaadin-7.1.10 application. My requirement is to get the
Tab
and
Shift + Tab
keyCode . But, when I’m applying
Tab key, the
Tab key doesn’t work anywhere in the UI. I just added
ShortcutListener only on a single
TextField . Not only that, when any key is passed for shortcut listening it doesn’t work there after.
See the below code :
TextField tf = new TextField();
tf.setDescription("tf");
TextField tf2 = new TextField();
tf2.setDescription("tf2");
tf2.addShortcutListener(
//pass any KeyCode.A, KeyCode.B, KeyCode.C etc..
new ShortcutListener("", KeyCode.A, new int[] {}) {
@Override
public void handleAction(Object sender, Object target) {
System.out.println("sender " + sender + " target " + target);
System.out.println("getKeyCode() " + getKeyCode());
if (getModifiers().length > 0) {
System.out.println("modifier " + getModifiers()[0]
);
}
if (getKeyCode() == KeyCode.TAB) {
}
}
}
);
HorizontalLayout horizontalLayout = new HorizontalLayout();
horizontalLayout.addComponent(tf);
horizontalLayout.addComponent(tf2);
layout.addComponent(horizontalLayout);
In that, the
ShortcutListener is added in the
TextField tf2 for
KeyCode.A
, the handleAction method invokes when key press of
A but no textfields are taking
A key, they are just ignoring it. This effect is same to all other keys. Is there any alternative way to achieve my requirement.