/***********************************************************************************
* C# mouse keyboard monitor
* 说明:
* 最近想用C#做一个鼠标、键盘模拟器,所以找了点资料模拟一下。
*
* 2016-7-10 深圳 南山平山村 曾剑锋
**********************************************************************************/
一、参考文档:
. C# 如何用按钮实现鼠标滚轮操作
http://blog.csdn.net/jglie/article/details/6872333
. c# mouse_event 模拟鼠标点击事件 绝对位置
http://blog.sina.com.cn/s/blog_71d894bd01013goa.html
. C# Win32API 模拟鼠标移动及点击事件
http://www.cnblogs.com/08shiyan/archive/2011/07/18/2109086.html
. How to: Simulate Mouse and Keyboard Events in Code
https://msdn.microsoft.com/en-us/library/ms171548.aspx
. SendKeys Class
https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx
. Virtual-Key Codes
https://msdn.microsoft.com/zh-cn/library/dd375731(v=vs.85).aspx
. C#中将字母/字符转换为键盘的key/键值/keycode
http://www.crifan.com/convert_char_letter_to_key_keycode_in_csharp/
. VkKeyScan function
https://msdn.microsoft.com/en-us/library/ms646329(VS.85).aspx
二、KeyBoard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MouseMonitorW
{
class KeyBoard
{
\[DllImport("user32.dll")\]
static extern void keybd\_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
\[DllImport("user32.dll")\]
public static extern Keys VkKeyScan(char ch);
public static void sendKey(char key)
{
keybd\_event((byte)VkKeyScan(key), , , );
keybd\_event((byte)VkKeyScan(key), , , );
}
}
}
三、Mouse:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace MouseMonitorW
{
class Mouse
{
private const int MOUSEEVENTF\_MOVE = 0x0001; // 移动鼠标
private const int MOUSEEVENTF\_LEFTDOWN = 0x0002; // 模拟鼠标左键按下
private const int MOUSEEVENTF\_LEFTUP = 0x0004; // 模拟鼠标左键抬起
private const int MOUSEEVENTF\_RIGHTDOWN = 0x0008; // 模拟鼠标右键按下
private const int MOUSEEVENTF\_RIGHTUP = 0x0010; // 模拟鼠标右键抬起
private const int MOUSEEVENTF\_WHEEL = 0x0800; // 模拟鼠标滚轮
private const int MOUSEEVENTF\_MIDDLEDOWN = 0x0020; // 模拟鼠标中键按下
private const int MOUSEEVENTF\_MIDDLEUP = 0x0040; // 模拟鼠标中键抬起
private const int MOUSEEVENTF\_ABSOLUTE = 0x8000; // 标示是否采用绝对坐标
\[DllImport("user32")\]
private static extern int mouse\_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public static void move(int dx, int dy)
{
mouse\_event(MOUSEEVENTF\_MOVE, dx, dy, , );
}
public static void absMove(int x, int y)
{
mouse\_event(MOUSEEVENTF\_ABSOLUTE | MOUSEEVENTF\_MOVE, , , , );
}
public static void wheel(int roll)
{
mouse\_event(MOUSEEVENTF\_WHEEL, , , roll, );
}
public static void leftSingle()
{
mouse\_event(MOUSEEVENTF\_LEFTDOWN | MOUSEEVENTF\_LEFTUP, , , , );
}
public static void leftDouble()
{
mouse\_event(MOUSEEVENTF\_LEFTDOWN | MOUSEEVENTF\_LEFTUP, , , , );
mouse\_event(MOUSEEVENTF\_LEFTDOWN | MOUSEEVENTF\_LEFTUP, , , , );
}
public static void right()
{
mouse\_event(MOUSEEVENTF\_RIGHTDOWN | MOUSEEVENTF\_RIGHTUP, , , , );
}
public static void middle()
{
mouse\_event(MOUSEEVENTF\_MIDDLEUP | MOUSEEVENTF\_MIDDLEDOWN, , , , );
}
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章