SendInput() for keyboard - only lowercase

半世苍凉 提交于 2019-11-29 07:19:51
tobi

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!