5
votes

I use keybd_event function to simulate SHIFT+END key combination but it doesn't work:

keybd_event(VK_SHIFT,0,0,0);
keybd_event(VK_END,0,0,0);
keybd_event(VK_END,0,KEYEVENTF_KEYUP,0);
keybd_event(VK_SHIFT,0,KEYEVENTF_KEYUP,0);

The caret is move to the end of the line, but do not select the text(Highlight). Just like input a single END key. Someone can help? Thanks very much~


I have solved this problem! I used spy++ to find what happen when I use the keyboard to send the key Shift + End, and I got these messages:

WM_KEYDOWN nVirtKey:VK_SHIFT cRepeat:1 ScanCode:2A fExtended:0 fAltDown:0 fRepeat:0 fUp:0
WM_KEYDOWN nVirtKey:VK_END cRepeat:1 ScanCode:4F fExtended:1 fAltDown:0 fRepeat:0 fUp:0
WM_KEYUP nVirtKey:VK_END cRepeat:1 ScanCode:4F fExtended:1 fAltDown:0 fRepeat:1 fUp:1
WM_KEYUP nVirtKey:VK_SHIFT cRepeat:1 ScanCode:2A fExtended:0 fAltDown:0 fRepeat:1 fUp:1

and when I run the code above, I got these messages:

WM_KEYDOWN nVirtKey:VK_SHIFT cRepeat:1 ScanCode:0 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
WM_KEYUP nVirtKey:VK_SHIFT cRepeat:1 ScanCode:0 fExtended:0 fAltDown:0 fRepeat:1 fUp:1
WM_KEYDOWN nVirtKey:VK_END cRepeat:1 ScanCode:0 fExtended:1 fAltDown:0 fRepeat:0 fUp:0
WM_KEYUP nVirtKey:VK_END cRepeat:1 ScanCode:0 fExtended:1 fAltDown:0 fRepeat:1 fUp:1
WM_KEYDOWN nVirtKey:VK_SHIFT cRepeat:1 ScanCode:0 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
WM_KEYUP nVirtKey:VK_SHIFT cRepeat:1 ScanCode:0 fExtended:0 fAltDown:0 fRepeat:1 fUp:1

the window get a VK_SHIFT's WM_KEYUP message before a VK_END's WM_KEYDOWN message. The different between the message is the value of "fExtended". when I used the keyboard, the END key's fExtended value is 1, and when I run the code the value is 0. So I try to set the fExtended value to 1, and I found the "dwFlags [in]" parameter of keybd_event function. The key combination works when I run the code as follow:

keybd_event(VK_SHIFT,0x2A,0,0);
keybd_event(VK_END,0x4F,KEYEVENTF_EXTENDEDKEY | 0,0);
keybd_event(VK_END,0x4F,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
keybd_event(VK_SHIFT,0x2A,KEYEVENTF_KEYUP,0); 
2

2 Answers

0
votes
UINT m_scanShift = MapVirtualKey(VK_SHIFT, 0);
UINT m_scanEnd   = MapVirtualKey(VK_END, 0);

keybd_event(VK_SHIFT, m_scanShift, 0, 0);
Sleep(10);
keybd_event(VK_END, m_scanEnd, 0, 0);
Sleep(10);
keybd_event(VK_END, m_scanEnd, KEYEVENTF_KEYUP, 0);
Sleep(10);
keybd_event(VK_SHIFT, m_scanShift, KEYEVENTF_KEYUP, 0);
0
votes

Use both shift VK_LSHIFT and VK_RSHIFT down then VK_END and then both SHIFT UP left and right at the same time will work for me