调用键盘鼠标钩子的方法

using myhook;

MouseHook mouseHook = new MouseHook();
KeyboardHook keyboardHook = new KeyboardHook();

#region [调用方法]

#region [查找焦点控件句柄]

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, string lpString);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
[DllImport("user32.dll")]
static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui);
[StructLayout(LayoutKind.Sequential)]
public struct GUITHREADINFO
{
public int cbSize;
public int flags;
public IntPtr hwndActive;
public IntPtr hwndFocus;
public IntPtr hwndCapture;
public IntPtr hwndMenuOwner;
public IntPtr hwndMoveSize;
public IntPtr hwndCaret;
public RECT rectCaret;
}

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
int left;
int top;
int right;
int bottom;
}

public GUITHREADINFO? GetGuiThreadInfo(IntPtr hwnd)
{
if (hwnd != IntPtr.Zero)
{
uint threadId = GetWindowThreadProcessId(hwnd, IntPtr.Zero);
GUITHREADINFO guiThreadInfo = new GUITHREADINFO();
guiThreadInfo.cbSize = Marshal.SizeOf(guiThreadInfo);
if (GetGUIThreadInfo(threadId, ref guiThreadInfo) == false)
return null;
return guiThreadInfo;
}
return null;
}

#endregion

void keyboardHook_KeyPress(object sender, KeyPressEventArgs e)
{
AddKeyboardEvent(
"KeyPress",
"",
e.KeyChar.ToString(),
"",
"",
""
);
}

void keyboardHook_KeyUp(object sender, KeyEventArgs e)
{
AddKeyboardEvent(
"KeyUp",
e.KeyCode.ToString(),
"",
e.Shift.ToString(),
e.Alt.ToString(),
e.Control.ToString()
);
}

void keyboardHook_KeyDown(object sender, KeyEventArgs e)
{
AddKeyboardEvent(
"KeyDown",
e.KeyCode.ToString(),
"",
e.Shift.ToString(),
e.Alt.ToString(),
e.Control.ToString()
);
}

void mouseHook_MouseWheel(object sender, MouseEventArgs e)
{
AddMouseEvent(
"MouseWheel",
"",
"",
"",
e.Delta.ToString()
);
}

void mouseHook_MouseUp(object sender, MouseEventArgs e)
{
AddMouseEvent(
"MouseUp",
e.Button.ToString(),
e.X.ToString(),
e.Y.ToString(),
""
);
}

void mouseHook_MouseDown(object sender, MouseEventArgs e)
{
AddMouseEvent(
"MouseDown",
e.Button.ToString(),
e.X.ToString(),
e.Y.ToString(),
""
);
}

void mouseHook_MouseMove(object sender, MouseEventArgs e)
{
SetXYLabel(e.X, e.Y);
}

void SetXYLabel(int x, int y)
{
curXYLabel.Text = String.Format("Current Mouse Point: X={0}, y={1}", x, y);
}

void AddMouseEvent(string eventType, string button, string x, string y, string delta)
{
this.textBox2.AppendText(eventType + ","
+ button + ","
+ x + ","
+ y + ","
+ delta + "\r\n");
}

void AddKeyboardEvent(string eventType, string keyCode, string keyChar, string shift, string alt, string control)
{
this.textBox3.AppendText(eventType + ","
+ keyCode + ","
+ keyChar + ","
+ shift + ","
+ alt + ","
+ control + "\r\n");

if (eventType == "KeyUp" && keyCode == this.comboBox2.Text)
{
//Clipboard.SetDataObject(this.textBox1.Lines[1].ToString());
//SendKeys.SendWait("^V");

SendKeys.Send(this.textBox1.Lines[1].ToString());

//int WM_SETTEXT = 0xC;
//IDataObject iData = Clipboard.GetDataObject();

//IntPtr hwnd = GetForegroundWindow();
//GUITHREADINFO? guiInfo = GetGuiThreadInfo(hwnd);

//SendMessage(guiInfo.Value.hwndFocus, WM_SETTEXT, 0, (string)iData.GetData(DataFormats.Text));
}
if (eventType == "KeyUp" && keyCode == this.comboBox3.Text)
{
//Clipboard.SetDataObject(this.textBox1.Lines[0].ToString());
//SendKeys.SendWait("^V");

SendKeys.Send(this.textBox1.Lines[0].ToString());
}
if (eventType == "KeyUp" && keyCode == this.comboBox4.Text)
{
//Clipboard.SetDataObject(this.textBox1.Lines[5].ToString());
//SendKeys.SendWait("^V");

SendKeys.Send(this.textBox1.Lines[5].ToString());
}
}

#endregion

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
// Not necessary anymore, will stop when application exits

mouseHook.Stop();
keyboardHook.Stop();
}

