使用特定字段创建 KeyEvent
Creating KeyEvent with specific fields
我将如何更改生成的 KeyEvent 的字段?
来自键盘的实际 KeyEvent
java.awt.event.KeyEvent[KEY_PRESSED,
keyCode=65,
keyText=A,
keyChar='a',
keyLocation=KEY_LOCATION_STANDARD,
rawCode=65,
primaryLevelUnicode=97,
scancode=30,
extendedKeyCode=0x41] on panel0
生成的按键事件
java.awt.event.KeyEvent[KEY_PRESSED,
keyCode=65,
keyText=A,
keyChar='a',
keyLocation=KEY_LOCATION_STANDARD,
rawCode=0,
primaryLevelUnicode=0,
scancode=0,
extendedKeyCode=0x0] on panel0
new KeyEvent(component, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, 65, 'a')
差异
rawCode
primaryLevelUnicode
scancode
extendedKeyCode
有没有办法在 KeyEvent 对象上设置这些字段?
文档https://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html
查看 KeyEvent
class 中的源代码,您似乎无法设置这些:
//set from native code.
private transient long rawCode = 0;
private transient long primaryLevelUnicode = 0;
private transient long scancode = 0; // for MS Windows only
private transient long extendedKeyCode = 0;
它是私有的,由本机代码设置(因此 Java 中没有更改)。
你可以使用反射:
KeyEvent m = new KeyEvent(component, type, System.currentTimeMillis(), 0, ext,(char)key, KEY_LOCATION_STANDARD);
try
{
Field f = m.getClass().getDeclaredField("rawCode");
f.setAccessible(true);
f.setLong(m, key);
f = m.getClass().getDeclaredField("primaryLevelUnicode");
f.setAccessible(true);
f.setLong(m, key);
f = m.getClass().getDeclaredField("scancode");
f.setAccessible(true);
f.setLong(m, scanCode);
}
catch (Exception e)
{
e.printStackTrace();
}
我将如何更改生成的 KeyEvent 的字段?
来自键盘的实际 KeyEvent
java.awt.event.KeyEvent[KEY_PRESSED,
keyCode=65,
keyText=A,
keyChar='a',
keyLocation=KEY_LOCATION_STANDARD,
rawCode=65,
primaryLevelUnicode=97,
scancode=30,
extendedKeyCode=0x41] on panel0
生成的按键事件
java.awt.event.KeyEvent[KEY_PRESSED,
keyCode=65,
keyText=A,
keyChar='a',
keyLocation=KEY_LOCATION_STANDARD,
rawCode=0,
primaryLevelUnicode=0,
scancode=0,
extendedKeyCode=0x0] on panel0
new KeyEvent(component, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, 65, 'a')
差异
rawCode
primaryLevelUnicode
scancode
extendedKeyCode
有没有办法在 KeyEvent 对象上设置这些字段?
文档https://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html
查看 KeyEvent
class 中的源代码,您似乎无法设置这些:
//set from native code.
private transient long rawCode = 0;
private transient long primaryLevelUnicode = 0;
private transient long scancode = 0; // for MS Windows only
private transient long extendedKeyCode = 0;
它是私有的,由本机代码设置(因此 Java 中没有更改)。
你可以使用反射:
KeyEvent m = new KeyEvent(component, type, System.currentTimeMillis(), 0, ext,(char)key, KEY_LOCATION_STANDARD);
try
{
Field f = m.getClass().getDeclaredField("rawCode");
f.setAccessible(true);
f.setLong(m, key);
f = m.getClass().getDeclaredField("primaryLevelUnicode");
f.setAccessible(true);
f.setLong(m, key);
f = m.getClass().getDeclaredField("scancode");
f.setAccessible(true);
f.setLong(m, scanCode);
}
catch (Exception e)
{
e.printStackTrace();
}