我正在尝试调试Java应用程序。有几个回合事件,我试图缩小它们,因为我还不知道bug来自哪里。有没有办法输出所有摆动事件的句柄导致它们到控制台?
答案 0 :(得分:4)
有没有办法用引起它们的句柄输出所有挥杆事件到控制台?
对于调试以帮助查找事件的来源,可以使用AWTEventListeners
来侦听所有已调度的事件。
只需尝试AWTEventListener即可。添加更多AWTEvent,无论你想听什么。
long eventMask = AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_WHEEL_EVENT_MASK
| AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.KEY_EVENT_MASK;
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
String paramString = e.paramString();
System.out.println(paramString);
}
}, eventMask);
示例输出;
MOUSE_MOVED,(131,144),absolute(131,144),clickCount=0
MOUSE_PRESSED,(131,144),absolute(131,144),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1
MOUSE_RELEASED,(131,144),absolute(131,144),button=1,modifiers=Button1,clickCount=1
MOUSE_CLICKED,(131,144),absolute(131,144),button=1,modifiers=Button1,clickCount=1
MOUSE_MOVED,(134,143),absolute(134,143),clickCount=0
MOUSE_MOVED,(135,142),absolute(135,142),clickCount=0
MOUSE_MOVED,(172,118),absolute(172,118),clickCount=0
KEY_PRESSED,keyCode=70,keyText=F,keyChar='f',keyLocation=KEY_LOCATION_STANDARD,rawCode=70,primaryLevelUnicode=102,scancode=33,extendedKeyCode=0x46
key pressed:70
KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='f',keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0,extendedKeyCode=0x0
KEY_PRESSED,keyCode=68,keyText=D,keyChar='d',keyLocation=KEY_LOCATION_STANDARD,rawCode=68,primaryLevelUnicode=100,scancode=32,extendedKeyCode=0x44
key pressed:68
KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='d',keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0,extendedKeyCode=0x0
KEY_PRESSED,keyCode=83,keyText=S,keyChar='s',keyLocation=KEY_LOCATION_STANDARD,rawCode=83,primaryLevelUnicode=115,scancode=31,extendedKeyCode=0x53
key pressed:83
KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='s',keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0,extendedKeyCode=0x0
KEY_RELEASED,keyCode=70,keyText=F,keyChar='f',keyLocation=KEY_LOCATION_STANDARD,rawCode=70,primaryLevelUnicode=102,scancode=33,extendedKeyCode=0x46
KEY_PRESSED,keyCode=70,keyText=F,keyChar='f',keyLocation=KEY_LOCATION_STANDARD,rawCode=70,primaryLevelUnicode=102,scancode=33,extendedKeyCode=0x46
key pressed:70