sendkeys SendKeys string[, wait]The SendKeys statement syntax has these named arguments:Part Description 
string Required. String expression specifying the keystrokes to send. 
Wait Optional. Boolean value specifying the wait mode. If False (default), control is returned to the procedure immediately after the keys are sent. If True, keystrokes must be processed before control is returned to the procedure. 
ResEach key is represented by one or more characters. To specify a single keyboard character, use the character itself. For example, to represent the letter A, use "A" for string. To represent more than one character, append each additional character to the one preceding it. To represent the letters A, B, and C, use "ABC" for string.The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses ( ) have special meanings to SendKeys. To specify one of these characters, enclose it within braces ({}). For example, to specify the plus sign, use {+}. Brackets ([ ]) have no special meaning to SendKeys, but you must enclose them in braces. In other applications, brackets do have a special meaning that may be significant when dynamic data exchange (DDE) occurs. To specify brace characters, use {{} and {}}.To specify characters that aren't displayed when you press a key, such as ENTER or TAB, and keys that represent actions rather than characters, use the codes shown below:Key Code 
BACKSPACE {BACKSPACE}, {BS}, or {BKSP} 
BREAK {BREAK} 
CAPS LOCK {CAPSLOCK} 
DEL or DELETE {DELETE} or {DEL} 
DOWN ARROW {DOWN} 
END {END} 
ENTER  {ENTER}or ~ 
ESC {ESC} 
HELP {HELP} 
HOME {HOME} 
INS or INSERT {INSERT} or {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} 
To specify keys combined with any combination of the SHIFT, CTRL, and ALT keys, precede the key code with one or more of the following codes:Key Code 
SHIFT + 
CTRL  ^ 
ALT % 
To specify that any combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, enclose the code for those keys in parentheses. For example, to specify to hold down SHIFT while E and C are pressed, use "+(EC)". To specify to hold down SHIFT while E is pressed, followed by C without SHIFT, use "+EC".To specify repeating keys, use the form {key number}. You must put a space between key and number. For example, {LEFT 42} means press the LEFT ARROW key 42 times; {h 10} means press H 10 times.Note   You can't use SendKeys to send keystrokes to an application that is not designed to run in Microsoft Windows. Sendkeys also can't send the PRINT SCREEN key {PRTSC} to any application.

解决方案 »

  1.   


    SendKeys 语句
          将一个或多个按键消息发送到活动窗口,就如同在键盘上进行输入一样。语法SendKeys string[, wait]SendKeys 语句的语法具有以下几个命名参数:部分 描述 
    string 必需的。字符串表达式,指定要发送的按键消息。 
    Wait 可选的。指定等待方式的 [email protected] 值。如果为 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 中运行。Sendkeys 也无法将 PRINT SCREEN 按键 {PRTSC} 发送到任何应用程序。
      

  2.   

    用keybd_event API:
    Const VK_H = 72
    Const VK_E = 69
    Const VK_L = 76
    Const VK_O = 79
    Const KEYEVENTF_EXTENDEDKEY = &H1
    Const KEYEVENTF_KEYUP = &H2
    Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    Private Sub Form_KeyPress(KeyAscii As Integer)
        'Print the key on the form
        Me.Print Chr$(KeyAscii);
    End Sub
    Private Sub Form_Paint()
        'KPD-Team 2000
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        'Clear the form
        Me.Cls
        keybd_event VK_H, 0, 0, 0   ' press H
        keybd_event VK_H, 0, KEYEVENTF_KEYUP, 0   ' release H
        keybd_event VK_E, 0, 0, 0  ' press E
        keybd_event VK_E, 0, KEYEVENTF_KEYUP, 0  ' release E
        keybd_event VK_L, 0, 0, 0  ' press L
        keybd_event VK_L, 0, KEYEVENTF_KEYUP, 0  ' release L
        keybd_event VK_L, 0, 0, 0  ' press L
        keybd_event VK_L, 0, KEYEVENTF_KEYUP, 0  ' release L
        keybd_event VK_O, 0, 0, 0  ' press O
        keybd_event VK_O, 0, KEYEVENTF_KEYUP, 0  ' release O
    End Sub
      

  3.   

    真是对不起楼上各位,我不能通过sendkeys发送键击,因为我真正的代码不是在VB中编写的,所以一定要通过API函数实现.
     turbochen的关于keybd_event 的例子很好,谢谢!
    希望各位高手继续讨论.
      

  4.   

    来晚了!同意gump2000(阿甘) 的做法,简单实用!
      

  5.   

    问题归结为:怎么用API keybd_event() 实现发送Alt+e联合键击
      

  6.   

    keybd_event() 和 SENDKEYS 效果一样
      

  7.   

    鎴戝湪鍏朵粬鐨処DE鐜涓嬪疄鐜板彂閫丄lt+e鑱斿悎閿嚮,鍥犳sendkeys 鍑芥暟鐢ㄤ笉涓
      

  8.   

    我的IDE环境没有sendkeys 函数,因此只能通过API函数实现,高手们,怎么办?