7

I have following code:

INPUT Input = { 0 };
Input.type       = INPUT_KEYBOARD;
Input.mi.dwFlags = KEYEVENTF_EXTENDEDKEY;
Input.ki.wVk   = 'A'; // tried 0x41, ( UCHAR )VkKeyScan( 'A' )
SendInput( 1, &Input, sizeof( INPUT ) );

but it generates me only 'a'. How to force it to generate upper case as well?

Thanks.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
tobi
  • 1,944
  • 6
  • 29
  • 48
  • 6
    You have to simulate holding a shift key, just like you would when typing – InspiredBy Jul 11 '12 at 17:28
  • 1
    As Shenaniganz hinted at, you are simulating a key press. Do you have an upper-case "A" key _and_ a lower-case "a" key on your keyboard? – TheZ Jul 11 '12 at 17:29
  • 1
    `INPUT` type contains a nameelss union, so you really should not assign to `mi` and `ki` members of the same variable. – rodrigo Jul 11 '12 at 19:44
  • oh my! There was a bug. You should have answered the question and get all of these votes! thanks – tobi Jul 11 '12 at 19:56
  • @tobi: Upvotes are nice, but this bug was not likely the cause of your problems, so technically it should not be an answer. – rodrigo Jul 11 '12 at 21:29
  • @rodrigo I confused this topic with my another one xD http://stackoverflow.com/questions/11438677/sendinput-for-keyboard-only-uppercase/11439873#11439873 – tobi Jul 12 '12 at 07:13
  • Take a look at [SetKeyboardState](http://msdn.microsoft.com/de-ch/library/windows/desktop/ms646314%28v=vs.85%29.aspx). I have used it in conjuction with PostMessage and WM_KEYDOWN in [this answer](http://stackoverflow.com/a/16255097/385995). – Mike Fuchs Apr 27 '13 at 20:09

1 Answers1

9

EDIT: some modifications according to rodrigo answer in comments.

INPUT Input = { 0 };
// shift key down
Input.type       = INPUT_KEYBOARD;
Input.ki.wVk   = VK_LSHIFT; 
SendInput( 1, &Input, sizeof( INPUT ) );

// 'a' key down
Input.type       = INPUT_KEYBOARD;
Input.ki.wVk   = 'A';
SendInput( 1, &Input, sizeof( INPUT ) );

// 'a' key release
Input.type       = INPUT_KEYBOARD;
Input.ki.dwFlags = KEYEVENTF_KEYUP;
Input.ki.wVk   = 'A';
SendInput( 1, &Input, sizeof( INPUT ) );

// shift key release
Input.type       = INPUT_KEYBOARD;
Input.ki.dwFlags = KEYEVENTF_KEYUP;
Input.ki.wVk   = VK_LSHIFT; 
SendInput( 1, &Input, sizeof( INPUT ) );

EDIT: here is another code with an example of turning caps/shift off after sending letter:

INPUT Event = { 0 };

const SHORT key = VkKeyScan('a');
const UINT mappedKey = MapVirtualKey( LOBYTE( key ), 0 );

// Press shift key
Event.type = INPUT_KEYBOARD;
Event.ki.dwFlags = KEYEVENTF_SCANCODE;
Event.ki.wScan = MapVirtualKey( VK_LSHIFT, 0 );
SendInput( 1, &Event, sizeof( Event ) );

// upper case 'A'
Event.type = INPUT_KEYBOARD;
Event.ki.dwFlags = KEYEVENTF_SCANCODE;
Event.ki.wScan = mappedKey;
SendInput( 1, &Event, sizeof( Event ) );

// release upper case 'A'
Event.type = INPUT_KEYBOARD;
Event.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
Event.ki.wScan = mappedKey;
SendInput( 1, &Event, sizeof( Event ) );

// Release shift key
Event.type = INPUT_KEYBOARD;
Event.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
Event.ki.wScan = MapVirtualKey( VK_LSHIFT, 0 );
SendInput( 1, &Event, sizeof( Event ) );

const SHORT key1 = VkKeyScan('A');
const UINT mappedKey1 = MapVirtualKey( LOBYTE( key1 ), 0 );

// lower case 'a'
Event.type = INPUT_KEYBOARD;
Event.ki.dwFlags = KEYEVENTF_SCANCODE;
Event.ki.wScan = mappedKey1;
SendInput( 1, &Event, sizeof( Event ) );

// release lower case 'a'
Event.type = INPUT_KEYBOARD;
Event.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
Event.ki.wScan = mappedKey1;
SendInput( 1, &Event, sizeof( Event ) );

explanation here (I hope the explanation is correct): SendInput() for keyboard - only uppercase

Community
  • 1
  • 1
tobi
  • 1,944
  • 6
  • 29
  • 48