最新玩了一下上学时候玩的游戏,但游戏里面变化太多了,进去后等级就很高,要不停地点击鼠标加技能。

所以利用工作中常用的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

C#实现自动单击的更多相关文章

  1. 【转载】Delphi下实现鼠标自动点击器

    本文最早于2009年6月1日在编程论坛(programbbs.com)上发表,页面地址:http://programbbs.com/bbs/view12-20849-1.htm . 众所周知,当鼠标指 ...

  2. jQuery使用之(五)处理页面的事件

    在之前dom操作中提到了javascript对事件处理的介绍.由于不同浏览器处理事件各不相相同,这给开发者带来了不必要的麻烦,jQuery的方便的解决了这个方面的麻烦. 1.绑定事件监听 (http: ...

  3. AYUI -AYUI风格的 超美 百度网盘8.0

    2017-03-23 19:18:43 (截止到2017-3-23 20:20:33开发结束)体验地址:  http://pan.baidu.com/s/1bX28H4 新增传输列表 ======== ...

  4. Chrome插件笔记之content_scripts

    一.概论 说这个之前先看一个段子,讲的是甲方有一奇葩客户,这客户看一网站某些样式很别扭不得劲,非要让乙方修改,乍一听没毛病,但关键是这网站不是乙方家的,根本没有修改权限,怎么办,客户就是上帝,上帝的要 ...

  5. Qt Installer Framework 使用说明(三)

    目录 6.Qt Installer Framework 示例 7.参考 Reference 配置文件 Configuration File 配置文件元素的简要说明 Summary of Configu ...

  6. Qt Installer Framework 使用说明(二)

    目录 4.教程: 创建一个安装程序 创建软件包目录 创建配置文件 创建程序包信息文件 指定组件信息 指定安装程序版本 添加许可证 选择默认内容 创建安装程序内容 创建安装程序二进制文件 5.创建安装程 ...

  7. 运维笔记--SqlServer相关版本&下载&安装&配置远程连接

    下载地址:SqlServer2008为例 SqlServer2008:https://www.microsoft.com/en-us/download/details.aspx?id=1695 Sql ...

  8. Qt Installer Framework翻译(7-3)

    控制脚本 对于每个安装程序,您可以指定一个控制脚本,用来与安装程序的部分UI或功能进行交互.控制脚本可以在向导中添加和删除页面,更改现有页面,进行附加检查以及通过模拟用户单击来与UI交互.例如,这允许 ...

  9. html5,单击文字自动获得焦点

    <p><label for=""></label>这样单击后自动获得焦点</p>

随机推荐

  1. ASP.NET MVC 实现AJAX跨域请求方法《1》

    ASP.NET MVC 实现AJAX跨域请求的两种方法 通常发送AJAX请求都是在本域内完成的,也就是向本域内的某个URL发送请求,完成部分页面的刷新.但有的时候需要向其它域发送AJAX请求,完成数据 ...

  2. HTTP 错误500.19 -Internal Server Error 错误代码 0x80070021

    转自:http://blog.csdn.net/goux_s/article/details/5676689 错误摘要 HTTP 错误500.19 -Internal Server Error 无法访 ...

  3. 关于GPL的一些知识

    1.什么是GPL GPL许可协议(GNU General Public License):只要软件中包含有其他GPL协议的产品或代码,那么该软件就必须也采用GPL许可协议且开源及免费.具有以下特点: ...

  4. NSSortDescriptor对象进行数组排序

    //创建一个数组 NSArray *array = @[@"zhangsan", @"lisi", @"zhonger", @"z ...

  5. Programming Entity Framework 翻译(2)-目录2-章节

    How This Book Is Organized 本书组织结构 Programming Entity Framework, Second Edition, focuses on two ways ...

  6. private + virtual in Java/C++

    在Java中,private方法是隐式final的,就是说即使在子类中定义一个一模一样的方法,编译器认为这是两个没有联系的方法.private方法不参与运行时多态,这点和 final方法.static ...

  7. SpreadJS电子表格

    SpreadJS是wijmo旗下的一款HTML5电子表格控件. 官方网址:http://wijmo.com/products/spreadjs/ 在这里简单整理了SpreadJS几个比较简单的特点,如 ...

  8. [delphi]SetWindowsHookExA函数入口处修改

    library Project2; uses SysUtils, Classes, windows, Dialogs; {$R *.res} function GetModuleHandleA(a: ...

  9. sql server之临时表

    一.临时表介绍 临时表在运行被创建的,虽说它的名字叫做临时表,可是对它的操作和我们平时操控表的操作基本一样, 比如最简单增.删.改.查等等.但是,需要注意的是临时表的创建是有范围限制的. 二.临时表分 ...

  10. Network服务器

    1.授权服务器与非授权服务器:大多数网游都是授权服务器,用户无法修改数据.