为什么WM_KEYDOWN不起作用,为什么WM_KEYUP不起作用? [英] Why WM_KEYUP doesn't work, if WM_KEYDOWN does?
问题描述
WM_KEYUP
不起作用
我尝试将ASCII码从 0x57
更改为 0x005701
,因为我的朋友说它应该可以,但不能.
I tried change ASCII code from 0x57
to 0x005701
, because my friend says it should work, but it doesn't.
#include <iostream>
#include <windows.h>
using namespace std;
int KEY_DOWN = 0x57;
int main()
{
HWND hwnd;
if (!(hwnd = FindWindow(L"AAAA", NULL))) {
cout << "Couldn't find window";
}
else {
Sleep(5000);
PostMessage(hwnd, WM_SETFOCUS, NULL, NULL);
PostMessage(hwnd, WM_KEYDOWN, KEY_DOWN, NULL);
}
}
我想制作将 WM_KEYDOWN
和 WM_KEYUP
发送到带有 PostMessage
或 SendMessage
的应用程序的应用程序,但是 WM_KEYUP
不起作用
I want to make application that will send WM_KEYDOWN
, and WM_KEYUP
to application with PostMessage
or SendMessage
but WM_KEYUP
doesn't work
推荐答案
As @David Heffernan said that, SendInput can do this.
这是最少的代码.
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
HWND hwnd;
INPUT input[2];
memset(input, 0, sizeof(input));
if (!(hwnd = FindWindow(L"AAAA", NULL)))
{
cout << "Couldn't find window";
}
else
{
Sleep(5000);
input[0].type = input[1].type = INPUT_KEYBOARD;
SetForegroundWindow(hwnd);
input[0].ki.wVk = 'W';
input[0].ki.dwFlags = 0;
input[1].ki.wVk = 'W';
input[1].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(2, input, sizeof(INPUT));
}
return 0;
}
将按键按下和按键按下事件放到长度为2的数组中,并将它们作为单个原子单元.
Put the key down and key up events into an array of length 2 and send them as a single atomic unit.
设置 ki.dwFlags
的值可控制键的向上和向下状态.
Set the value of ki.dwFlags
to control up and down states of the key.
这篇关于为什么WM_KEYDOWN不起作用,为什么WM_KEYUP不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!