---
title: "关于 SENDKEYS 的代码"
author: "cjc"
date: "2005-09-26T07:03:00Z"
tags: []
slug: "sendkeys"
draft: false
lastmod: "2005-09-26T07:03:00Z"
id: 299157
url: "https://cjc.iteye.com/blog/299157"
---

sendkeys 的用法（MSDN）：

# [SendKeys 语句]()

                     

[将一个或多个按键消息发送到活动窗口，就如同在键盘上进行输入一样。  
\
**语法**  
\
**SendKeys** ***string***\[, ***wait***\]]()

[**SendKeys** 语句的语法具有以下几个]()[命名参数]()：

部分 描述 ***string*** 必需的。[字符串表达式]()，指定要发送的按键消息。 ***Wait*** 可选的。指定等待方式的 BooleandefBooleanDataType@veendf98.chm 值。如果为 **False**（缺省值），则控件在按键发送出去之后立刻返回到[过程]()。如果为 **True**，则按键消息必须在控件返回到过程之前加以处理。

 

**说明**

每个按键由一个或多个字符表示。为了指定单一键盘字符，必须按字符本身的键。例如，为了表示字母 A，可以用 `"A"` 作为 ***string***。为了表示多个字符，就必须在字符后面直接加上另一个字符。例如，要表示 A、B 及 C，可用 `"ABC"` 作为 ***string***。

