键盘虚拟键值编码表 使用keybd_Event 以及虚拟按键与扫描码相互转化函数MapVirtualKey

也是在cnblogs上找的,怕到时忘了,先记下来
原文章 :http://www.cnblogs.com/nemolog/archive/2005/10/30/265035.html

模拟键盘输入首先要用到一个API函数:keybd_event。
  我们是菜鸟,所以不必具体去理解它的详细用法,只要按以下方法使用即可了!呵呵!
  模拟按键有两个基本动作,即按下键和放开按键,所以我们每模拟一次按键就要调用两次该API函数,其方法是:
  例子1:模拟按下'A'键
   keybd_event(65,0,0,0);
   keybd_event(65,0,KEYEVENTF_KEYUP,0);
  例子2:模拟按下'ALT+F4'键
   keybd_event(18,0,0,0);
   keybd_event(115,0,0,0);
   keybd_event(115,0,KEYEVENTF_KEYUP,0);

   keybd_event(18,0,KEYEVENTF_KEYUP,0);

另外,keybd_event的函数原型为:

VOID WINAPI keybd_event(
  _In_  BYTE bVk,
  _In_  BYTE bScan,
  _In_  DWORD dwFlags,
  _In_  ULONG_PTR dwExtraInfo
);

第二个参数是scancode(扫描码)。由虚拟按键得到扫描码使用函数MapVirtualKey。MapVirtualKey还可以由扫描码得到虚拟按键编码。

UINT WINAPI MapVirtualKey(
  _In_  UINT uCode,
  _In_  UINT uMapType
);

Parameters
    uCode [in]
Type: UINT
    The virtual key code or scan code for a key. How this value is interpreted depends on the value of the uMapType parameter.
uMapType [in]
    Type: UINT
    The translation to be performed. The value of this parameter depends on the value of the uCode parameter.
Value                                  
(1)MAPVK_VK_TO_CHAR  2
uCode is a virtual-key code and is translated into an unshifted character value in the low-order word of the return value. Dead keys (diacritics) are indicated by setting the top bit of the return value. If there is no translation, the function returns 0.
(2)MAPVK_VK_TO_VSC    0
uCode is a virtual-key code and is translated into a scan code. If it is a virtual-key code that does not distinguish between left- and right-hand keys, the left-hand scan code is returned. If there is no translation, the function returns 0.
(3)MAPVK_VSC_TO_VK   1
uCode is a scan code and is translated into a virtual-key code that does not distinguish between left- and right-hand keys. If there is no translation, the function returns 0.
(4)MAPVK_VSC_TO_VK_EX   3
uCode is a scan code and is translated into a virtual-key code that distinguishes between left- and right-hand keys. If there is no translation, the function returns 0.

Return value
Type: UINT

The return value is either a scan code, a virtual-key code, or a character value, depending on the value of uCode and uMapType. If there is no translation, the return value is zero.


例子:在启动一个程序之前清空屏幕(按Win +D)

[DllImport("User32.dll")]
public static extern void keybd_event(Byte bVk, Byte bScan, Int32 dwFlags, Int32 dwExtraInfo);

