我不明白为什么这样做是假的,而且被认为是不平等的。
KeyStroke test1 = KeyStroke.getKeyStroke('1');
KeyStroke test2 = KeyStroke.getKeyStroke(KeyEvent.VK_1, 0);
System.out.println(test1.equals(test2));在哪种情况下,这是不平等的,即这是一个特性还是一个错误?
发布于 2014-03-06 13:13:40
在第一行中传递的是一个Character,而KeyEvent.VK_1是一个用十六进制(0x30)表示的Integer。数字和键由带有十六进制值的基元类型int表示。
例如:从0到9的数字是以这种方式表示的十六进制:
public static final int VK_0 = 0x30;
...
public static final int VK_9 = 0x39;编辑
它们是不同的,因为第一个KeyStroke认为数字1是键入的
第二个KeyStroke正在考虑按下1。
它们不是不同的键,而是不同的动作
KeyStroke test1 = KeyStroke.getKeyStroke('1', KeyEvent.KEY_LOCATION_UNKNOWN);
KeyStroke test2 = KeyStroke.getKeyStroke(KeyEvent.VK_1, KeyEvent.KEY_LOCATION_UNKNOWN);
System.out.println(test1.equals(test2));这将是true
0参数KeyStroke.getKeyStroke(KeyEvent.VK_1, 0);表示常量KeyEvent.KEY_LOCATION_UNKNOWN
文件上说:
A constant indicating that the keyLocation is indeterminate
or not relevant.
KEY_TYPED events do not have a keyLocation; this value
is used instead.https://stackoverflow.com/questions/22225508
复制相似问题