private void button2_Click(object sender, EventArgs e)
{
if (this.button2.Text == "启动")
{
mouseHook.MouseMove += new MouseEventHandler(mouseHook_MouseMove);
mouseHook.MouseDown += new MouseEventHandler(mouseHook_MouseDown);
mouseHook.MouseUp += new MouseEventHandler(mouseHook_MouseUp);
mouseHook.MouseWheel += new MouseEventHandler(mouseHook_MouseWheel);

keyboardHook.KeyDown += new KeyEventHandler(keyboardHook_KeyDown);
keyboardHook.KeyUp += new KeyEventHandler(keyboardHook_KeyUp);
keyboardHook.KeyPress += new KeyPressEventHandler(keyboardHook_KeyPress);

mouseHook.Start();
keyboardHook.Start();

SetXYLabel(MouseSimulator.X, MouseSimulator.Y);

this.button2.Text = "停止";
return;
}
else
{
mouseHook.Stop();
keyboardHook.Stop();

this.button2.Text = "启动";
}
}

原文地址:https://www.cnblogs.com/wenlong2019/p/10540572.html

时间: 2024-10-11 17:28:26

调用键盘鼠标钩子的方法的相关文章

c#键盘鼠标钩子

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows.Forms; 6 using System.Runtime.InteropServices; 7 using System.ComponentModel; 8 using System.Reflection; 9 10 namespace Alif.CommonAP

Android自动化测试初探(五): 再述模拟键盘鼠标事件(adb shell 实现) .

http://blog.csdn.net/roger_ge/article/details/5552740 转自csdn,实现模拟鼠标键盘系列 上一篇博文中讲述了通过Socket编程从外部向Emulator发送键盘鼠标模拟事件,貌似实现细节有点复杂.其实Android还有一种更简单的模拟键盘鼠标事件的方法,那就是通过使用adb shell 命令. 1.     发送键盘事件: 命令格式1:adb shell input keyevent "value" 其中value以及对应的key

C#键盘钩子 鼠标钩子

最新对C#模拟键盘按键,鼠标操作产生了兴趣.特从网上收集了一些常用的API用来调用键盘,鼠标操作. class Win32API { #region DLL导入 /// <summary> /// 用于设置窗口 /// </summary> /// <param name="hWnd"></param> /// <param name="hWndInsertAfter"></param> ///

win7和XP下全局键盘钩子的方法并不相同

win7: hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardHookProcedure, IntPtr.Zero, 0); XP: hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); 可以先执行win7的语句,如果返回的hH

VC6 鼠标钩子 最简单样例

Windows系统是建立在事件驱动的机制上的,说穿了就是整个系统都是通过消息的传递来实现的.而钩子是Windows系统中非常重要的系统接口,用它能够截获并处理送给其它应用程序的消息,来完毕普通应用程序难以实现的功能.钩子能够监视系统或进程中的各种事件消息,截获发往目标窗体的消息并进行处理.这样,我们就能够在系统中安装自己定义的钩子,监视系统中特定事件的发生,完毕特定的功能,比方截获键盘.鼠标的输入,屏幕取词,日志监视等等.可见,利用钩子能够实现很多特殊而实用的功能.因此,对于高级编程人员来说,掌

键盘鼠标模拟全知道

http://www.cnblogs.com/bjxsky/p/3656076.html 本文目录如下 一.基于windows 消息机制的鼠标键盘模拟  (一).应用程序级模拟  (二).系统级模拟         1. 用API函数keybd_event 模拟键盘事件         2. SendInput函数模拟全局键盘鼠标事件         3.用全局钩子模拟键盘消息 二.驱动级模拟 ***************************************************

隐藏ios虚拟键盘的几种方法

在iOS应用开发中,有三类视图对象会打开虚拟键盘,进行输入操作,但如何关闭虚拟键盘,却没有提供自动化的方法.这个需要我们自己去实现.这三类视图对象分别是UITextField,UITextView和UISearchBar. 这里介绍一下UITextField中关闭虚拟键盘的几种方法. (miki西游 @mikixiyou 原文链接: http://mikixiyou.iteye.com/blog/1753330 ) 第一种方法,使用它的委托UITextFieldDelegate中的方法textF

HOOK API (一)——HOOK基础+一个鼠标钩子实例

HOOK API (一)——HOOK基础+一个鼠标钩子实例 0x00 起因 最近在做毕业设计,有一个功能是需要实现对剪切板的监控和进程的防终止保护.原本想从内核层实现,但没有头绪.最后决定从调用层入手,即采用HOOK API的技术来挂钩相应的API,从而实现预期的功能.在这样的需求下,就开始学习了HOOK API. 0x01什么是HOOK API HOOK(钩子,挂钩)是一种实现Windows平台下类似于中断的机制[24].HOOK机制允许应用程序拦截并处理Windows消息或指定事件,当指定的

关闭ios虚拟键盘的几种方法

在iOS应用开发中,有三类视图对象会打开虚拟键盘,进行输入操作,但如何关闭虚拟键盘,却没有提供自动化的方法.这个需要我们自己去实现.这三类视图对象分别是UITextField,UITextView和UISearchBar. 这里介绍一下UITextField中关闭虚拟键盘的几种方法. 第一种方法,使用它的委托UITextFieldDelegate中的方法textFieldShouldReturn:来关闭虚拟键盘. 在UITextField视图对象如birdNameInput所在的类中实现这个方法