我的代码是这样的:
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();
}
}
});
}
}
});
我需要倾听CTRL
+ ENTER
组合,
当我按foo()
时,问题是ENTER
正在调用。
谢谢!
答案 0 :(得分:2)
您可以通过调用isControlKeyDown
来检查在给定事件发生时是否按下了CTRL键。
public void onKeyDown(KeyDownEvent event) {
if (event.isControlKeyDown()) {
if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
//do something here
}
}
}
答案 1 :(得分:-1)
你可以查看控件,alt和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;
}