(一).功能

实现用键盘模拟鼠标移动的功能,在游戏设计中常用到

*操作说明:  当运行程序后,放开鼠标,按键盘上的光标键移动,可以代替鼠标.

(二).代码

  1  using  System;
  2  using  System.Drawing;
  3  using  System.Collections;
  4  using  System.ComponentModel;
  5  using  System.Windows.Forms;
  6  using  System.Data;
  7 
  8  namespace  模拟鼠标
  9  {
 10    ///   <summary>
 11    ///  Form1 的摘要说明。
 12    ///   </summary>
 13    public   class  Form1 : System.Windows.Forms.Form
 14   {
 15     ///   <summary>
 16     ///  必需的设计器变量。
 17     ///   </summary>
 18     private  System.ComponentModel.Container components  =   null ;
 19 
 20     public  Form1()
 21    {
 22      //
 23      //  Windows 窗体设计器支持所必需的
 24      //
 25     InitializeComponent();
 26 
 27      //
 28      //  TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 29      //
 30    }
 31 
 32     ///   <summary>
 33     ///  清理所有正在使用的资源。
 34     ///   </summary>
 35     protected   override   void  Dispose(  bool  disposing )
 36    {
 37      if ( disposing )
 38     {
 39       if  (components  !=   null ) 
 40      {
 41       components.Dispose();
 42      }
 43     }
 44      base .Dispose( disposing );
 45    }
 46 
 47     #region  Windows 窗体设计器生成的代码
 48     ///   <summary>
 49     ///  设计器支持所需的方法 - 不要使用代码编辑器修改
 50     ///  此方法的内容。
 51     ///   </summary>
 52     private   void  InitializeComponent()
 53    {
 54      this .pictureBox1  =   new  System.Windows.Forms.PictureBox();
 55      this .label1  =   new  System.Windows.Forms.Label();
 56      this .SuspendLayout();
 57      //  
 58      //  pictureBox1
 59      //  
 60      this .pictureBox1.BackColor  =  System.Drawing.SystemColors.ActiveCaption;
 61      this .pictureBox1.Location  =   new  System.Drawing.Point( 88 ,  120 );
 62      this .pictureBox1.Name  =   " pictureBox1 " ;
 63      this .pictureBox1.TabIndex  =   0 ;
 64      this .pictureBox1.TabStop  =   false ;
 65      this .pictureBox1.KeyDown  +=   new  System.Windows.Forms.KeyEventHandler( this .pictureBox1_KeyDown);
 66      this .pictureBox1.MouseDown  +=   new  System.Windows.Forms.MouseEventHandler( this .pictureBox1_MouseDown);
 67      //  
 68      //  label1
 69      //  
 70      this .label1.BackColor  =  System.Drawing.Color.DarkOliveGreen;
 71      this .label1.FlatStyle  =  System.Windows.Forms.FlatStyle.System;
 72      this .label1.Location  =   new  System.Drawing.Point( 48 ,  208 );
 73      this .label1.Name  =   " label1 " ;
 74      this .label1.TabIndex  =   1 ;
 75      this .label1.Text  =   " label1 " ;
 76      //  
 77      //  Form1
 78      //  
 79      this .AutoScaleBaseSize  =   new  System.Drawing.Size( 6 ,  14 );
 80      this .ClientSize  =   new  System.Drawing.Size( 292 ,  266 );
 81      this .Controls.Add( this .label1);
 82      this .Controls.Add( this .pictureBox1);
 83      this .Name  =   " Form1 " ;
 84      this .Text  =   " Form1 " ;
 85      this .KeyDown  +=   new  System.Windows.Forms.KeyEventHandler( this .Form1_KeyDown);
 86      this .Load  +=   new  System.EventHandler( this .Form1_Load);
 87      this .KeyUp  +=   new  System.Windows.Forms.KeyEventHandler( this .Form1_KeyUp);
 88      this .MouseMove  +=   new  System.Windows.Forms.MouseEventHandler( this .Form1_MouseMove);
 89      this .ResumeLayout( false );
 90 
 91    }
 92     #endregion
 93 
 94     ///   <summary>
 95     ///  应用程序的主入口点。
 96     ///   </summary>
 97    [STAThread]
 98     static   void  Main() 
 99    {
100     Application.Run( new  Form1());
101    }
102    [System.Runtime.InteropServices.DllImport( " user32 " )]
103        private   static   extern   int  mouse_event( int  dwFlags, int  dx, int  dy, int  cButtons, int  dwExtraInfo);
104     const   int  MOUSEEVENTF_MOVE = 0x0001 ;
105     const   int  MOUSEEVENTF_LEFTDOWN = 0X0002 ;
106     const   int  MOUSEEVENTF_LEFTUP = 0X0004 ;
107     const   int  MOUSEEVENTF_RIGHTDOWN = 0X0008 ;
108     const   int  MOUSEEVENTF_RIGHTUP = 0X0010 ;
109     const   int  MOUSEEVENTF_MIDDLEDOWN = 0X0020 ;
110     const   int  MOUSEEVENTF_MIDDLEUP = 0X0040 ;
111     private  System.Windows.Forms.PictureBox pictureBox1;
112     private  System.Windows.Forms.Label label1;
113     const   int  MOUSEEVENTF_ABSOLUTE = 0X8000 ;
114 
115     private   void  Form1_MouseMove( object  sender, System.Windows.Forms.MouseEventArgs e)
116    {
117      // mouse_event(MOUSEEVENTF_MOVE,-10,-10,0,0);
118    }
119 
120     private   void  Form1_KeyDown( object  sender, System.Windows.Forms.KeyEventArgs e)
121    {
122      if (e.KeyCode == Keys.Down)
123      mouse_event(MOUSEEVENTF_MOVE, 0 , 20 , 0 , 0 );
124      if (e.KeyCode == Keys.Up)
125      mouse_event(MOUSEEVENTF_MOVE, 0 , - 20 , 0 , 0 );
126      if (e.KeyCode == Keys.Left)
127      mouse_event(MOUSEEVENTF_MOVE, - 20 , 0 , 0 , 0 );
128      if (e.KeyCode == Keys.Right)
129      mouse_event(MOUSEEVENTF_MOVE, 20 , 0 , 0 , 0 );
130    }
131 
132     public   void  pictureBox1_KeyDown( object  sender, System.Windows.Forms.KeyEventArgs e)
133    {
134      /* if(e.KeyCode==Keys.Down)
135      mouse_event(MOUSEEVENTF_MOVE,0,20,0,0);
136     if(e.KeyCode==Keys.Up)
137      mouse_event(MOUSEEVENTF_MOVE,0,-20,0,0);
138     if(e.KeyCode==Keys.Left)
139      mouse_event(MOUSEEVENTF_MOVE,-20,0,0,0);
140     if(e.KeyCode==Keys.Right)
141      mouse_event(MOUSEEVENTF_MOVE,20,0,0,0);
142       */
143    }
144     private   void  Form1_KeyUp( object  sender, System.Windows.Forms.KeyEventArgs e)
145    {
146    
147    }
148 
149    
150 
151     private   void  pictureBox1_MouseDown( object  sender, System.Windows.Forms.MouseEventArgs e)
152    {
153      if (e.Button == MouseButtons.Right)
154      mouse_event(MOUSEEVENTF_MOVE, 0 , 20 , 0 , 0 );
155      else
156      mouse_event(MOUSEEVENTF_MOVE, 0 , - 20 , 0 , 0 );
157    }
158 
159     private   void  Form1_Load( object  sender, System.EventArgs e)
160    {
161     
162    }  
163   
164   }
165  }
166 
167 