对 **SendKeys** 来说，加号 (**+**)、插入符 (**^**)、百分比符号 (**%**)、上划线 (**~**) 及圆括号 **( )** 都具有特殊意义。为了指定上述任何一个字符，要将它放在大括号 ({}) 当中。例如，要指定正号，可用 {`+}` 表示。方括号 (\[ ]) 对 **SendKeys** 来说并不具有特殊意义，但必须将它们放在大括号中。在其它应用程序中，方括号有特殊意义，在出现[动态数据交换 (DDE)]() 的时候，它可能具有重要意义。为了指定大括号字符，请使用 {{}` `及 {}}。

为了在按下按键时指定那些不显示的字符，例如 ENTER 或 TAB 以及那些表示动作而非字符的按键，请使用下列代码：

按键 代码 BACKSPACE {`BACKSPACE}`, {`BS}`, 或 {`BKSP}` BREAK {`BREAK}` CAPS LOCK {`CAPSLOCK}` DEL or DELETE {`DELETE}` 或 {`DEL}` DOWN ARROW {`DOWN}` END {`END}` ENTER {`ENTER}`或 `~` ESC {`ESC}` HELP {`HELP}` HOME {`HOME}` INS or 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}`

 

为了指定那些与 SHIFT、CTRL 及 ALT 等按键结合的组合键，可在这些按键码的前面放置一个或多个代码，这些代码列举如下：

按键 代码 SHIFT `+` CTRL `^` ALT `%`

 

为了说明在按下其它按键时应同时按下 SHIFT、CTRL、及 ALT 的任意组合键，请把那些按键的码放在括号当中。例如，为了说明按下 E 与 C 的时候同时按下 SHIFT 键，请使用 "`+(EC)`"。为了说明在按下 E 的时候同时按下 SHIFT 键，但接着按 C 而不按 SHIFT，则使用 "`+EC`"。

为了指定重复键，使用 {`key number}`的形式。必须在 `key` 与 `number` 之间放置一个空格。例如，{`LEFT 42}` 意指 42 次按下 LEFT ARROW 键；{`h 10}` 则是指 10 次按下 H 键。

**注意** 不能用 **SendKeys** 将按键消息发送到这样一个应用程序，这个应用程序并没有被设计成在 Microsoft Windows or Macintosh中运行。**Sendkeys** 也无法将 PRINT SCREEN 按键 {`PRTSC`} 发送到任何应用程序。

我们如果想发送键命令，可以用 SENDKEYS ，但要发送 WINDOWS 微标键怎么做？（MSDN中没有给出WINDOWS键 的键码）  
下面是可以执行的代码:

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Private Declare Sub keybd\_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Private Const VK\_LWIN = &H5B 'Left Windows key (Microsoft Natural keyboard)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Private Const VK\_RWIN = &H5C 'Right Windows key (Natural keyboard)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Private Sub Command1\_Click() 'left windows  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)keybd\_event VK\_LWIN, 0, &H1, 0  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)keybd\_event VK\_LWIN, 0, &H2, 0  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)End Sub  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Private Sub Command2\_Click() 'right windows  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)keybd\_event VK\_RWIN, 0, &H1, 0  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)keybd\_event VK\_RWIN, 0, &H2, 0  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)End Sub

 

详细的解释可参看SENDKEYS 类（[http://www.vbaccelerator.com/home/VB/Tips/SendKeys\_using\_the\_API/SendKeys\_Demonstration\_zip\_cSendKeys\_cls.asp](http://www.vbaccelerator.com/home/VB/Tips/SendKeys_using_the_API/SendKeys_Demonstration_zip_cSendKeys_cls.asp)）：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)vbAccelerator - Contents of code file: cSendKeys.clsVERSION 1.0 CLASS  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)BEGIN  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  MultiUse = -1  'True  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)END  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Attribute VB\_Name = "cSendKeys"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Attribute VB\_GlobalNameSpace = False  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Attribute VB\_Creatable = True  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Attribute VB\_PredeclaredId = False  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Attribute VB\_Exposed = False  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Option Explicit  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Public Enum MoreKeyConstants  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_LWIN = &H5B 'Left Windows key (Microsoft Natural keyboard)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_RWIN = &H5C 'Right Windows key (Natural keyboard)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_APPS = &H5D 'Applications key (Natural keyboard)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_SLEEP = &H5F 'Computer Sleep key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_RMENU = &HA5 ' Right MENU key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_BROWSER\_BACK = &HA6 'Windows 2000/XP: Browser Back key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_BROWSER\_FORWARD = &HA7 'Windows 2000/XP: Browser Forward key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_BROWSER\_REFRESH = &HA8 'Windows 2000/XP: Browser Refresh key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_BROWSER\_STOP = &HA9 'Windows 2000/XP: Browser Stop key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_BROWSER\_SEARCH = &HAA 'Windows 2000/XP: Browser Search key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_BROWSER\_FAVORITES = &HAB 'Windows 2000/XP: Browser Favorites key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_BROWSER\_HOME = &HAC 'Windows 2000/XP: Browser Start and Home key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_VOLUME\_MUTE = &HAD 'Windows 2000/XP: Volume Mute key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_VOLUME\_DOWN = &HAE  'Windows 2000/XP: Volume Down key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_VOLUME\_UP = &HAF  'Windows 2000/XP: Volume Up key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_MEDIA\_NEXT\_TRACK = &HB0  'Windows 2000/XP: Next Track key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_MEDIA\_PREV\_TRACK = &HB1  'Windows 2000/XP: Previous Track key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_MEDIA\_STOP = &HB2  'Windows 2000/XP: Stop Media key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_MEDIA\_PLAY\_PAUSE = &HB3  'Windows 2000/XP: Play/Pause Media key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_LAUNCH\_MAIL = &HB4  'Windows 2000/XP: Start Mail key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_LAUNCH\_MEDIA\_SELECT = &HB5  'Windows 2000/XP: Select Media key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_LAUNCH\_APP1 = &HB6  'Windows 2000/XP: Start Application 1 key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_LAUNCH\_APP2 = &HB7  'Windows 2000/XP: Start Application 2 key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_OEM\_1 = &HBA 'Used for miscellaneous characters; it can vary by keyboard.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    Windows 2000/XP: For the US standard keyboard, the ';:' key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_OEM\_PLUS = &HBB 'Windows 2000/XP: For any country/region, the '+' key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_OEM\_COMMA = &HBC 'Windows 2000/XP: For any country/region, the ',' key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_OEM\_MINUS = &HBD 'Windows 2000/XP: For any country/region, the '-' key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_OEM\_PERIOD = &HBE 'Windows 2000/XP: For any country/region, the '.' key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_OEM\_2 = &HBF 'Used for miscellaneous characters; it can vary by keyboard.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    Windows 2000/XP: For the US standard keyboard, the '/?' key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_OEM\_3 = &HC0 'Used for miscellaneous characters; it can vary by keyboard.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    Windows 2000/XP: For the US standard keyboard, the '\`~' key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)'  C1D7 Reserved  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)'  D8DA Unassigned  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_OEM\_4 = &HDB 'Used for miscellaneous characters; it can vary by keyboard.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    Windows 2000/XP: For the US standard keyboard, the '[{' key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_OEM\_5 = &HDC 'Used for miscellaneous characters; it can vary by keyboard.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    Windows 2000/XP: For the US standard keyboard, the '|' key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_OEM\_6 = &HDD 'Used for miscellaneous characters; it can vary by keyboard  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    Windows 2000/XP: For the US standard keyboard, the ']}' key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_OEM\_7 = &HDE ' Used for miscellaneous characters; it can vary by  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    keyboard. Windows 2000/XP: For the US standard keyboard, the  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    'single-quote/double-quote' key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_OEM\_8 = &HDF 'Used for miscellaneous characters; it can vary by keyboard.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      E0 Reserved  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)'- E1 OEM specific  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_OEM\_102 = &HE2 'Windows 2000/XP: Either the angle bracket key or the  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    backslash key on the RT 102-key keyboard  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)' E3E4 OEM specific  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_PROCESSKEY = &HE5 'Windows 95/98/Me, Windows NT 4.0, Windows 2000/XP: IME  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    PROCESS key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)' E6 OEM specific  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_PACKET = &HE7 'Windows 2000/XP: Used to pass Unicode characters as if  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    they were keystrokes. The VK\_PACKET key is the low word of a 32-bit Virtual  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    Key value used for non-keyboard input methods. For more information, see  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    Remark in KEYBDINPUT, SendInput, WM\_KEYDOWN, and WM\_KEYUP  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)'  E8 Unassigned  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)' E9F5 OEM specific  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_ATTN = &HF6 'Attn key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_CRSEL = &HF7 'CrSel key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_EXSEL = &HF8 'ExSel key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_EREOF = &HF9 'Erase EOF key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_PLAY = &HFA 'Play key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_ZOOM = &HFB 'Zoom key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_NONAME = &HFC 'Reserved for future use  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_PA1 = &HFD 'PA1 key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   VK\_OEM\_CLEAR = &HFE 'Clear key  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)End Enum  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Private m\_colKeyMap As New Collection  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Private Declare Sub keybd\_event Lib "user32" ( _  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   ByVal bVk As Byte, ByVal bScan As Byte, _  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   ByVal dwFlags As Long, ByVal dwExtraInfo As Long)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Private Const KEYEVENTF\_EXTENDEDKEY = &H1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Private Const KEYEVENTF\_KEYUP = &H2  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Private Declare Function GetVersion Lib "kernel32" () As Long  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" ( _  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   ByVal cChar As Byte) As Integer  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Private Declare Function VkKeyScanW Lib "user32" ( _  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   ByVal cChar As Integer) As Integer  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Private Function nextChar(ByRef sString As String, ByVal iPos As Long, Optional  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) ByVal lLen As Long = 0) As String  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   If (lLen = 0) Then lLen = Len(sString)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   If (iPos + 1 &lt;= lLen) Then  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)