My code is like this:
final TextArea textArea = new TextArea();
textArea.addKeyDownHandler(new KeyDownHandler() {
@Override
public void onKeyDown(KeyDownEvent event) {
if (event.getNativeKeyCode() == KeyCodes.KEY_CTRL) {
textArea.addKeyDownHandler(new KeyDownHandler() {
public void onKeyDown(KeyDownEvent event) {
if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
foo();
}
}
});
}
}
});
I ne开发者_如何学Pythoned to listen for CTRL
+ENTER
combination,
problem is foo()
is calling when I press ENTER
.
Thanks!
You can check whether the CTRL key was depressed when the given event occurred by calling its isControlKeyDown
.
public void onKeyDown(KeyDownEvent event) {
if (event.isControlKeyDown()) {
if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
//do something here
}
}
}
you can check for stuff like control, alt, and meta:
public void keyPressed(KeyEvent e) {
int modifiers = e.getModifiersEx();
String tmpString = KeyEvent.getModifiersExText(modifiers);
final int keyCode = e.getKeyCode();
int id = e.getID();
char c = 0;
if (id == KeyEvent.KEY_TYPED)
c = e.getKeyChar();
if (!e.isAltDown() && !e.isControlDown() && !e.isMetaDown())
normalKey(keyCode);
else if (!e.isAltDown() && e.isControlDown() && !e.isMetaDown())
controlKey(keyCode, e.isShiftDown());
else
log.info("keycode not processed: " + e.getKeyCode() + ' ' + id + ' ' + c + ' ' + tmpString + ' ' + e.isAltDown() + ' ' + e.isControlDown() + ' '
+ e.isShiftDown() + ' ' + e.isMetaDown());
lastKeyCode=keyCode;
}
精彩评论