最新玩了一下上学时候玩的游戏,但游戏里面变化太多了,进去后等级就很高,要不停地点击鼠标加技能。
所以利用工作中常用的C#调用 API不停地点击鼠标。
如图:
为方便在基础上修改,我把整个解决方案放到百度云盘。以下为链接:
链接: http://pan.baidu.com/s/1o7t80bS 密码: pehi
先将用到的公用方法贴上来:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms; namespace AutoKeyTool
{
public class KeyboardHook
{
int hHook;
Win32Api.HookProc KeyboardHookDelegate;
public event KeyEventHandler OnKeyDownEvent;
public event KeyEventHandler OnKeyUpEvent;
public event KeyPressEventHandler OnKeyPressEvent; public KeyboardHook() { }
public void SetHook()
{
KeyboardHookDelegate = new Win32Api.HookProc(KeyboardHookProc);
Process cProcess = Process.GetCurrentProcess();
ProcessModule cModule = cProcess.MainModule;
var mh = Win32Api.GetModuleHandle(cModule.ModuleName);
hHook = Win32Api.SetWindowsHookEx(Win32Api.WH_KEYBOARD_LL, KeyboardHookDelegate, mh, );
} public void UnHook()
{
Win32Api.UnhookWindowsHookEx(hHook);
} private List<Keys> preKeysList = new List<Keys>();//存放被按下的控制键,用来生成具体的键
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
//如果该消息被丢弃(nCode<0)或者没有事件绑定处理程序则不会触发事件
if ((nCode >= ) && (OnKeyDownEvent != null || OnKeyUpEvent != null || OnKeyPressEvent != null))
{
Win32Api.KeyboardHookStruct KeyDataFromHook = (Win32Api.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Api.KeyboardHookStruct));
Keys keyData = (Keys)KeyDataFromHook.vkCode;
//按下控制键
if ((OnKeyDownEvent != null || OnKeyPressEvent != null) && (wParam == Win32Api.WM_KEYDOWN || wParam == Win32Api.WM_SYSKEYDOWN))
{
if (IsCtrlAltShiftKeys(keyData) && preKeysList.IndexOf(keyData) == -)
{
preKeysList.Add(keyData);
}
}
//WM_KEYDOWN和WM_SYSKEYDOWN消息,将会引发OnKeyDownEvent事件
if (OnKeyDownEvent != null && (wParam == Win32Api.WM_KEYDOWN || wParam == Win32Api.WM_SYSKEYDOWN))
{
KeyEventArgs e = new KeyEventArgs(GetDownKeys(keyData));
OnKeyDownEvent(this, e);
} //WM_KEYDOWN消息将引发OnKeyPressEvent if (OnKeyPressEvent != null && wParam == Win32Api.WM_KEYDOWN)
{
byte[] keyState = new byte[];
Win32Api.GetKeyboardState(keyState);
byte[] inBuffer = new byte[];
if (Win32Api.ToAscii(KeyDataFromHook.vkCode, KeyDataFromHook.scanCode, keyState, inBuffer, KeyDataFromHook.flags) == )
{
KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[]);
OnKeyPressEvent(this, e);
}
} //松开控制键
if ((OnKeyDownEvent != null || OnKeyPressEvent != null) && (wParam == Win32Api.WM_KEYUP || wParam == Win32Api.WM_SYSKEYUP))
{
if (IsCtrlAltShiftKeys(keyData))
{
for (int i = preKeysList.Count - ; i >= ; i--)
{
if (preKeysList[i] == keyData) { preKeysList.RemoveAt(i); }
}
}
} //WM_KEYUP和WM_SYSKEYUP消息,将引发OnKeyUpEvent事件
if (OnKeyUpEvent != null && (wParam == Win32Api.WM_KEYUP || wParam == Win32Api.WM_SYSKEYUP))
{
KeyEventArgs e = new KeyEventArgs(GetDownKeys(keyData));
OnKeyUpEvent(this, e);
}
}
return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam); } //根据已经按下的控制键生成key
private Keys GetDownKeys(Keys key)
{
Keys rtnKey = Keys.None;
foreach (Keys i in preKeysList)
{
if (i == Keys.LControlKey || i == Keys.RControlKey) { rtnKey = rtnKey | Keys.Control; }
if (i == Keys.LMenu || i == Keys.RMenu) { rtnKey = rtnKey | Keys.Alt; }
if (i == Keys.LShiftKey || i == Keys.RShiftKey) { rtnKey = rtnKey | Keys.Shift; }
}
return rtnKey | key;
}
private Boolean IsCtrlAltShiftKeys(Keys key)
{
if (key == Keys.LControlKey || key == Keys.RControlKey || key == Keys.LMenu || key == Keys.RMenu || key == Keys.LShiftKey || key == Keys.RShiftKey) { return true; }
return false;
}
}
}
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text; namespace AutoKeyTool
{
public class MouseHook
{
//结构体布局 本机位置
[StructLayout(LayoutKind.Sequential)]
struct NativeRECT
{
public int left;
public int top;
public int right;
public int bottom;
} //将枚举作为位域处理
[Flags]
enum MouseEventFlag : uint //设置鼠标动作的键值
{
Move = 0x0001, //发生移动
LeftDown = 0x0002, //鼠标按下左键
LeftUp = 0x0004, //鼠标松开左键
RightDown = 0x0008, //鼠标按下右键
RightUp = 0x0010, //鼠标松开右键
MiddleDown = 0x0020, //鼠标按下中键
MiddleUp = 0x0040, //鼠标松开中键
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800, //鼠标轮被移动
VirtualDesk = 0x4000, //虚拟桌面
Absolute = 0x8000
}
//设置鼠标位置
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y); //设置鼠标按键和动作
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy,uint data, UIntPtr extraInfo); //UIntPtr指针多句柄类型 [DllImport("user32.dll")]
static extern IntPtr FindWindow(string strClass, string strWindow); //该函数获取一个窗口句柄,该窗口雷鸣和窗口名与给定字符串匹配 hwnParent=Null从桌面窗口查找
[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string strClass, string strWindow); [DllImport("user32.dll")]
static extern bool GetWindowRect(HandleRef hwnd, out NativeRECT rect); public static void MouseClick()
{
mouse_event(MouseEventFlag.LeftDown, , , , UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, , , , UIntPtr.Zero);
}
}
}
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text; namespace AutoKeyTool
{
public class Win32Api
{
#region 常数和结构
public const int WM_KEYDOWN = 0x100;
public const int WM_KEYUP = 0x101;
public const int WM_SYSKEYDOWN = 0x104;
public const int WM_SYSKEYUP = 0x105;
public const int WH_KEYBOARD_LL = ; [StructLayout(LayoutKind.Sequential)] //声明键盘钩子的封送结构类型
public class KeyboardHookStruct
{
public int vkCode; //表示一个在1到254间的虚似键盘码
public int scanCode; //表示硬件扫描码
public int flags;
public int time;
public int dwExtraInfo;
} #endregion #region Api
public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
//安装钩子的函数
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
//卸下钩子的函数
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
//下一个钩挂的函数
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
[DllImport("user32")]
public static extern int ToAscii(int uVirtKey, int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey, int fuState);
[DllImport("user32")]
public static extern int GetKeyboardState(byte[] pbKeyState);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
#endregion
}
}
调用放在winform窗体里面了,如果需要可下载源码:链接: http://pan.baidu.com/s/1jGO85Xc 密码: jk37