/***********************************************************************************
* C# mouse keyboard monitor
* 说明:
* 最近想用C#做一个鼠标、键盘模拟器,所以找了点资料模拟一下。
*
* 2016-7-10 深圳 南山平山村 曾剑锋
**********************************************************************************/
一、参考文档:
1. C# 如何用按钮实现鼠标滚轮操作
http://blog.csdn.net/jglie/article/details/6872333
2. c# mouse_event 模拟鼠标点击事件 绝对位置
http://blog.sina.com.cn/s/blog_71d894bd01013goa.html
3. C# Win32API 模拟鼠标移动及点击事件
http://www.cnblogs.com/08shiyan/archive/2011/07/18/2109086.html
4. How to: Simulate Mouse and Keyboard Events in Code
https://msdn.microsoft.com/en-us/library/ms171548.aspx
5. SendKeys Class
https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx
6. Virtual-Key Codes
https://msdn.microsoft.com/zh-cn/library/dd375731(v=vs.85).aspx
7. C#中将字母/字符转换为键盘的key/键值/keycode
http://www.crifan.com/convert_char_letter_to_key_keycode_in_csharp/
8. 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), 0, 0, 0);
keybd_event((byte)VkKeyScan(key), 0, 2, 0);
}
}
}
三、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, 0, 0);
}
public static void absMove(int x, int y)
{
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, 500, 500, 0, 0);
}
public static void wheel(int roll)
{
mouse_event(MOUSEEVENTF_WHEEL, 0, 0, roll, 0);
}
public static void leftSingle()
{
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
public static void leftDouble()
{
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
public static void right()
{
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
}
public static void middle()
{
mouse_event(MOUSEEVENTF_MIDDLEUP | MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
}
}
}