gpt4 book ai didi

c++ - SetWindowsHookEx WH_KEYBOARD_LL 右移不响应

转载 作者:可可西里 更新时间:2023-11-01 14:18:44 27 4
gpt4 key购买 nike

我尝试在 c++ 中使用 Windows API,但 SetWindowsHookEx WH_KEYBOARD_LL 似乎没有从正确的 Shift 键(< kbd>Shift 键位于 qwerty 键盘右侧,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”字符串。有人知道吗???

附言:

如果我删除带有“msg += ((st_hook.flags & LLKHF_EXTENDED) << 24);”的行-> “RIGHT SHIFT”现在确实出现了,但是当按下“Windows 键”时,未定义出现了

最佳答案

左移与右移显示在 KBDLLHOOKSTRUCTvkCode 字段中。您正在使用扫描码的 key 名称;右 Shift 键名为“Shift”,就像它在键盘上显示的那样。

显然,右移以设置扩展标志结束,这导致 GetKeyNameText 在错误的表中查找。删除扩展标志以“右移”键名称结束。

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

关于c++ - SetWindowsHookEx WH_KEYBOARD_LL 右移不响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18900831/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com