Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
664 views
in Technique[技术] by (71.8m points)

winapi - SendInput() for keyboard - only lowercase

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...