5

我使用 keybd_event 函数来模拟 SHIFT+END 组合键,但它不起作用:

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);

插入符号移动到行尾,但不要选择文本(突出显示)。就像输入一个 END 键一样。有人可以帮忙吗?非常感谢~


我已经解决了这个问题!我使用 spy++ 来查找当我使用键盘发送键 Shift + End 时会发生什么,我收到了以下消息:

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

当我运行上面的代码时,我收到了这些消息:

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

窗口在 VK_END 的 WM_KEYDOWN 消息之前获得 VK_SHIFT 的 WM_KEYUP 消息。消息之间的区别在于“fExtended”的值。当我使用键盘时,END键的fExtended值为1,而当我运行代码时值为0。所以我尝试将fExtended值设置为1,我发现keybd_event函数的“dwFlags [in]”参数. 当我按如下方式运行代码时,组合键有效:

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); 
4

2 回答 2

0
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);
于 2013-11-12T15:58:29.983 回答
0

使用 shift VK_LSHIFT 和 VK_RSHIFT down 然后 VK_END 然后同时向左和向右 SHIFT UP 对我有用

于 2020-01-31T18:49:41.540 回答