关于 SENDKEYS 的代码

sendkeys 的用法(MSDN):

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} 的形式。必须在 keynumber 之间放置一个空格。例如,{LEFT 42} 意指 42 次按下 LEFT ARROW 键;{h 10} 则是指 10 次按下 H 键。

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

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

Private Declare Sub keybd_eventLib " user32 " (ByValbVk As Byte ,ByValbScan As Byte ,ByValdwFlags As Long ,ByValdwExtraInfo As Long )
Private Const VK_LWIN = & H5B ' LeftWindowskey(MicrosoftNaturalkeyboard)
Private Const VK_RWIN = & H5C ' RightWindowskey(Naturalkeyboard)
Private Sub Command1_Click() ' leftwindows
keybd_eventVK_LWIN, 0 , & H1, 0
keybd_eventVK_LWIN,
0 , & H2, 0
EndSub
Private Sub Command2_Click() ' rightwindows
keybd_eventVK_RWIN, 0 , & H1, 0
keybd_eventVK_RWIN,
0 , & H2, 0
EndSub

详细的解释可参看SENDKEYS 类(http://www.vbaccelerator.com/home/VB/Tips/SendKeys_using_the_API/SendKeys_Demonstration_zip_cSendKeys_cls.asp):

vbAccelerator - Contentsofcodefile:cSendKeys.clsVERSION 1.0 CLASS
BEGIN
MultiUse
= - 1 ' True
END
AttributeVB_Name
= " cSendKeys "
AttributeVB_GlobalNameSpace
= False
AttributeVB_Creatable
= True
AttributeVB_PredeclaredId
= False
AttributeVB_Exposed
= False
Option Explicit

Public EnumMoreKeyConstants
VK_LWIN
= & H5B ' LeftWindowskey(MicrosoftNaturalkeyboard)
VK_RWIN = & H5C ' RightWindowskey(Naturalkeyboard)
VK_APPS = & H5D ' Applicationskey(Naturalkeyboard)
VK_SLEEP = & H5F ' ComputerSleepkey

VK_RMENU
= & HA5 ' RightMENUkey
VK_BROWSER_BACK = & HA6 ' Windows2000/XP:BrowserBackkey
VK_BROWSER_FORWARD = & HA7 ' Windows2000/XP:BrowserForwardkey
VK_BROWSER_REFRESH = & HA8 ' Windows2000/XP:BrowserRefreshkey
VK_BROWSER_STOP = & HA9 ' Windows2000/XP:BrowserStopkey
VK_BROWSER_SEARCH = & HAA ' Windows2000/XP:BrowserSearchkey
VK_BROWSER_FAVORITES = & HAB ' Windows2000/XP:BrowserFavoriteskey
VK_BROWSER_HOME = & HAC ' Windows2000/XP:BrowserStartandHomekey
VK_VOLUME_MUTE = & HAD ' Windows2000/XP:VolumeMutekey
VK_VOLUME_DOWN = & HAE ' Windows2000/XP:VolumeDownkey
VK_VOLUME_UP = & HAF ' Windows2000/XP:VolumeUpkey
VK_MEDIA_NEXT_TRACK = & HB0 ' Windows2000/XP:NextTrackkey
VK_MEDIA_PREV_TRACK = & HB1 ' Windows2000/XP:PreviousTrackkey
VK_MEDIA_STOP = & HB2 ' Windows2000/XP:StopMediakey
VK_MEDIA_PLAY_PAUSE = & HB3 ' Windows2000/XP:Play/PauseMediakey
VK_LAUNCH_MAIL = & HB4 ' Windows2000/XP:StartMailkey
VK_LAUNCH_MEDIA_SELECT = & HB5 ' Windows2000/XP:SelectMediakey
VK_LAUNCH_APP1 = & HB6 ' Windows2000/XP:StartApplication1key
VK_LAUNCH_APP2 = & HB7 ' Windows2000/XP:StartApplication2key
VK_OEM_1 = & HBA ' Usedformiscellaneouscharacters;itcanvarybykeyboard.
Windows 2000 / XP: For theUSstandardkeyboard,the ' ;:'key

VK_OEM_PLUS
= & HBB ' Windows2000/XP:Foranycountry/region,the'+'key
VK_OEM_COMMA = & HBC ' Windows2000/XP:Foranycountry/region,the','key
VK_OEM_MINUS = & HBD ' Windows2000/XP:Foranycountry/region,the'-'key
VK_OEM_PERIOD = & HBE ' Windows2000/XP:Foranycountry/region,the'.'key
VK_OEM_2 = & HBF ' Usedformiscellaneouscharacters;itcanvarybykeyboard.
Windows 2000 / XP: For theUSstandardkeyboard,the ' /?'key
VK_OEM_3 = & HC0 ' Usedformiscellaneouscharacters;itcanvarybykeyboard.
Windows 2000 / XP: For theUSstandardkeyboard,the ' `~'key

' C1D7Reserved
'
D8DAUnassigned
VK_OEM_4 = & HDB ' Usedformiscellaneouscharacters;itcanvarybykeyboard.
Windows 2000 / XP: For theUSstandardkeyboard,the ' [{'key
VK_OEM_5 = & HDC ' Usedformiscellaneouscharacters;itcanvarybykeyboard.
Windows 2000 / XP: For theUSstandardkeyboard,the ' |'key
VK_OEM_6 = & HDD ' Usedformiscellaneouscharacters;itcanvarybykeyboard
Windows 2000 / XP: For theUSstandardkeyboard,the ' ]}'key
VK_OEM_7 = & HDE ' Usedformiscellaneouscharacters;itcanvaryby
keyboard.Windows 2000 / XP: For theUSstandardkeyboard,the
' single-quote/double-quote'key
VK_OEM_8 = & HDF ' Usedformiscellaneouscharacters;itcanvarybykeyboard.
E0Reserved
' -E1OEMspecific
VK_OEM_102 = & HE2 ' Windows2000/XP:Eithertheanglebracketkeyorthe
backslashkey on theRT 102 - keykeyboard
' E3E4OEMspecific
VK_PROCESSKEY = & HE5 ' Windows95/98/Me,WindowsNT4.0,Windows2000/XP:IME
PROCESSkey
' E6OEMspecific
VK_PACKET = & HE7 ' Windows2000/XP:UsedtopassUnicodecharactersasif
theywerekeystrokes.TheVK_PACKETkey is thelowwordofa 32 - bitVirtual
Keyvalueused
for non - keyboardinputmethods. For moreinformation,see
RemarkinKEYBDINPUT,SendInput,WM_KEYDOWN,
and WM_KEYUP
' E8Unassigned
'
E9F5OEMspecific
VK_ATTN = & HF6 ' Attnkey
VK_CRSEL = & HF7 ' CrSelkey
VK_EXSEL = & HF8 ' ExSelkey
VK_EREOF = & HF9 ' EraseEOFkey
VK_PLAY = & HFA ' Playkey
VK_ZOOM = & HFB ' Zoomkey
VK_NONAME = & HFC ' Reservedforfutureuse
VK_PA1 = & HFD ' PA1key
VK_OEM_CLEAR = & HFE ' Clearkey
End Enum

Private m_colKeyMap As New Collection

Private Declare Sub keybd_eventLib " user32 " (_
ByValbVk
As Byte ,ByValbScan As Byte ,_
ByValdwFlags
As Long ,ByValdwExtraInfo As Long )
Private Const KEYEVENTF_EXTENDEDKEY = & H1
Private Const KEYEVENTF_KEYUP = & H2

Private Declare Function GetVersionLib " kernel32 " () As Long
Private Declare Function VkKeyScanLib " user32 " Alias " VkKeyScanA " (_
ByValcChar
As Byte ) As Integer
Private Declare Function VkKeyScanWLib " user32 " (_
ByValcChar
As Integer ) As Integer

Private Declare Sub CopyMemoryLib " kernel32 " Alias " RtlMoveMemory " (_
lpvDest
As Any,lpvSource As Any,ByValcbCopy As Long )

Private Function nextChar(ByRefsString As String ,ByValiPos As Long ,Optional
ByVallLen
As Long = 0 ) As String
If (lLen = 0 ) Then lLen = Len (sString)
If (iPos + 1 <= lLen) Then
nextChar
= Mid $(sString,iPos + 1 , 1 )
End If
EndFunction

Public Sub SendKeys(ByValsKeys As String ,OptionalByValWait As Boolean )

' Theplussign(+),caret(^),percentsign(%),
' tilde(~),andparentheses()havespecial
' meaningstoSendKeys
' Brackets([])havenospecialmeaningtoSendKeys,
' butyoumustenclosetheminbraces.
' Tospecifybracecharacters,use{{}and{}}.
' Repeatingkeys:{LEFT42}doleft42times.

' +=Shift
' ^=Ctrl
' %=Alt
' ~=enter
' (=startsubexpression.+(EC)=ShiftthenEthenC

On Error GoTo errorHandler

Dim sMsg As String
Dim lErr As Long
Dim iPos As Long
Dim iNextPos As Long
Dim iLen As Long
Dim sChar As String
Dim colBrace As New Collection
htt
分享到:
评论

你可能感兴趣的:(windows,XP,UP,asp,vb)