如果我的程序不在前台,如何检测鼠标滚动事件 [英] How to detect mouse scroll events if my program is not in foreground
问题描述
我写了一个程序,在游戏中渲染叠加,显示一些额外的信息。我可以通过使用GetKeyState检测键盘和一些鼠标按键,但没有虚拟键代码向上和向下滚动,我也想使用。
I wrote a program which render overlay in game, showing some additional info. I can detect keyboard and some of mouse keypresses by using GetKeyState, however there is no Virtual Key Code for scroll up and down which I would like to use as well.
我知道滚动是以事件的方式处理,而不是按键,但这不是真正的帮助。
I know that scroll is handled more in a way of an event, rather than keypress, but that doesn't really help. So is there any solutions for my problem?
我想到的事情:
- 通过某些功能检测滚动事件?
- 以某种方式在我的程序中将Windows消息发送到游戏中(称为Hooking IIRC)
我使用Visual Studio 2013 Express [C ++]
I'm using Visual Studio 2013 Express [C++]
推荐答案
我使用下面的代码解决了这个问题。
I solved the issue by using following code.
LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
using namespace MouseLog;
MSLLHOOKSTRUCT * pMouseStruct = (MSLLHOOKSTRUCT *)lParam;
if (pMouseStruct != NULL)
{
if (wParam == WM_MOUSEWHEEL)
{
if (HIWORD(pMouseStruct->mouseData) == 120) MScrollUp = 1;
else MScrollDown = 1;
}
if (wParam == WM_MBUTTONDOWN)
MScrollBtn = 1;
//printf("Mouse position X = %d Mouse Position Y = %d\n", pMouseStruct->pt.x, pMouseStruct->pt.y);
}
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}
DWORD WINAPI MouseLogger(LPVOID lpParm)
{
HINSTANCE hInstance = GetModuleHandle(NULL);
hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, hInstance, NULL);
MSG message;
while (GetMessage(&message, NULL, 0, 0)) {
TranslateMessage(&message);
DispatchMessage(&message);
}
UnhookWindowsHookEx(hMouseHook);
return 0;
}
要启动MouseLogger,请使用CreateThread()。
For starting MouseLogger use CreateThread().
这篇关于如果我的程序不在前台,如何检测鼠标滚动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!