关于Windows下模拟Shift+END(功能键)无效问题

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

我用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

以及模拟按键shift+end时的消息信息:

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

通过对比可以看出是ScanCode和fExtended不同,据此做以下修改就可以使shift+end正常工作了:

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); 
另外如果用SendInput方法,则设置ki.dwFlags 
down为0|KEYEVENTF_EXTENDEDKEY,up为KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY
    
ki.wScan可为MapVirtualKey(VK_SHIFT, 0);

你可能感兴趣的:(shift,shift+end,shift+功能键,c++,qt)