difference between virtual code and scan code
Asked Answered
P

1

6

What is the difference between Virtual Key Code and Scan Code. I read about scan code here but I do not understand what are virtual codes used for and what makes scan code different from virtual code ?

For example in Java :

private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {                                       
   int code = evt.getKeyCode(); // WHAT DO I GET ? A Scan code or a Virtual Code..?
}

In C :

KBDLLHOOKSTRUCT *kbhook = (KBDLLHOOKSTRUCT *) lParam;
printf("%u\n",kbhook->vkCode); // WHAT DO I GET ? A Scan code or a Virtual Code..?

The above are just examples and I don't want the concept be clarified on this basis.

Pokorny answered 5/10, 2012 at 4:0 Comment(8)
IIRC, scancodes can vary, so vk codes are a way to make the different scancodes that are actually the same key into one uniform code. On the other hand, my memory might be completely out of whack.Persuade
To put it simply Virtual KeyCodes are hardware independent where as Scan Codes are Hardware DependentAgnail
@Vulcan Are you sure ,they are virtual codes ?Pokorny
@SuhailGupta For the Java example, definitely. For the C example, I'm pretty sure, especially since the field is called "vkCode" meaning virtual key code.Casarez
@Vulcan read the top most comment @ #10849401Pokorny
@SuhailGupta There's no standardization of virtual key codes, but it might be scancode actually. I recall performing similar tests where I found that the left shift key was 160 and the right shift key was 161, suggesting that they don't translate to a virtual VK_SHIFT with the vkCode field.Casarez
@Vulcan It looks vague to know the result is a scan code when I called kbhook->vkCode! ( vkCode , I guess stands for virtual code )Pokorny
@Vulcan: Imagine a computer game where left shift fires a missile and right shift does something else. Because they're different physical keys you need different virtual codes. If left shift is 160 on every system (regardless of hardware) then it's a virtual code and you can do "if(key = 160) fire_missile()" and it'd work. If left shift has different values on different hardware then it's a (hardware dependant) scan code that is almost entirely useless.Jugum
H
2

scan code represents hardware dependent code for a particular key, however virtual code represents hardware independent and OS dependent code for the same key. So as a programmer we should always be using virtual codes...

now lets come to Java and C++. VK_ keywords are microsoft defined and Java uses its own VK_ keywords... hence they may refer to same character codes but their values are different.

Hibernal answered 5/10, 2012 at 6:27 Comment(2)
"Always" - Only a sith deals in absolutes. Yes you should generally use VK codes, but Scan codes have their places too. Like when you need to simulate a keyboard at a lower level with SendInput (as one example). I'm sure there are other situations where scan codes are helpful.Arabella
An example where scan codes should be preferred - games: Key bindings should be linked to the physical location so they don't change and if a manual says to press the "tilde key, the one below esc" then using scan codes would do the right thing most of the time, like on idTech game engines. Unreal Engine gets it wrong and i remember the frustration of finding the console key, had to press all buttons on my keyboard to figure out which one it is.Devinna

© 2022 - 2024 — McMap. All rights reserved.