gpt4 book ai didi

KeyListener 上的 Java SyntheticEvent

转载 作者:行者123 更新时间:2023-12-01 14:51:24 30 4
gpt4 key购买 nike

我正在实现一些扩展 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 的意思是什么,并且在 Google 上没有成功。谁能指出我正确的方向 - 这有什么问题吗?此后窗口正常处理。

谢谢!

最佳答案

不知道输出是什么。

不要使用 KeyListener 来实现此类功能。请参阅:

  1. How to Use Menus ,或
  2. How to Use Key Bindings

关于KeyListener 上的 Java SyntheticEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14804347/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com