2

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_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());
}

2 Answers 2

3

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)

1
  • I changed the flag to KEYEVENTF_UNICODE and it worked with the scancode. The up event isn't required actually though the example I showed had it.
    – asmw
    Mar 2, 2010 at 23:25
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.