views:

343

answers:

2

I am working with a WPF application sending keys to a game. I opened spy++ to observer s as a key press on the keyboard. I then press my button on the application and I noticed a different scan code in spy++ messages. Could this be somthing to do with Windows 7 64bit? Partial listing:

var down = new INPUT();
down.Type = (UInt32)InputType.KEYBOARD;
down.Data.Keyboard = new KEYBDINPUT();
down.Data.Keyboard.Vk = (UInt16)keyCode;
down.Data.Keyboard.Scan = 0;
down.Data.Keyboard.Flags = 0;
down.Data.Keyboard.Time = 0;
down.Data.Keyboard.ExtraInfo = IntPtr.Zero;
//down.Data.Keyboard.ExtraInfo = GetMessageExtraInfo();

var up = new INPUT();
up.Type = (UInt32)InputType.KEYBOARD;
up.Data.Keyboard = new KEYBDINPUT();
up.Data.Keyboard.Vk = (UInt16)keyCode;
up.Data.Keyboard.Scan = 0;
up.Data.Keyboard.Flags = (UInt32)KeyboardFlag.KEYUP;
up.Data.Keyboard.Time = 0;
up.Data.Keyboard.ExtraInfo = IntPtr.Zero;
//up.Data.Keyboard.ExtraInfo = GetMessageExtraInfo();

INPUT[] inputList = new INPUT[2];
inputList[0] = down;
inputList[1] = up;

var numberOfSuccessfulSimulatedInputs = SendInput(2, inputList, Marshal.SizeOf(typeof(INPUT)));

The image shows when I use my code to send a key I receive ScanCode:00fExtended from spy++ message output. When I actually press the same key I receive ScanCode:1FfExtended. Everything else is identical.

http://spilmansoftware.com/images/scancode.gif

A: 

According to the MSDN documentation for WM_KEYDOWN, the scan code value depends on the OEM.

In particular, it's possible that your keyboard is not English/US American keyboard. Or you can have any non-standard toggle keys (like F Lock) turned on. Play with these and see if changing these will have any effect on the scan code.

Franci Penov
+1  A: 

You'll need to mentally put a space between 00 and fExtended, the actual phrase is "ScanCode:00" followed by "fExtended:0"

That the scan code is 0 is unsurprising, you set it to 0 in your code:

 down.Data.Keyboard.Scan = 0;

This shouldn't cause trouble, the scan code is only used when the virtual key is ambiguous. The left and right Shift key for example. You can use MapVirtualKey() if you prefer.

Hans Passant
Good catch! Thank you.
Stanomatic