C ++键盘挂钩CTRL键被卡住

编程入门 行业动态 更新时间:2024-10-22 22:56:29
本文介绍了C ++键盘挂钩CTRL键被卡住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在将Windows 10机器上的 ctrl + c 和 ctrl + v 重写为添加一些其他功能.

I'm looking to rewrite ctrl+c and ctrl+v on my Windows 10 machine to add some additional functionality.

我能够正确地复制和粘贴,并在按下这些键后成功创建了一个键盘挂钩来执行我的代码,但是当我的程序在按下 ctrl 后出现问题在运行时, ctrl 会像被按住一样连续运行.即使我完全终止了程序,在我完全注销计算机之前, ctrl 仍会像被按住一样继续工作.我该怎么做才能纠正这个问题?

I'm able to copy and paste correctly and have successfully created a keyboard hook to execute my code after these keys are pressed but I'm having an issue after I press ctrl while my program is running, ctrl continuously acts as if it's held down. Even after I terminate the program entirely, ctrl continues to act as if it's being held down until I logout of the computer entirely. What can I do to correct this?

谢谢!

经过一番混乱之后,我可以得出结论,任何键都卡住了. Shift和Caps锁定也会卡住.

after doing a bit of messing around, I can conclude any key gets stuck. Shift and caps lock get stuck as well.

#include <Windows.h> #include <stdio.h> #include <queue> using namespace std; LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam; if (wParam == WM_KEYDOWN) { if (p->vkCode == 0x43 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-c is pressed WM_COPY; } else if (p->vkCode == 0x56 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-v is pressed OpenClipboard(NULL); char* buffer; buffer = (char*)GetClipboardData(CF_TEXT); CloseClipboard(); cout << buffer; } return CallNextHookEx(NULL, nCode, wParam, lParam); } } int main() { HHOOK keyBoard = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, NULL, NULL); MSG msg; while (!GetMessage(&msg, NULL, NULL, NULL)) { TranslateMessage(&msg); DispatchMessage(&msg); } UnhookWindowsHookEx(keyBoard); }

推荐答案

请勿将return CallNextHookEx(NULL, nCode, wParam, lParam)放在if (wParam == WM_KEYDOWN)中.

修改:

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam; if (wParam == WM_KEYDOWN) { if (p->vkCode == 0x43 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-c is pressed WM_COPY; } else if (p->vkCode == 0x56 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-v is pressed OpenClipboard(NULL); char* buffer; buffer = (char*)GetClipboardData(CF_TEXT); CloseClipboard(); cout << buffer; } } return CallNextHookEx(NULL, nCode, wParam, lParam); }

更多推荐

C ++键盘挂钩CTRL键被卡住

本文发布于:2023-11-23 18:36:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622478.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:键盘   CTRL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!