SendMessage, When To Use KEYDOWN, SYSKEYDOWN, etc?

2020-04-08 13:28发布

问题:

I am writing an application that sends keystrokes to another application using SendMessage.

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);

I have been experimenting with the various arguments and I have noticed that, depending on which keystroke I am sending, I have to pass in different arguments to the parameter int Msg.

For example, I have the following defined as possible values for Msg.

private static ushort WM_SYSKEYDOWN = 0x0104;
private static ushort WM_CHAR = 0x0102;
private static ushort WM_KEYDOWN = 0x0100;

and the following test keys that are passed in to the 3rd parameter wParam:

private static ushort VK_F1 = 0x70;
private static ushort VK_A = 0x41;
private static ushort VK_TAB = 0x09;

When I want to send the F1 key (VK_F1) it only works whenever is use WM_SYSKEYDOWN, when I want to send the A key it only works when I use WM_CHAR, and when I want to send the TAB key it only works when I use WM_KEYDOWN.

My question is: is there some documentation out there that tells me when to use WM_KEYDOWN, WM_SYSKEYDOWN, etc. based on what key that is being sent? Or perhaps I am doing it wrong and there is one consistent method that works for any key?

回答1:

Microsoft explains the difference between these WM messages here Keyboard Input (Windows).

WM_SYSKEYDOWN simulates system commands like ALT + TAB used to switch windows.

WM_CHAR simulates user input like input in a text box for instance.

WM_KEYDOWN is usually used together with WM_KEYUP. See WM_KEYDOWN message (Windows).

Most probably you will want to use WM_CHAR.