windows SendInput (C++) 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2367521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:03:03  来源:igfitidea点击:

SendInput (C++) is not working

c++windowswinapi

提问by asmw

The return value is 4 and I'm running Visual Studio in administrative mode so permissions should be ok. I don't see anything typed out though. Any help? I'm using Windows 7 x64.

返回值是 4,我在管理模式下运行 Visual Studio,所以权限应该没问题。虽然我没有看到任何输入。有什么帮助吗?我正在使用 Windows 7 x64。

INPUT input[4];

input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = 0;
input[0].ki.wScan = 'a';
input[0].ki.dwFlags = KEYEVENTF_SCANCODE;
input[0].ki.time = 0;
input[0].ki.dwExtraInfo = 0;

input[1].type = INPUT_KEYBOARD;
input[1].ki.wVk = 0;
input[1].ki.wScan = 'a';
input[1].ki.dwFlags = KEYEVENTF_KEYUP | KEYEVENTF_SCANCODE;
input[1].ki.time = 0;
input[1].ki.dwExtraInfo = 0;

input[2].type = INPUT_KEYBOARD;
input[2].ki.wVk = 0;
input[2].ki.wScan = 'a';
input[2].ki.dwFlags = KEYEVENTF_SCANCODE;
input[2].ki.time = 0;
input[2].ki.dwExtraInfo = 0;

input[3].type = INPUT_KEYBOARD;
input[3].ki.wVk = 0;
input[3].ki.wScan = 'a';
input[3].ki.dwFlags = KEYEVENTF_KEYUP | KEYEVENTF_SCANCODE;
input[3].ki.time = 0;
input[3].ki.dwExtraInfo = 0;

int retval = SendInput(4, input, sizeof(INPUT));
if(retval > 0)
{
  wxLogDebug("SendInput sent %i", retval);
}
else
{
  wxLogError("Unable to send input commands. Error is: %i", GetLastError());
}

回答by SLaks

You need to send both KeyDown and KeyUp events for each key.
To send a KeyUp event, set dwFlagsto KEYEVENTF_KEYUP.

您需要为每个键发送 KeyDown 和 KeyUp 事件。
要发送 KeyUp 事件,请设置dwFlagsKEYEVENTF_KEYUP

Also, you need to use wVkinstead of wScan. (wScanis only used with KEYEVENTF_UNICODE)

此外,您需要使用wVk而不是wScan. (wScan仅用于KEYEVENTF_UNICODE)

回答by SLaks

Just to spell it out for whomever comes across this. I addedKEYEVENTF_UNICODE and removedKEYEVENTF_SCANCODE.

只是为遇到此问题的任何人拼写出来。我添加了KEYEVENTF_UNICODE 并删除了KEYEVENTF_SCANCODE。

KEYEVENTF_UNICODE 0x0004 If specified, the system synthesizes a VK_PACKET keystroke. The wVk parameter must be zero. This flag can only be combined with the KEYEVENTF_KEYUP flag. For more information, see the Remarks section.

KEYEVENTF_UNICODE 0x0004 如果指定,系统将合成 VK_PACKET 击键。wVk 参数必须为零。此标志只能与 KEYEVENTF_KEYUP 标志结合使用。有关更多信息,请参阅备注部分。

-MSDN

- MSDN

The sample should output "aa".

样本应该输出“aa”。

#include <windows.h>
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
    INPUT input[4]; 

    input[0].type = INPUT_KEYBOARD; 
    input[0].ki.wVk = 0; 
    input[0].ki.wScan = L'a'; 
    input[0].ki.dwFlags = KEYEVENTF_UNICODE ; 
    input[0].ki.time = 0; 
    input[0].ki.dwExtraInfo = 0; 

    input[1].type = INPUT_KEYBOARD; 
    input[1].ki.wVk = 0; 
    input[1].ki.wScan = L'a'; 
    input[1].ki.dwFlags = KEYEVENTF_KEYUP  | KEYEVENTF_UNICODE ; 
    input[1].ki.time = 0; 
    input[1].ki.dwExtraInfo = 0; 

    input[2].type = INPUT_KEYBOARD; 
    input[2].ki.wVk = 0; 
    input[2].ki.wScan = L'a'; 
    input[2].ki.dwFlags = KEYEVENTF_UNICODE ; 
    input[2].ki.time = 0; 
    input[2].ki.dwExtraInfo = 0; 

    input[3].type = INPUT_KEYBOARD; 
    input[3].ki.wVk = 0; 
    input[3].ki.wScan = L'a'; 
    input[3].ki.dwFlags = KEYEVENTF_KEYUP | KEYEVENTF_UNICODE ; 
    input[3].ki.time = 0; 
    input[3].ki.dwExtraInfo = 0; 

     SetConsoleTitle(L"TESTING");
     ShowWindow(FindWindow(NULL, L"TESTING"),SW_MINIMIZE );

    int retval = SendInput(4, input, sizeof(INPUT)); 
    if(retval > 0) 
    { 
      _tprintf(_T("SendInput sent %i"), retval); 
    } 
    else 
    { 
      _tprintf(_T("Unable to send input commands. Error is: %i"), GetLastError()); 
    } 



    return 0;
}