(三).示例代码下载

http://www.cnblogs.com/Files/ChengKing/模拟鼠标.rar

(四). 相关文章

1. 在WebForm(Asp.net页面)中实现拖动效果, 请看:

http://blog.csdn.net/ChengKing/archive/2006/08/30/1142605.aspx

2. 在WinForm中实现拖动效果, 请看:

http://blog.csdn.net/chengking/archive/2005/10/07/496739.aspx

Control Study - 键盘模拟鼠标(实现用键盘操作鼠标光标)(示例代码下载)相关推荐

  1. C# 系统应用之鼠标模拟技术及自动操作鼠标

    游戏程序的操作不外乎两种--键盘输入控制和鼠标输入控制,几乎所有游戏中都使用鼠标来改变角色的位置和方向,本文主要是讲述如何使用C#调用Windows API函数实现鼠标模拟操作的功能.首先通过结合Fi ...

  2. leaflet鼠标进出事件 mouseover和mouseout (示例代码009)

    第009个 点击查看专栏目录 本示例的目的是介绍演示如何在vue+leaflet中给marker添加鼠标事件,这里用到了mouseover和mouseout. 直接复制下面的 vue+leaflet源 ...

  3. python keyboard模块_[python] PyMouse、PyKeyboard用python操作鼠标和键盘

    1.PyUserInput 简介 PyUserInput是一个使用python的跨平台的操作鼠标和键盘的模块,非常方便使用.支持的平台及依赖如下: Linux - Xlib Mac - Quartz, ...

  4. 小学计算机课程介绍鼠标,小学信息技术《认识鼠标》

    小学信息技术<认识鼠标> 2018-11-27 <认识鼠标> ──鼠标的单击.双击和拖动 山东济宁市永丰街中心小学 翟海峰 [适用版本]小学信息技术(泰山版)教材教学案例 [适 ...

  5. 模拟鼠标键盘html,模拟按键操作神器(鼠标键盘模拟操作助手)V2.0.2.1 正式版

    模拟按键操作神器(鼠标键盘模拟操作助手)是一款很优秀好用的由网友自制的模拟按键操作的辅助工具.如果你需要一款好用的键盘模拟软件,小编带来的这款模拟按键操作神器是很不错的选择,功能强大全面,使用后可以帮 ...

  6. python实现按键精灵的功能_利用Python实现Windows下的鼠标键盘模拟的实例代码

    本文介绍了利用Python实现Windows下的鼠标键盘模拟的实例代码,分享给大家 本来用按键精灵是可以实现我的需求,而且更简单,但既然学python ,就看一下呗. 依赖: pip install ...

  7. vba模拟鼠标点击_鼠标键盘模拟大师下载_鼠标键盘模拟大师免费版官方下载6.2...

    鼠标键盘模拟大师是目前国内唯一一款将鼠标自动点击.键盘自动输入.网页自动刷新和自动切换IP地址等功能完美结合与一体的鼠标键盘动作模拟软件,可以实现桌面及游戏的自动点击和按键点击等功能,有需要的小伙伴快 ...

  8. html 鼠标中键事件,Javascript事件模拟(鼠标事件、键盘事件)

    在javascript编程中,事件是用来描述网页中某一特定有趣时刻的,众所周知事件通常是在由用户和浏览器进行交互时触发,其实不然,通过Javascript可以在任何时间触发特定的事件,并且这些事件与浏 ...

  9. python3键盘事件_python+selenium3 鼠标事件和键盘事件

    1.鼠标事件: # 每个模拟事件后需加.perform() 才会执行 # context_click() 右击 # double_click() 双击 # drag_and_drop(source, ...

最新文章

  1. mysql 单标递归_MySQL递归CTE(公共表表达式)
  2. 用python绘制柱状图标题-使用Python绘制图表大全总结
  3. Oracle的rownum原理和使用(整理几个达人的帖子)
  4. vue导入html登陆页,Vue 实现 登陆后打开主页面(登陆组件 + 主页面组件)
  5. 关于开源中国手机App的说明
  6. matlab gui教程 计算器,matlab gui编写的计算器程序
  7. LYNC 2010 安装指南和心得
  8. mac php apache mysql 集成环境 的软件
  9. 多线程同步工具——volatile变量
  10. Web系统集成OCS在线状态功能
  11. 冰点文库下载器2021新版 v3.2.16
  12. python3.6 编程技巧总结
  13. 2019-05-22 SQL注入;啊D注入工具;
  14. kodi电视smb android,小米/天猫魔盒KODI(XBMC)SMB协议播放测试
  15. C语言有必要学的很深入细致吗?
  16. 在Scrum中添加目标与合弄制
  17. 【踩坑日记】Vue组件@click事件点击没有反应
  18. Kafka学习——基于已有zookeeper集群实现kafka的集成
  19. FileReader()用法
  20. XML和Dom4j、正则表达式

热门文章

  1. 动态规划状态机模型:股票买卖I
  2. ADRC线性跟踪微分器(ST+SCL语言)
  3. 基于C++的鬼脚图抽签游戏
  4. EMQ 携手 NNG 联合发布新一代超轻量边缘 MQTT 消息引擎 NanoMQ
  5. CrossEntropyLabelSmooth 的代码
  6. 全网最新刷题神器来了,面试题想搜就搜!
  7. php使用递归完成以下图形,php试题与答案(一)
  8. 二叉树--二叉搜索树
  9. Kafka-消息积压处理方案
  10. 教你如何通过ssh控制远程主机,远程执行命令