How to use SendInput?

So what does SendInput API do?

SendInput API is a helper function to simulate keyboard and mouse inputs. It’s an ideal function to insert characters into a password which otherwise is not possible. Here is what MSDN says about this function…

“The SendInput function inserts the events in the INPUT structures serially into the keyboard or mouse input stream. These events are not interspersed with other keyboard or mouse input events inserted either by the user (with the keyboard or mouse) or by calls to keybd_event, mouse_event, or other calls to SendInput.”

Here is a simple and basic function which sends a given text to a foreground window (my earlier function was crap so I replaced it with this new one that works for all texts, haven’t tried out with foreign keyboards though)…

[sourcecode language=”cpp”]BOOL SendText( LPCSTR lpctszText )
{
   vector< INPUT > EventQueue;

   char Buff[120] = {0};
   GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ILANGUAGE, Buff, sizeof(Buff));
   HKL hKeyboardLayout = ::LoadKeyboardLayout( Buff, KLF_ACTIVATE );

   const int Len = strlen( lpctszText );
   for( int Index = 0; Index &amp;amp;amp;lt; Len; ++Index )
   {
      INPUT Event = { 0 };

      const SHORT Vk = VkKeyScanEx(lpctszText[Index], hKeyboardLayout);
      const UINT VKey = ::MapVirtualKey( LOBYTE( Vk ), 0 );

      if( HIBYTE( Vk ) == 1 ) // Check if shift key needs to be pressed for this key
      {
          // Press shift key
          ::ZeroMemory( &amp;amp;amp;amp;Event, sizeof( Event ));
          Event.type = INPUT_KEYBOARD;
          Event.ki.dwFlags = KEYEVENTF_SCANCODE;
          Event.ki.wScan = ::MapVirtualKey( VK_LSHIFT, 0 );
          EventQueue.push_back( Event );
      }

      // Keydown
      ::ZeroMemory( &amp;amp;amp;amp;Event, sizeof( Event ));
      Event.type = INPUT_KEYBOARD;
      Event.ki.dwFlags = KEYEVENTF_SCANCODE;
      Event.ki.wScan = VKey;
      EventQueue.push_back( Event );

      // Keyup
      ::ZeroMemory( &amp;amp;amp;amp;Event, sizeof( Event ));
      Event.type = INPUT_KEYBOARD;
      Event.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
      Event.ki.wScan = VKey;
      EventQueue.push_back( Event );

      if( HIBYTE( Vk ) == 1 )// Release if previouly pressed
      {
           // Release shift key
          ::ZeroMemory( &amp;amp;amp;amp;Event, sizeof( Event ));
          Event.type = INPUT_KEYBOARD;
          Event.ki.dwFlags = KEYEVENTF_SCANCODE| KEYEVENTF_KEYUP;
          Event.ki.wScan = ::MapVirtualKey( VK_LSHIFT, 0 );
          EventQueue.push_back( Event );
      }
   }// End for

   if( hKeyboardLayout )
   {
       UnloadKeyboardLayout( hKeyboardLayout );
   }

   return ::SendInput( EventQueue.size(), &amp;amp;amp;amp;EventQueue[0], sizeof( INPUT ));
}[/sourcecode]

22 thoughts on “How to use SendInput?

  1. Doesn’t do capital letters and that is where I am having the difficulty with. Followed all sorts of code samples but the best I could manage to do is all caps or all lowercase but not a mixture.

    • Yes you are right. I’ll update the code to fix this. What you need to do simulate a shift key down and a shift key up after inserting an capital letter to event queue vector. Bit lazy on my part.

    • Do you happen to know why SendInput would work in some programs and not others since it is simulating an actual key press?

        • Trust me I did a lot of googling and I am not the only one having this issue. Seems really weird but I think, after looking at all those sites from Google, that DirectInput kills it. Right now I am trying to automate something in a game I play and lower case works but not ever uppercase. keybd_event nor SendInput work in it but both work for all other programs. keybd_event, after doing so much reading, is now just a wrapper for SendInput but I am just parroting what I read about that part.

          • Wanted to clarify it isn’t just uppercase it is any key that demand the shift. Seems really weird for it to not work when SendInput is supposed to be the lowest level method that inserts my keys at the hardware level.

          • Did that as a first recourse and that didn’t work either. What is funny is I can run all of this and hold down the shift key and it works 90% (some slip by).

          • I finally was able to get keybd_event to work and SendInput as well but what you can’t do is do the VK_SHIFT then char down and up then VK_SHIFT up. You have to delay after the shift down and before the shift up or no go.

          • Hello,

            Can you try again with this new function. My earlier function was full of bugs. Now I’ve replaced it with a new one. Now shift key is properly handled. Please let me know if it works.

          • I haven’t tried it but I can tell you it doesn’t handle shift characters and there is no need for so many SendInput statements (3 is all that you need).

          • I tried it and it did not work for shifting the chars (!@#$%, etc…) but did shift the capital letters.

  2. Very odd. My routine that works has a ton more code than yours just to get the special chars. No idea why that is either.

  3. Hi, i’m a complete newbie to the SendInput function. Could you please guide as to how am i suppose to use this snippet? How do I send characters to a application? Please do reply.

    • hi, i have a problem with this function..when i use keyboard_input it only works for either small or capital letters… no other special characters(printable) can be output on the notepad. However with the same unicode i can print on the program screen. But why is it not showing on the notepad ?

Appreciate your comments...