5

与 C++/C 相比,某些键(如shift、[、]、Del 等)的虚拟键代码在 java 中显示为不同的值。例如 :

Key     Java       C / C++
Shift   16         160
[       91         219
]       93         221
\       92         220
Del     127        46
Window  524        91

这是什么原因?这些代码是虚拟代码还是它们是不同的类型?对于包括字母、数字、功能键(F1-F12)、退格、`等的键是相同的。

我可能误解了一个概念,在这种情况下请澄清。

在 C/C++ 中检查

KBDLLHOOKSTRUCT * kbhook = (KBDLLHOOKSTRUCT *) lParam;
printf("%u\n",kbhook->vkCode);

在 Java 中检查

private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {                                       
    int code = evt.getKeyCode();
    // code isEqualTo KeyEvent.VK_DELETE : NOTE

}

参考:KeyEvent 类

4

4 回答 4

3

Virtual Key Codes are extremely virtual, I should say.

You won't get away without some code like JavaKeyToWin32Key, Win32KeyToJava and so on for each platform you're trying to interoperate with.

I believe all of these keycodes are mostly historical. Some come from hardware design decisions (take a look at Apple's "modern" key codes where the 0 code is 'A', 1 is 'S', 2 is 'D' and so on - should I continue or you get the "pattern" which follows from the keyboard layout ?).

"Why there are no standard ?"

It's business and nothing personal. Thirty-forty years ago everyone where developing their own hardware from scratch, twenty five years ago everybody were trying to make the best CPU, 15 years ago it has all began with the "platforms", where everything was once again redefined, but also should maintain compatibility with existing solutions (by the same company, of course).

Java is a standard, but not for everyone. It is already an abstraction above all the OSes with its own set of keycodes. So "VK_" is a legacy of Microsoft, Java key codes might be influenced by the Sun Solaris OS, but I'm not sure.

于 2012-06-01T11:27:46.027 回答
2

虚拟键码是典型键盘上某些键的特定于 MS 的表示。因此虚拟修饰符。请注意,您为 Java 指定的值表示使用ASCII编码时这些键的值。它们构成较低 ASCII编码的一部分。如果 OTOH,您使用了标准 C 函数,例如,getchar如果您使用 ASCII 编码,您将获得与 Java 中相同的值。但是,您可以有一个特殊的(想想非 ASCII/非 Unicode)编码,其中这些字符将被分配不同的整数。

特别是 ASCII 集经过精心设计,考虑到某些经常使用的操作(例如小写到大写)等可以优化。

于 2012-06-01T10:59:43.867 回答
1

MSDN 库关于使用虚拟键代码的说明:“键盘上的每个键在按下和释放键时都会生成一个扫描码。扫描码是一个与硬件相关的数字,用于识别该键。键盘驱动程序会翻译或映射每次扫描代码转换为虚拟键代码。虚拟键代码是一个独立于硬件的数字,用于标识键。由于键盘布局因语言而异,Windows CE 仅提供所有虚拟键代码的核心集键盘。这个核心集包括英文字符、数字和一些关键键,例如功能键和箭头键”

这是一组虚拟键代码- 这些是您从KBDLLHOOKSTRUCTvkCode成员检索的值。

于 2012-06-01T11:03:09.083 回答
0

yes,in both the cases they are the virtual codes.

于 2012-06-01T11:27:03.210 回答