我如何可以挂钩globaly所有键和所有的鼠标按键? [英] How can i hook globaly all the keys and all the mouse buttons?
问题描述
我在Form1的顶部做的:
I did in my Form1 top :
using Gma.UserActivityMonitor;
随后宣布:
Then declared :
Gma.UserActivityMonitor.GlobalEventProvider actHook = new GlobalEventProvider();
然后在Load事件我所做的:
Then in the Load event i did :
actHook.MouseClick += actHook_MouseClick;
然后在底部:
Then in the bottom :
private void actHook_MouseClick(object sender, MouseEventArgs e)
{
if (e.Clicks > 0)
{
MessageBox.Show("hi");
}
}
但一旦我之后才作罢第二次运行我的应用程序装我得到一个异常的文件:HookManager.Callbacks.cs
But once i run my application after a second before anything loaded i'm getting an exception on the file : HookManager.Callbacks.cs
在行:
throw new Win32Exception(errorCode);
例外:指定的模块找不到
The exception : The specified module could not be found
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:
什么或模块找不到
What or which module couldn't be found ?
我下载的版本2.
的源代码,并从所有文件添加的.cs:GMA硬盘.UserActivityMonitor目录。
I downloaded the source code of Version 2. And added all the files .cs from the : Gma.UserActivityMonitor directory on hard disk.
我没有得到的东西缺失的错误使那是什么?
I'm not getting errors that something is missing so what can it be ?
推荐答案
我只是下载的文件从 GlobalMouseKeyHook CodePlex上网站并提取然后我加入了 MouseKeyboardActivityMonitor.dll
我的项目。然后,我添加了使用引用的形式。我在运行Visual Studio 2010专业版面向.NET 4客户端配置文件。我有没有问题。
I just downloaded the file from the GlobalMouseKeyHook CodePlex site and extracted it then I added the MouseKeyboardActivityMonitor.dll
to my Project. Then I added the Using References to the Form. I am running Visual Studio 2010 Pro targeting the .Net 4 Client profile. I have had no problems
这是工作示例(此代码是从GlobalMouseKeyHook演示项目中修改:
This is a Working example( This Code was modified from GlobalMouseKeyHook Demo project:
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";
}
}
}
这篇关于我如何可以挂钩globaly所有键和所有的鼠标按键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!