繁体   English   中英

SetWindowsHookEx WH_KEYBOARD_LL在右移时未响应

[英]SetWindowsHookEx WH_KEYBOARD_LL not responding on right shift

我尝试在c ++中使用Windows API,并且SetWindowsHookEx WH_KEYBOARD_LL似乎没有从右Shift键(qwerty键盘右侧的Shift键在Enter键下方)获取事件。 它确实与左Shift键一起使用。 如何解决此问题???

#include "stdafx.h"
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <windows.h>
#include <string> 
#include <shlobj.h>
#include <Shlwapi.h>
#include <stdio.h>
#include <aclapi.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <future>
#include <stdlib.h>
#include <random>
#include <ctime>
#include <time.h>       
#include <Lmcons.h>



HHOOK   kbdhook;    /* Keyboard hook handle */
bool    running;    /* Used in main loop */


__declspec(dllexport) LRESULT CALLBACK handlekeys(int code, WPARAM wp, LPARAM lp)
{
        static bool capslock = false;
        static bool shift = false;
        char tmp[0xFF] = {0};
        std::string str;
        DWORD msg = 1;
        KBDLLHOOKSTRUCT st_hook = *((KBDLLHOOKSTRUCT*)lp);



        msg += (st_hook.scanCode << 16);
        msg += ((st_hook.flags & LLKHF_EXTENDED) << 24);
        GetKeyNameText(msg, tmp, 0xFF);
        str = std::string(tmp);


    if (code == HC_ACTION && (wp == WM_SYSKEYDOWN || wp == WM_KEYDOWN )) {
        MessageBox(NULL,str.c_str(),NULL,MB_OK);
}
return CallNextHookEx(kbdhook, code, wp, lp);
}

LRESULT CALLBACK windowprocedure(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch (msg) {
        case WM_CLOSE: case WM_DESTROY:
            running = false;
            break;
        default:
            /* Call default message handler */
            return DefWindowProc(hwnd, msg, wp, lp);
    }

    return 0;
}

int WINAPI WinMain(HINSTANCE thisinstance, HINSTANCE previnstance,
        LPSTR cmdline, int ncmdshow)
{


    HWND        hwnd;
    HWND        fgwindow = GetForegroundWindow(); 
    MSG     msg;
    WNDCLASSEX  windowclass;
    HINSTANCE   modulehandle;

    modulehandle = GetModuleHandle(NULL);
    kbdhook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)handlekeys, modulehandle, NULL);
    running = true;






    while (running) {

        if (!GetMessage(&msg, NULL, 0, 0))
            running = false; 
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

右移会在警报中显示空白字符串。 但是,向左移动会在警报中显示“ SHIFT”字符串。 任何人都有线索吗?

PS:

如果我用“ msg + =((st_hook.flags&LLKHF_EXTENDED)<< 24);”删除该行。 ->现在确实显示“ RIGHT SHIFT”,但是在按下“ Windows键”时显示未定义

左与右方向表示了在vkCode领域KBDLLHOOKSTRUCT 您使用的是扫描代码的键 就像键盘上的提示一样,右Shift键称为“ Shift”。

显然,右移以扩展标志集结尾,这会使GetKeyNameText在错误的表中查找。 删除扩展标志最终以键名“ right shift”结束。

    msg += (st_hook.scanCode << 16);
    if (st_hook.scanCode != 0x3a)
    {
        msg += ((st_hook.flags & LLKHF_EXTENDED) << 24);
    }
    GetKeyNameText(msg, tmp, 0xFF);

此解决方案不依赖特定的代码

if (st_hook.vkCode != VK_RSHIFT)
  msg += ((st_hook.flags & LLKHF_EXTENDED) << 24);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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