gpt4 book ai didi

java - 为什么类似的 keyChar 和 keyCode 在 KeyStroke 中不相等?

转载 作者:太空宇宙 更新时间:2023-11-04 06:59:59 24 4
gpt4 key购买 nike

我不明白为什么这会给出错误并且不被视为平等。

KeyStroke test1 = KeyStroke.getKeyStroke('1');
KeyStroke test2 = KeyStroke.getKeyStroke(KeyEvent.VK_1, 0);
System.out.println(test1.equals(test2));

在什么情况下这不相等,即这是一个功能还是一个错误?

最佳答案

您在第一行中传递的是一个字符KeyEvent.VK_1 是一个以十六进制 (0x30) 表示的整数。数字和键由具有十六进制值的基本类型 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.

关于java - 为什么类似的 keyChar 和 keyCode 在 KeyStroke 中不相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22225508/

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