keybd_event(0x5b, 0, 0, 0);
keybd_event(68, 0, 0, 0);
keybd_event(0x5b, 0, 0x2, 0);
keybd_event(68, 0, 0x2, 0);

   附:常用模拟键的键值对照表。
                      键盘键与虚拟键码对照表


      字母和数字键     数字小键盘的键       功能键         其它键
      键   键码     键   键码       键   键码     键      键码
      A   65      0   96        F1   112     Backspace    8
      B   66      1   97        F2   113     Tab        9
      C   67               2   98        F3   114     Clear        12
      D   68      3   99                     F4   115     Enter       13
      E   69       4   100                   F5   116     Shift        16
      F   70       5   101                   F6   117     Control       17
      G   71       6   102        F7   118       Alt          18
      H   72       7   103        F8   119     Caps Lock     20
      I   73       8   104       F9   120      Esc         27
      J   74       9   105       F10  121     Spacebar    32
      K   75                 *   106       F11  122     Page Up      33
      L   76                 +   107       F12  123     Page Down    34
      M   77       Enter  108       --   --     End         35
      N   78                 -    109       --   --       Home      36
      O   79       .   110       --   --       Left Arrow     37
      P   80       /    111       --   --      Up Arrow      38
      Q   81       --   --       --   --        Right Arrow     39
      R   82       --   --       --   --        Down Arrow     40
      S   83       --   --       --   --        Insert           45
      T   84       --   --       --   --        Delete          46
      U   85       --   --       --   --        Help          47
      V   86       --   --       --   --        Num Lock      144
      W   87          
      X   88      
      Y   89      
      Z   90      
      0   48      
      1   49      
      2   50       
      3   51       
      4   52       
      5   53       
      6   54       
      7   55       
      8   56       
      9   57  
----------------------------------
与键盘上各键对应的键值
在软件开发的过程中我们经常与键盘打交道,以下是我查MSDN 所得希望对各位有帮助。
可在代码中的任何地方用下列值代替键盘上的键:
值 描述
0x1 鼠标左键
0x2 鼠标右键
0x3 CANCEL 键
0x4 鼠标中键
0x8 BACKSPACE 键
0x9 TAB 键
0xC CLEAR 键
0xD ENTER 键
0x10 SHIFT 键
0x11 CTRL 键
0x12 MENU 键
0x13 PAUSE 键
0x14 CAPS LOCK 键
0x1B ESC 键
0x20 SPACEBAR 键
0x21 PAGE UP 键
0x22 PAGE DOWN 键
0x23 END 键
0x24 HOME 键
0x25 LEFT ARROW 键
0x26 UP ARROW 键
0x27 RIGHT ARROW 键
0x28 DOWN ARROW 键
0x29 SELECT 键
0x2A PRINT SCREEN 键
0x2B EXECUTE 键
0x2C SNAPSHOT 键
0x2D INSERT 键
0x2E DELETE 键
0x2F HELP 键
0x90 NUM LOCK 键


A 至 Z 键与 A - Z 字母的 ASCII 码相同:
值 描述
65 A 键
66 B 键
67 C 键
68 D 键
69 E 键
70 F 键
71 G 键
72 H 键
73 I 键
74 J 键
75 K 键
76 L 键
77 M 键
78 N 键
79 O 键
80 P 键
81 Q 键
82 R 键
83 S 键
84 T 键
85 U 键
86 V 键
87 W 键
88 X 键
89 Y 键
90 Z 键


0 至 9 键与数字 0 - 9 的 ASCII 码相同:
值 描述
48 0 键
49 1 键
50 2 键
51 3 键
52 4 键
53 5 键
54 6 键
55 7 键
56 8 键
57 9 键


下列常数代表数字键盘上的键:
值 描述
0x60 0 键
0x61 1 键
0x62 2 键
0x63 3 键
0x64 4 键
0x65 5 键
0x66 6 键
0x67 7 键
0x68 8 键
0x69 9 键
0x6A MULTIPLICATION SIGN (*) 键
0x6B PLUS SIGN (+) 键
0x6C ENTER 键
0x6D MINUS SIGN (-) 键
0x6E DECIMAL POINT (.) 键
0x6F DIVISION SIGN (/) 键


下列常数代表功能键:
值 描述
0x70 F1 键
0x71 F2 键
0x72 F3 键
0x73 F4 键
0x74 F5 键
0x75 F6 键
0x76 F7 键
0x77 F8 键
0x78 F9 键
0x79 F10 键
0x7A F11 键
0x7B F12 键
0x7C F13 键
0x7D F14 键
0x7E F15 键
0x7F F16 键

你可能感兴趣的:(Windows)