开发者

SendInput (C++) is not working

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.

INPUT input[4];

input[0].type = INPUT_KEY开发者_Go百科BOARD;
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.
To send a KeyUp event, set dwFlags to KEYEVENTF_KEYUP.

Also, you need to use wVk instead of wScan. (wScan is only used with KEYEVENTF_UNICODE)


Just to spell it out for whomever comes across this. I added KEYEVENTF_UNICODE and removed 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.

-MSDN

The sample should output "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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