简体   繁体   English

SendInput(C ++)无法正常工作

[英]SendInput (C++) is not working

The return value is 4 and I'm running Visual Studio in administrative mode so permissions should be ok. 返回值为4,并且我在管理模式下运行Visual Studio,因此权限应该确定。 I don't see anything typed out though. 我看不到任何内容。 Any help? 有什么帮助吗? I'm using Windows 7 x64. 我正在使用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());
}

You need to send both KeyDown and KeyUp events for each key. 您需要为每个键发送KeyDown和KeyUp事件。
To send a KeyUp event, set dwFlags to KEYEVENTF_KEYUP . 要发送KeyUp事件,请将dwFlags设置为KEYEVENTF_KEYUP

Also, you need to use wVk instead of wScan . 另外,您需要使用wVk而不是wScan ( wScan is only used with KEYEVENTF_UNICODE ) wScan仅与KEYEVENTF_UNICODE

Just to spell it out for whomever comes across this. 只是为遇到这个问题的任何人说明一下。 I added KEYEVENTF_UNICODE and removed KEYEVENTF_SCANCODE. 添加了 KEYEVENTF_UNICODE并删除了 KEYEVENTF_SCANCODE。

KEYEVENTF_UNICODE 0x0004 If specified, the system synthesizes a VK_PACKET keystroke. KEYEVENTF_UNICODE 0x0004如果指定,系统将合成VK_PACKET击键。 The wVk parameter must be zero. wVk参数必须为零。 This flag can only be combined with the KEYEVENTF_KEYUP flag. 该标志只能与KEYEVENTF_KEYUP标志结合使用。 For more information, see the Remarks section. 有关更多信息,请参见“备注”部分。

- 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM