Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keybd_event KEYEVENTF_EXTENDEDKEY explanation required

In documentation it says:

KEYEVENTF_EXTENDEDKEY (0x0001): If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).

Can someone explain what this means?

What is the difference between this:

keybd_event(RIGHT, 0, 0, 0);
keybd_event(RIGHT, 0, 2, 0);

and this:

keybd_event(RIGHT, 0, 1 | 0, 0);
keybd_event(RIGHT, 0, 1 | 2, 0);

because when I execute this code I can't see no difference?

Also, what is "byte bScan" for? In description it is: A hardware scan code for the key. What that means?

like image 950
mgulan Avatar asked Jan 17 '14 22:01

mgulan


2 Answers

Both answers here are wrong. I don't understand why people vote for wrong answers ??

Both answers suggest that the flag is irrelevant. This is completely wrong. And the flag has NOTHING to do with the keypad.

The correct answer is that there are only scan codes from 01 to 7F but Virtual keys range from 01 to FF.

So as keyboards grew it became necessary that some scan codes have double assignment. For example on my keyboard the scan code 0x45 is assigned to the NumLock key AND to the Pause key.

To distinguish them the keyborard sends the Extended Key flag for the NumLock key but not for the Pause key.

There are several other keys that have double assignment like for example all media keys.

Run Spy++ that comes with the Visual Studio Tools and filter only WM_KEYDOWN and enable "Decoded message parameters" then hit some keys in a text editor.

Spy++ will show you for which keys the flag is set and for which keys it is not set.

Here the output from Spy++:

P WM_KEYDOWN nVirtKey:VK_NUMLOCK cRepeat:1 ScanCode:45 fExtended:1 fAltDown:0 fRepeat:0 fUp:0
P WM_KEYDOWN nVirtKey:VK_PAUSE cRepeat:1 ScanCode:45 fExtended:0 fAltDown:0 fRepeat:0 fUp:0

Another example is the scan code 2E which is assigned to the letter "C" and to VK_VOLUME_DOWN on my keyboard.

You must program it exactly the same way, otherwise keyboard injection with keybd_event() will fail because another key is hit than the one you intended.

Do NOT trust in MapVirtualKeyEx(MAPVK_VK_TO_VSC_EX) (>= Vista) because it does not return the extended flag for some keys although they are extended keys, like VK_LEFT for example. Another bug in this function is that it returns the extended flag (E1) for the VK_PAUSE key although this key is not extended.

like image 110
Elmue Avatar answered Nov 16 '22 00:11

Elmue


It is an ancient implementation detail of keyboard layouts on the original IBM PC. This is what the keyboard looked like back in 1981:

enter image description here

Doesn't look much like keyboards look like today. This evolved, extra keys were added like the dedicated cursor keys and the Ctrl and Alt keys to the right of the space bar. To keep it compatible with existing software that directly reads the keyboard (a very common crime back in those days), the keyboard controller reports those extended keys with the same scan code but an extra special byte ahead of it. So the right-side Ctrl and Alt keys worked the same way the left ones did, if a program cares about the distinction then it could detect the difference from the prefix byte. 0xE0 is that prefix.

Many programs don't care which particular key you pressed, they just use the virtual key code and don't care of if it is an extended key. Just like those old MS-Dos programs didn't. Which is why you don't see a difference. And since you didn't specify the scan code, it can't make a difference. A detailed document from Microsoft that describes keyboard scan codes is available here.

like image 25
Hans Passant Avatar answered Nov 16 '22 02:11

Hans Passant