我正在实现一些扩展JDialog的对话窗口,需要实现一些键盘快捷键,例如:
class MyDialog extends JDialog implements KeyListener
{
/** Constructor
*
*/
public MyDialog() {
super();
// add keyboard shortcuts:
addKeyListener(this);
setFocusable(true);
}
// ---------------------------------------------------------
// Keyboard shortcuts
// ---------------------------------------------------------
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e)
{
System.out.println(e);
if ((e.getKeyCode() == KeyEvent.VK_W) && ((e.getModifiers() & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0)) {
dispose();
}
}
}
然后我有几种不同类型的对话框来扩展MyDialog。触发键盘快捷键时,我在控制台上获得以下输出:
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=157,keyText=?,keyChar=Undefined keyChar,modifiers=?,extModifiers=?,keyLocation=KEY_LOCATION_LEFT,rawCode=0,primaryLevelUnicode=0,scancode=0] on dialog0
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=87,keyText=W,keyChar='w',modifiers=?,extModifiers=?,keyLocation=KEY_LOCATION_STANDARD,rawCode=0,primaryLevelUnicode=0,scancode=0] on dialog0
2013-02-10 19:24:28.716 java[10029:2707] Attempt to call non-applicable method "keyCode" on SyntheticEvent!
2013-02-10 19:24:28.718 java[10029:2707] (
0 CoreFoundation 0x00007fff90e5f0a6 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff9617b3f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff90e5ee7c +[NSException raise:format:] + 204
3 libawt.jnilib 0x0000000110e3564b -[SyntheticEvent keyCode] + 58
4 libawt.jnilib 0x0000000110e397a4 -[NSViewAWT performKeyEquivalent:] + 507
5 AppKit 0x00007fff938793c9 -[NSView _performKeyEquivalent:conditionally:] + 41
6 AppKit 0x00007fff93879512 -[NSView performKeyEquivalent:] + 166
7 AppKit 0x00007fff938793c9 -[NSView _performKeyEquivalent:conditionally:] + 41
8 AppKit 0x00007fff93879329 -[NSWindow performKeyEquivalent:] + 64
9 AppKit 0x00007fff93879114 -[NSApplication _handleKeyEquivalent:] + 462
10 AppKit 0x00007fff93c29232 -[NSWindow keyDown:] + 89
11 Foundation 0x00007fff91965677 __NSThreadPerformPerform + 225
12 CoreFoundation 0x00007fff90dde101 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
13 CoreFoundation 0x00007fff90dddaed __CFRunLoopDoSources0 + 445
14 CoreFoundation 0x00007fff90e00dc5 __CFRunLoopRun + 789
15 CoreFoundation 0x00007fff90e006b2 CFRunLoopRunSpecific + 290
16 HIToolbox 0x00007fff8b4430a4 RunCurrentEventLoopInMode + 209
17 HIToolbox 0x00007fff8b442e42 ReceiveNextEventCommon + 356
18 HIToolbox 0x00007fff8b442cd3 BlockUntilNextEventMatchingListInMode + 62
19 AppKit 0x00007fff93655613 _DPSNextEvent + 685
20 AppKit 0x00007fff93654ed2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
21 libawt.jnilib 0x0000000110de6fcf -[NSApplicationAWT nextEventMatchingMask:untilDate:inMode:dequeue:] + 124
22 AppKit 0x00007fff9364c283 -[NSApplication run] + 517
23 libawt.jnilib 0x0000000110de5680 +[AWTStarter startAWT:] + 1495
24 libawt.jnilib 0x0000000110de4ffa -[CPerformer perform] + 93
25 Foundation 0x00007fff91965677 __NSThreadPerformPerform + 225
26 CoreFoundation 0x00007fff90dde101 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
27 CoreFoundation 0x00007fff90ddda25 __CFRunLoopDoSources0 + 245
28 CoreFoundation 0x00007fff90e00dc5 __CFRunLoopRun + 789
29 CoreFoundation 0x00007fff90e006b2 CFRunLoopRunSpecific + 290
30 java 0x0000000107c97843 java + 18499
31 java 0x0000000107c9729a java + 17050
32 java 0x0000000107c94a98 java + 6808
)
前两个是println语句,这似乎是正常的。我不知道SyntheticEvent的东西意味着什么,并且谷歌没有成功。任何人都可以指出我正确的方向 - 这有什么问题?在此之后窗口正常处理。
谢谢!