问题
Virtual key codes for some keys like shift, [ , ],Del etc are displayed as a different value in java compared to C++/C. For example :
Key Java C / C++
Shift 16 160
[ 91 219
] 93 221
\ 92 220
Del 127 46
Window 524 91
What is the reason for this ? Are these codes the virtual codes or they are a different type ? For the keys including alphabets,numbers,the function keys(F1-F12),backspace,`,etc are the same.
I might be misunderstanding a concept,in that case please clarify.
Checked in C/C++
KBDLLHOOKSTRUCT * kbhook = (KBDLLHOOKSTRUCT *) lParam;
printf("%u\n",kbhook->vkCode);
Checked in Java
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
int code = evt.getKeyCode();
// code isEqualTo KeyEvent.VK_DELETE : NOTE
}
Ref : KeyEvent class
回答1:
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.
回答2:
Virtual Key Codes are a MS specific representation of certain keys found on a typical keyboard. Hence the virtual modifier. Note, the values that you have specified for Java represent the values of those keys when using the ASCII encoding. They form part of the lower ASCII encoding. If OTOH, you used a standard C function such as getchar
you'd get the same values as in Java provided you are using the ASCII encoding. You could however have a special (think non-ASCII/non-Unicode) encoding where these characters will be assigned different integers.
The ASCII set particularly is carefully designed keeping in mind that certain oft-used operations (such as lowercase to uppercase) etc. can be optimized.
回答3:
MSDN Library says about Using Virtual-Key Codes: "Each key on the keyboard generates a scan code when the key is pressed and released. The scan code is a hardware-dependent number that identifies the key. The keyboard driver translates or maps each scan code to a virtual-key code. The virtual-key code is a hardware-independent number that identifies the key. Because keyboard layouts vary from language to language, Windows CE offers only the core set of virtual-key codes that are found on all keyboards. This core set includes English characters, numbers, and a few critical keys, such as the function and arrow keys".
Here's the set of Virtual-Key Codes - these are the values you retrieve from vkCode
member of KBDLLHOOKSTRUCT.
回答4:
yes,in both the cases they are the virtual codes.
来源:https://stackoverflow.com/questions/10848901/are-they-really-the-virtual-codes