如何检测鼠标的点击? [英] How to detect mouse clicks?

查看:380
本文介绍了如何检测鼠标的点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测鼠标点击在Windows? (XP / Vista / 7的)。 因为当我的应用程序正在运行的例子,它会检测用户是否点击的东西(而不是在该应用程序的用户界面,但在Windows UI)。如果是的话,执行另一个进程。

how can i detect mouse clicks on Windows ? (XP/Vista/7). for example when my application is running , it will detect if user click on something (not on that application UI but on Windows UI). if yes , execute another process.

我不知道这是可能的,我希望有人可以给我一些指导。

I don't know if this is possible , i am hoping someone can give me some guidance.

谢谢!

推荐答案

您需要编写一个鼠标钩子,如果你想拦截任何鼠标点击,移动,鼠标滚轮点击...等。

You need to write a mouse hook if you want to intercept any mouse clicks, movement, mouse wheel clicks...etc.

这是AFAIK,如果你希望在自己的应用程序之外跟踪鼠标活动的唯一途径。您需要从User32.dll文件导入调用SetWindowsHookEx(...)的功能,如果你想安装一个挂钩。它涉及到互操作(PInvoke的),你就必须导入(的DllImport)的一些功能。

This is the only way AFAIK if you want to track mouse activity outside of your own application. You need to import the SetWindowsHookEx(...) function from the User32.dll file if you want to install a hook. It involves interop (PInvoke) and you'll have to import (DllImport) some functions.

下面是微软如何在C#中实现这一点的正式文件:

Here's an official document by Microsoft on how to achieve this in C#:

如何在Visual C设置Windows钩子#.NET

我来总结一下在这里,只要是完整的答案应该链接死去的一天。

I'll summarize it here, just to be complete the answer should the link die one day.

开始与SetWindowsHookEx函数:

Starting with the SetWindowsHookEx function:

[DllImport("user32.dll",CharSet=CharSet.Auto,
 CallingConvention=CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, 
IntPtr hInstance, int threadId);

现在你可以设置你的钩子。例如:

Now you can setup your hook. For example:

public class Form1
{
    static int hHook = 0;
    public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
    HookProc MouseHookProcedure;

    private void ActivateMouseHook_Click(object sender, System.EventArgs e)
    {
        if(hHook == 0)
        {
            MouseHookProcedure = new HookProc(Form1.MouseHookProc);
            hHook = SetWindowsHookEx(WH_MOUSE, 
                             MouseHookProcedure,
                             (IntPtr) 0,
                             AppDomain.GetCurrentThreadId());
        }
    }
}

不要忘了之后解开它。你需要另外的DllImport此:

Don't forget to unhook it afterwards. You'll need another DllImport for this:

[DllImport("user32.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);

private void DeactivateMouseHook_Click(object sender, System.EventArgs e)
{
    bool ret = UnhookWindowsHookEx(hHook);
}    

您可以使用HOOKPROC委托(MouseHookProcedure)来捕捉鼠标的活动。这涉及到一些编组以捕捉数据

You can use the HookProc delegate (MouseHookProcedure) to capture the mouse activity. This involves some marshalling in order to capture the data.

[StructLayout(LayoutKind.Sequential)]
public class POINT 
{
    public int x;
    public int y;
}

[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct 
{
    public POINT pt;
    public int hwnd;
    public int wHitTestCode;
    public int dwExtraInfo;
}

public static int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{          
    MouseHookStruct MyMouseHookStruct = (MouseHookStruct)
        Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));

    // You can get the coördinates using the MyMouseHookStruct.
    // ...       
    return CallNextHookEx(hHook, nCode, wParam, lParam); 
}

不要忘了调用挂钩链之后(CallNextHookEx方法)的下一个项目!

Don't forget to call the next item in the hook chain afterwards (CallNextHookEx)!

[DllImport("user32.dll",CharSet=CharSet.Auto,      
 CallingConvention=CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, 
IntPtr wParam, IntPtr lParam);

PS:您可以为键盘做同样的

PS: You can do the same for the keyboard.

这篇关于如何检测鼠标的点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