11

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?

ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
mgulan
  • 795
  • 1
  • 14
  • 33

3 Answers3

22

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.

ADDITIONAL INFO:

Keyboards are very old and were formerly connected over a serial PS/2 cable. The highest bit was used to transmit if the key was pressed or released, so there are only 127 scancodes available. Special keys have sent their scancode prefixed with the byte 0xE0 which acted like an escape byte. When this byte was sent the scancode was an "extended scancode" and had another meaning than without 0xE0. Windows presents us this "extended scancode" by setting KEYEVENTF_EXTENDEDKEY.

Here you find more details about the origin of the very old keyboards:

https://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html

And this one has to be searched in the WayBackMachine:

http://www.barcodeman.com/altek/mule/scandoc.php

Elmue
  • 7,602
  • 3
  • 47
  • 57
  • 3
    [To add from](https://msdn.microsoft.com/en-us/library/ms646267%28v=vs.85%29.aspx) : _The extended-key flag indicates whether the keystroke message originated from one of the additional keys on the enhanced keyboard. The extended keys consist of the ALT and CTRL keys on the right-hand side of the keyboard; the INS, DEL, HOME, END, PAGE UP, PAGE DOWN, and arrow keys in the clusters to the left of the numeric keypad; the NUM LOCK key; the BREAK (CTRL+PAUSE) key; the PRINT SCRN key; and the divide (/) and ENTER keys in the numeric keypad. The extended-key flag is set if the key is a extended key_ – Carol Aug 21 '15 at 00:09
  • I don't know why you post this text here? This is a very old description (from the times of Windows 95) which is not up to date anymore. And it is not correct. There are much more extended keys than the ones that you enumerate in your posting like for example all the Media keys which are NOT at the right hand side of the keyboard. – Elmue Aug 21 '15 at 11:49
  • 1
    I think it still adds to your post, if you have something more recent that shows ALL extended keys, please go ahead. – Carol Aug 21 '15 at 20:39
  • I have searched a long for a complete list of extended keys in internet, but without success, or with wrong data (even in the MSDN you find wrong information). So I recommend to hit the keys on your keyboard and observe what Spy++ logs. This is the easiest and fastest way to get this information. It is really a desaster that there is no API which returns reliable information. – Elmue Aug 21 '15 at 22:04
  • I am wondering why Windows tells us that NumLock is an extended key and Pause is not? In hardware is the opposite: NumLock does not send scan code E0, Pause sends E1 (there is no Pause key on the original XT/AT keyboards). – CoolCmd Apr 23 '23 at 11:06
  • In Windows 11 you can chose between 204 keyboard layouts. I doubt that there is a general rule how scan codes are translated. It depends on the specific keyboard. Each keyboard layout uses it's own KBDXXX.DLL in C:\Windows\System32 which contains the scancode definitions. – Elmue Apr 24 '23 at 14:33
12

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

The simple (and incomplete) explanation is that KEYEVENTF_EXTENDEDKEY means "this keystroke is from the numeric keypad"

Since most programs behave the same if you hit '1' above the 'q' key as they do when you hit '1' on the numeric keypad (which is an extended key) - you wouldn't normally expect to see any difference when you set this flag when simulating keyboard input.

Programs that pay attention to the location of a key will usually respond to this flag.

The bScan value is a raw hardware scancode. For an explanation of scancodes look here http://en.wikipedia.org/wiki/Scancode. Like the Extended key flag, most programs pay no attention to scancodes. The values are there in case the program wants to treat the keyboard as a bunch of buttons.

John Knoeller
  • 33,512
  • 4
  • 61
  • 92
  • Thank you very much for this great explanation. Now it's more clear to me. Also, where can I get the complete explanation for EXTENDEDKEY, is there some article? – mgulan Jan 18 '14 at 10:17
  • 5
    This answer is wrong. I don't understand why people vote for an answer that is completely wrong?? The extended flag has absolutely nothing to do with the keypad. The only keys on the keypad that require this flag are VK_DIVIDE and VK_NUMLOCK. On the other hand there are other keys that require this flag and have absolutely nothing to do with the keypad like VK_LWIN or VK_RIGHT or VK_VOLUME_DOWN. See my answer here. – Elmue May 27 '15 at 14:14
  • 1
    There is a lack to this answer. This is not related to the numeric keypad per se. [Read here](https://msdn.microsoft.com/en-us/library/ms646267%28v=vs.85%29.aspx) or my comment below – Carol Aug 21 '15 at 00:14