简体   繁体   中英

how to simulate Ctrl + V using keybd_event () in C++

i want to paste text from clipboard to some program's textbox. so i tried to use keybd_event.

keybd_event(VK_CONTROL,0x1D, 0, 0);
keybd_event('V', 0x2F, 0, 0);
keybd_event('V', 0x2F, KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, 0x1D, KEYEVENTF_KEYUP, 0);

but this is not executed. so i tried different way

keybd_event(VK_CONTROL,0x1D,  KEYEVENTF_EXTENDEDKEY, 0);
keybd_event('V', 0x2F, 0, 0);
keybd_event('V', 0x2F, KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, 0x1D, KEYEVENTF_KEYUP, 0);

so this work well. However, since then, all keyboard inputs have been entered with the ctrl key pressed. maybe i think key-up message is not worked

how to solve this problem?

However, since then, all keyboard inputs have been entered with the ctrl key pressed. maybe i think key-up message is not worked

You simulate right CONTROL WM_KEYDOWN but left CONTROL WM_KEYUP . So the right-hand CTRL key has not been released.

The following code will work:

keybd_event(VK_CONTROL, 0x1D, KEYEVENTF_EXTENDEDKEY | 0, 0);
keybd_event('V', 0x2F, KEYEVENTF_EXTENDEDKEY | 0, 0);
keybd_event('V', 0x2F, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, 0x1D, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);

extended-key flag ( KEYEVENTF_EXTENDEDKEY )

Indicates whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.

Refer to WM_KEYUP message .

keybd_event function has been superseded. Use SendInput instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM