如何全局挂钩所有键和所有鼠标按钮?

时间:2013-05-27 23:50:54

标签: c# winforms

我在Form1上面做过:

using Gma.UserActivityMonitor;

然后声明:

Gma.UserActivityMonitor.GlobalEventProvider actHook = new GlobalEventProvider();

然后在Load事件中我做了:

actHook.MouseClick += actHook_MouseClick;

然后在底部:

private void actHook_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Clicks > 0)
            {
                MessageBox.Show("hi");
            }
        }

但是一旦我在加载任何内容之后运行我的应用程序,我就会在文件上遇到异常:HookManager.Callbacks.cs

在线:

throw new Win32Exception(errorCode);

例外:找不到指定的模块

System.ComponentModel.Win32Exception was unhandled
  HResult=-2147467259
  Message=The specified module could not be found
  Source=ScreenVideoRecorder
  ErrorCode=-2147467259
  NativeErrorCode=126
  StackTrace:
       at Gma.UserActivityMonitor.HookManager.EnsureSubscribedToGlobalMouseEvents() in d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorderWorkingVersion\HookManager.Callbacks.cs:line 236
       at Gma.UserActivityMonitor.HookManager.add_MouseClick(MouseEventHandler value) in d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorderWorkingVersion\HookManager.cs:line 69
       at Gma.UserActivityMonitor.GlobalEventProvider.add_MouseClick(MouseEventHandler value) in d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorderWorkingVersion\GlobalEventProvider.cs:line 71
       at ScreenVideoRecorder.Form1.Form1_Load(Object sender, EventArgs e) in d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorderWorkingVersion\Form1.cs:line 47
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at System.Windows.Forms.Form.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  InnerException: 

找不到哪个或哪个模块?

我下载了第2版的源代码。 并从硬盘上的:Gma.UserActivityMonitor目录添加了所有文件.cs。

我没有得到错误的东西,所以它可以是什么?

1 个答案:

答案 0 :(得分:4)

我只是来自downloaded the file网站的GlobalMouseKeyHook CodePlex并将其解压缩然后我将MouseKeyboardActivityMonitor.dll添加到我的项目中。然后我添加了使用引用到表单。我正在运行针对.Net 4客户端配置文件的Visual Studio 2010 Pro。我没有遇到任何问题

这是一个工作示例(此代码已从GlobalMouseKeyHook演示项目修改:

using System;
using System.Collections.Generic;  
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MouseKeyboardActivityMonitor;
using MouseKeyboardActivityMonitor.WinApi;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        private readonly KeyboardHookListener m_KeyboardHookManager;
        private readonly MouseHookListener m_MouseHookManager;
        public Form1()
        {
            InitializeComponent();
            m_KeyboardHookManager = new KeyboardHookListener(new GlobalHooker());
            m_KeyboardHookManager.Enabled = true;
            m_KeyboardHookManager.KeyDown += HookManager_KeyDown;
            m_KeyboardHookManager.KeyUp += HookManager_KeyUp;



            m_MouseHookManager = new MouseHookListener(new GlobalHooker());
            m_MouseHookManager.Enabled = true;
            m_MouseHookManager.MouseDown += HookManager_MouseDown;
            m_MouseHookManager.MouseUp += HookManager_MouseUp;

        }

        private void HookManager_KeyDown(object sender, KeyEventArgs e)
        {
            label1.Text = e.KeyData.ToString() + " Pressed";
        }

        private void HookManager_KeyUp(object sender, KeyEventArgs e)
        {
            label1.Text = e.KeyData.ToString() + " Released";
        }

        private void HookManager_MouseUp(object sender, MouseEventArgs e)
        {
            label1.Text = e.Button.ToString() + " Released";
        }


        private void HookManager_MouseDown(object sender, MouseEventArgs e)
        {
            label1.Text = e.Button.ToString() + " Pressed";
        }

    }
}