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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With