ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

调用键盘鼠标钩子的方法

2019-03-16 08:53:35  阅读:210  来源: 互联网

标签:IntPtr 鼠标 钩子 键盘 ToString mouseHook keyboardHook public string


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 = "启动";
}
}

标签:IntPtr,鼠标,钩子,键盘,ToString,mouseHook,keyboardHook,public,string
来源: https://www.cnblogs.com/wenlong2019/p/10540572.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有