我正在使用此代码
const int WM_KEYDOWN = 256;
const int WM_KEYUP = 257;
const int WM_CHAR = 258;
public static void SendKeys(string message){
int foregroundWindowHandle = GetForegroundWindow();
uint remoteThreadId = GetWindowThreadProcessId(foregroundWindowHandle, 0);
uint currentThreadId = GetCurrentThreadId();
//AttachTrheadInput is needed so we can get the handle of a focused window in another app
AttachThreadInput(remoteThreadId, currentThreadId, true);
//Get the handle of a focused window
int focused = GetFocus();
//Now detach since we got the focused handle
AttachThreadInput(remoteThreadId, currentThreadId, false);
foreach (char c in message)
{
//SendMessage(focused, WM_CHAR, (int)c, null);
SendMessage(focused, WM_KEYDOWN, 65, null);
SendMessage(focused, WM_KEYUP, 66, null);
SendMessage(focused, WM_CHAR, 67, null);
}
}
当我测试它时(例如,记事本处于活动状态),只有字母 C 打印,所以只有 WM_CHAR 工作 - 为什么?