利用C#的键盘全局监听事件实现操控鼠标
有关C#的全局键盘监听,可参考其他网友的文章
http://blog.csdn.net/c0411034/article/details/70306464
用到了KeyboardHook这个类
可以利用Windows 的Api设置鼠标位置,也可以使用Cursor.Position 来设置位置
//设置鼠标位置
[DllImport("user32.DLL", EntryPoint = "SetCursorPos")]
private static extern int SetCursorPos(int x,int y);
定义一些基本参数
private int hSpeed = 0;//水平速度
private int vSpeed = 0;//垂直速度
private int acceleration = 1;//加速度
private int max_speed = 10;//最大速度
private void hook_KeyUp(object sender, KeyEventArgs e) {
if (e.KeyData == Keys.Left || e.KeyData == Keys.Right)
{
hSpeed = 0;
}
else if (e.KeyData == Keys.Up || e.KeyData == Keys.Down)
{
vSpeed = 0;
}
}
private void hook_KeyDown(object sender,KeyEventArgs e) {
if (e.KeyData == Keys.Left)
{
if(hSpeed > -(max_speed)) hSpeed -= acceleration;
}
else if (e.KeyData == Keys.Right)
{
if(hSpeed < max_speed) hSpeed += acceleration;
}
else if (e.KeyData == Keys.Up)
{
if (vSpeed > -(max_speed)) vSpeed -= acceleration;
}
else if (e.KeyData == Keys.Down)
{
if (vSpeed < max_speed) vSpeed += acceleration;
}
}
添加一个行动方法
private void move() {
Thread t = new Thread(delegate() {
System.Drawing.Point point = new System.Drawing.Point();
while (true) {
int x = Cursor.Position.X;
int y = Cursor.Position.Y;
SetCursorPos(x+hSpeed,y+vSpeed);
Thread.Sleep(10);
}
});
t.IsBackground = true;
t.Start();
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
//设置鼠标位置
[DllImport("user32.DLL", EntryPoint = "SetCursorPos")]
private static extern int SetCursorPos(int x,int y);
private KeyEventHandler handlerDown = null;
private KeyEventHandler handlerUp = null;
private KeyboardHook hook = new KeyboardHook();
private int hSpeed = 0;//水平速度
private int vSpeed = 0;//垂直速度
private int acceleration = 1;//加速度
private int max_speed = 10;//最大速度
private void hook_KeyDown(object sender,KeyEventArgs e) {
if (e.KeyData == Keys.Left)
{
if(hSpeed > -(max_speed)) hSpeed -= acceleration;
}
else if (e.KeyData == Keys.Right)
{
if(hSpeed < max_speed) hSpeed += acceleration;
}
else if (e.KeyData == Keys.Up)
{
if (vSpeed > -(max_speed)) vSpeed -= acceleration;
}
else if (e.KeyData == Keys.Down)
{
if (vSpeed < max_speed) vSpeed += acceleration;
}
}
private void hook_KeyUp(object sender, KeyEventArgs e) {
if (e.KeyData == Keys.Left || e.KeyData == Keys.Right)
{
hSpeed = 0;
}
else if (e.KeyData == Keys.Up || e.KeyData == Keys.Down)
{
vSpeed = 0;
}
}
private void move() {
Thread t = new Thread(delegate() {
System.Drawing.Point point = new System.Drawing.Point();
while (true) {
int x = Cursor.Position.X;
int y = Cursor.Position.Y;
SetCursorPos(x+hSpeed,y+vSpeed);
Thread.Sleep(10);
}
});
t.IsBackground = true;
t.Start();
}
//开启监听
public void startListen() {
handlerDown = new KeyEventHandler(hook_KeyDown);
handlerUp = new KeyEventHandler(hook_KeyUp);
hook.KeyDownEvent += handlerDown;
hook.KeyUpEvent += handlerUp;
hook.Start();//安装键盘钩子
}
//关闭监听
public void stopListen() {
if (handlerDown != null && handlerUp != null) {
hook.KeyDownEvent -= handlerDown;
hook.KeyUpEvent -= handlerUp;
hook.Stop();//关闭键盘钩子
}
}
private void button1_Click(object sender, EventArgs e)
{
this.startListen();
this.move();
}
private void button2_Click(object sender, EventArgs e)
{
this.stopListen();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
this.stopListen();
}
}
记得在程序退出的时候要关闭监听哦
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
this.stopListen();
}
这样一个利用键盘的上下左右就能超控鼠标的程序就完成啦!!
不过,这个只能移动鼠标,鼠标没有点击。