一个简单的模拟键盘鼠标操作的类

一个简单的模拟键盘鼠标操作的类,扩充 VirtualKeys 枚举就可以了,或者直接写!

using System;
using System.Runtime.InteropServices;
using System.Text;
class Keyboard
   {
     const uint KEYEVENTF_EXTENDEDKEY = 0x1;
     const uint KEYEVENTF_KEYUP = 0x2;
     [DllImport("user32.dll")]
      static extern short GetKeyState(int nVirtKey);
     [DllImport("user32.dll")]
     static extern void keybd_event(
                      byte bVk,
                      byte bScan,
                      uint dwFlags,
                      uint dwExtraInfo
                                               );

   public enum VirtualKeys: byte
{
    VK_NUMLOCK = 0x90, //数字锁定键
    VK_SCROLL = 0x91, //滚动锁定
    VK_CAPITAL = 0x14, //大小写锁定
   VK_A = 62
   }


     public static bool GetState(VirtualKeys Key)
   {
     return (GetKeyState((int)Key)==1);
   }
public static void SetState(VirtualKeys Key, bool State)
   {
    if(State!=GetState(Key))
     {
      keybd_event(
                           (byte)Key,
                            0x45,
                             KEYEVENTF_EXTENDEDKEY | 0,
                          0
                          );
     keybd_event(
                        (byte)Key,
                          0x45,
                        KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
                        0
                        );
    }
   }
}

示例:
    模拟操作
     Keyboard.SetState(
                                   VirtualKeys.VK_CAPITAL,
                                   !Keyboard.GetState(VirtualKeys.VK_CAPITAL)
                                   );
   得到键盘状态
   Keyboard.GetState(VirtualKeys.VK_CAPITAL)

* 十进制值 标识符 IBM兼容键盘 

--------------------------------------------------------------------------------

1    VK_LBUTTON   鼠标左键
2    VK_RBUTTON   鼠标右键
3    VK_CANCEL   Ctrl+Break(通常不需要处理)
4    VK_MBUTTON   鼠标中键
8    VK_BACK    Backspace
9    VK_TAB     Tab
12   VK_CLEAR    Num Lock关闭时的数字键盘5
13   VK_RETURN   Enter(或者另一个)
16   VK_SHIFT    Shift(或者另一个)
17   VK_CONTROL   Ctrl(或者另一个)
18   VK_MENU    Alt(或者另一个)
19   VK_PAUSE    Pause
20   VK_CAPITAL   Caps Lock
27   VK_ESCAPE   Esc
32   VK_SPACE    Spacebar
33   VK_PRIOR    Page Up
34   VK_NEXT    Page Down
35   VK_END     End
36   VK_HOME    Home
37    VK_LEFT    左箭头
38   VK_UP     上箭头
39   VK_RIGHT   右箭头
40   VK_DOWN    下箭头
41   VK_SELECT   可选
42   VK_PRINT   可选
43   VK_EXECUTE  可选
44   VK_SNAPSHOT  Print Screen
45   VK_INSERT   Insert
46   VK_DELETE  Delete
47   VK_HELP   可选
48~57  无      主键盘上的0~9
65~90  无      A~Z
96~105  VK_NUMPAD0~VK_NUMPAD9   Num Lock打开时数字键盘上的0~9
106   VK_NULTIPLY         数字键盘上的*
107   VK_ADD           数字键盘上的+
108   VK_SEPARATOR        可选
109   VK_SUBTRACT         数字键盘上的-
110   VK_DECIMAL         数字键盘上的.
111   VK_DIVIDE          数字键盘上的/
112~135 VK_F1~VK_F24        功能键F1~F24
144   VK_NUMLOCK         Num Lock
145   VK_SCROLL          Scroll Lock

*/

 

 

突然发现在c#里面原来还有一个 System.Windows.Forms.SendKeys

不过这个只能模拟键盘

 

真正能模拟鼠标操作的代码在这里!找的我好辛苦啊!

函数声明:
        private readonly int MOUSEEVENTF_LEFTDOWN = 0x2;
        private readonly int MOUSEEVENTF_LEFTUP = 0x4;
        [DllImport("user32")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

调用方法:
mouse_event(MOUSEEVENTF_LEFTDOWN, X * 65536 / 1024, Y * 65536 / 768, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, X * 65536 / 1024, Y * 65536 / 768, 0, 0);
其中X,Y分别是你要点击的点的横坐标和纵坐标

posted on 2008-11-08 14:23  Nick-fbx  阅读(595)  评论(0编辑  收藏  举报

导航