C#模拟键盘鼠标事件

本文介绍了如何在C#中模拟键盘事件,利用SendKeys类发送各种按键,包括特殊键,并提供了示例代码。此外,还探讨了通过Windows API模拟鼠标动作,如改变鼠标位置、模拟点击等。

1.模拟键盘事件
System.Windows.Forms.SendKeys
以下是   SendKeys   的一些特殊键 代码表。  
  键   代码    
  BACKSPACE   {BACKSPACE}、{BS}   或   {BKSP}    
  BREAK   {BREAK}    
  CAPS   LOCK   {CAPSLOCK}    
  DEL   或   DELETE   {DELETE}   或   {DEL}    
  DOWN   ARROW(下箭头键)   {DOWN}    
  END   {END}    
  ENTER   {ENTER}   或   ~    
  ESC   {ESC}    
  HELP   {HELP}    
  HOME   {HOME}    
  INS   或   INSERT   {INSERT}   或   {INS}    
  LEFT   ARROW(左箭头键)   {LEFT}    
  NUM   LOCK   {NUMLOCK}    
  PAGE   DOWN   {PGDN}    
  PAGE   UP   {PGUP}    
  PRINT   SCREEN   {PRTSC}(保留,以备将来使用)    
  RIGHT   ARROW(右箭头键)   {RIGHT}    
  SCROLL   LOCK   {SCROLLLOCK}    
  TAB   {TAB}    
  UP   ARROW(上箭头键)   {UP}    
  F1   {F1}    
  F2   {F2}    
  F3   {F3}    
  F4   {F4}    
  F5   {F5}    
  F6   {F6}    
  F7   {F7}    
  F8   {F8}    
  F9   {F9}    
  F10   {F10}    
  F11   {F11}    
  F12   {F12}    
  F13   {F13}    
  F14   {F14}    
  F15   {F15}    
  F16   {F16}    
  数字
键盘加号   {ADD}    
  数字键盘减号   {SUBTRACT}    
  数字键盘乘号   {MULTIPLY}    
 
数字键盘除号   {DIVIDE}    
   
  若要指定与   SHIFT、CTRL   和   ALT   键的任意组合一起使用的键,请在这些键代码之前加上以下一个或多个代码:  
   
  键   代码    
  SHIFT   +     (SHIFT="+") 
  CTRL   ^     (CTRL="^") 如果输入
  ALT   %    

private void button1_Click( object sender, System.EventArgs e)

        {//英文输入

            this

.richTextBox1.Focus();                                            
            for

(int

 i=65;i<91;i++)
            {
                char

 Letter=(char

)i;
                SendKeys.Send(Letter.ToString());
                System.Threading.Thread.Sleep(100);        
                SendKeys.Flush();
            }        
            for

(int

 i=97;i<123;i++)
            {
                char

 Letter=(char

)i;
                SendKeys.Send(Letter.ToString());
                System.Threading.Thread.Sleep(100);
                SendKeys.Flush();
            }        
        }
 
        private

 void

 button3_Click(object

 sender, System.EventArgs e)
        {//数字输入

            this

.richTextBox1.Focus();                                            
            for

(int

 i=0;i<10;i++)
            {                
                SendKeys.Send(i.ToString());
                System.Threading.Thread.Sleep(100);        
                SendKeys.Flush();
            }                
        }
 
        private

 void

 button4_Click(object

 sender, System.EventArgs e)
        {//Backspace

            this

.richTextBox1.Focus();
            SendKeys.Send("{Backspace}"

);        
        }
 
        private

 void

 button5_Click(object

 sender, System.EventArgs e)
        {//Home

            this

.richTextBox1.Focus();
            SendKeys.Send("{Home}"

);                
        }
 
        private

 void

 button6_Click(object

 sender, System.EventArgs e)
        {//End

            this

.richTextBox1.Focus();
            SendKeys.Send("{End}"

);        
        }
 
        private

 void

 button7_Click(object

 sender, System.EventArgs e)
        {//Enter

            this

.richTextBox1.Focus();
            SendKeys.Send("{Enter}"

);        
        }
 
        private

 void

 button8_Click(object

 sender, System.EventArgs e)
        {//Delete

            this

.richTextBox1.Focus();
            SendKeys.Send("{Delete}"

);        
        }
 
        private

 void

 button2_Click(object

 sender, System.EventArgs e)
        {//Shift+Home

            this

.richTextBox1.Focus();
            SendKeys.Send("+{Home}"

);                
        }
 
        private

 void

 button9_Click(object

 sender, System.EventArgs e)
        {//Shift+End

            this

.richTextBox1.Focus();
            SendKeys.Send("+{End}"

);                

}


 


 看下方法的说明
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
public
 class
 SendKeys : System.Object
    System.Windows.Forms 的成员
 
摘要:
 提供将键击发送到应用程序的方法。  

 

public
 static
 void
 Send ( System.String keys )
    System.Windows.Forms.SendKeys 的成员
 
摘要:
 向活动应用程序发送击键。  

 

public
 static
 void
 Sleep ( System.TimeSpan timeout )
    System.Threading.Thread 的成员
 
摘要:

将当前线程阻塞指定的时间。


 


.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
public
 static
 void
 Flush (  )
    System.Windows.Forms.SendKeys 的成员
 
2.模拟鼠标
  
有时,我们需在我们的程序中模拟鼠标的移动、点击等动作。——比如,一个再现用户操作的宏,或者一个演示操作方法的Demo程序。那么,我们在.Net中如何实现呢?

.Net并没有提供改变鼠标指针位置、模拟点击操作的函数;但是Windows API提供了。其中一个是:

        [DllImport( " user32.dll " )]
        
static   extern   bool  SetCursorPos( int  X,  int  Y);  


该函数可以改变鼠标指针的位置。其中X,Y是相对于屏幕左上角的绝对位置。
另一个函数是:


        [DllImport( " user32.dll " )]
        
static   extern   void  mouse_event(MouseEventFlag flags,  int  dx,  int  dy,  uint  data, UIntPtr extraInfo);


这个函数不仅可以设置鼠标指针绝对的位置,而且可以以相对坐标来设置。另外,该函数还可以模拟鼠标左右键点击、鼠标滚轮操作等。其中的MouseEventFlag是一个基于uint类型的枚举,定义如下:


        [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
        }


关于这两个函数的详细说明,可以查看MSDN Library或者Windows的Platform SDK文档。
下面的演示程序(完整版源代码,VS.Net 2005/C#)演示了使用上面的函数,控制鼠标移动到任务栏并点击“开始”按钮的方法。
(该程序使用了FindWindowEx等API函数来查找任务栏及开始菜单)

点这里下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值