简体   繁体   English

当用户按下 Ctrl 并单击时获取鼠标坐标

[英]get mouse coordinates when user press Ctrl and click

I want to get mouse click coordinates X and Y when user press Ctrl and click at same time.当用户按下Ctrl并同时单击时,我想获得鼠标单击坐标 X 和 Y。 User may click anywhere on the screen or programs.用户可以单击屏幕或程序上的任意位置。 I want my program to catch the event and get coordinates when Ctrl key is down pressed and mouse click occurs at same time.我希望我的程序在按下 Ctrl键并同时发生鼠标单击时捕获事件并获取坐标。 I want to get system coordinates X and Y, not the window coordinates of other programs.我想获得系统坐标 X 和 Y,而不是其他程序的窗口坐标。 I'm using C++ .我正在使用 C++ 。

How to do that ?怎么做?

Windows OS, WIN API code Windows 操作系统,WIN API 代码

I'm doing next which is not working:我正在做的下一个不起作用:

HHOOK MouseHook;
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam){

        PKBDLLHOOKSTRUCT k = (PKBDLLHOOKSTRUCT)(lParam);
        POINT p;


        if(wParam == WM_RBUTTONDOWN)
        {
          // right button clicked
          GetCursorPos(&p);
          //p.x 
          //p.y
          //my program is never getting here, why ?
        }

}
 MouseHook = SetWindowsHookEx(WH_MOUSE_LL,(HOOKPROC)MouseHookProc,0,0);

if I change the above line to: MouseHook = SetWindowsHookEx(WH_MOUSE,(HOOKPROC)MouseHookProc,GetModuleHandle(NULL),0);如果我将上面的行更改为: MouseHook = SetWindowsHookEx(WH_MOUSE,(HOOKPROC)MouseHookProc,GetModuleHandle(NULL),0); then it will work only for my own program window, but not hooking clicks outside of my program那么它只适用于我自己的程序窗口,而不是在我的程序之外挂钩

The way I would approach is I would create a global variable say我的方法是创建一个全局变量说

int ctrl_pressed = 0 
/* 
0 -- ctrl nt pressed
1 -- Crtl Pressed
*/

Step 1: Now I would handle both WM_KEYDOWN and WM_KEYUP for ctrl第 1 步:现在我将为 ctrl 处理 WM_KEYDOWN 和 WM_KEYUP

Step 2: In the callback of WM_KEYDOWN (if lparam is ctrl ) I would set ctrl_pressed to 1. And in case of WM_KEYUP I would set ctrl_pressed to 0.第 2 步:在WM_KEYDOWN (if lparam is ctrl )的回调中WM_KEYDOWN (if lparam is ctrl )我会将 ctrl_pressed 设置为 1。如果是WM_KEYUP我会将ctrl_pressed to 0.设置ctrl_pressed to 0.

Step 3: Now Finally ill handle WM_MOUSECLICKED and then check if ctrl_pressed(global) is 1 , which if true I can just get the coordinates.第 3 步:现在终于处理WM_MOUSECLICKED ,然后检查ctrl_pressed(global) is 1 ,如果为真,我就可以获取坐标。

This is just an approach may be not the most efficient solution.这只是一种方法,可能不是最有效的解决方案。 Wait for more experienced WIN32 Developers to give their input on this.等待更有经验的 WIN32 开发人员对此提出意见。

Have you read this article?你读过这篇文章吗? link 链接

First of all, if you want to catch system entire mouse + keyboard event, your hooking function must be placed on a dll.首先,如果你想捕获系统整个鼠标+键盘事件,你的钩子函数必须放在一个dll上。

1)If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx. 1) 如果 nCode 小于零,钩子过程必须返回 CallNextHookEx 返回的值。 Like below,像下面,

if(nCode < 0)
{ 
   CallNextHookEx(hook, nCode, wParam, lParam);
   return 0;
} 

If CallNextHookEx is not called, there is a chance that other processes that have installed hook may not get the events correctly.如果未调用CallNextHookEx ,则其他安装了 hook 的进程可能无法正确获取事件。

2)And then, check nCode again, whether it is HC_ACTION . 2)然后,再次检查nCode ,是否是HC_ACTION

switch (nCode) 
{ 
  case HC_ACTION:
     ... 

3)Finally, you can check WPARAM for WM_RBUTTONDOWN , and LPARAM for MSLLHOOKSTRUCT 3)最后,您可以检查WPARAMWM_RBUTTONDOWNLPARAMMSLLHOOKSTRUCT

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

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