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