PressKeyMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法
描述
模拟通过键盘按下一个按键并立即释放。
语法
object.PressKey( key )
参数
object : Mercury.DeviceReplay对象。
key : 按键的数值码。可查阅后面的“Key Codes 参考”。
返回值
无。
例子
下面的例子会激活记事本并使用热键CTRL+O来模拟选择文件打开菜单,然后用ESCAPE按键关闭对话框。
Const VK_O = 24 : Const VK_F = 33
Const VK_CONTROL = 29 : Const VK_ESCAPE = 1 : Const VK_MENU = 56
Set deviceReplay = CreateObject( "Mercury.DeviceReplay" )
SystemUtil.Run "notepad.exe", "", "", "open"
Window( "nativeclass:=Notepad", "index:=0" ).Activate micLeftBtn
Wait 1
' ** Opening the menu Alt + F + O
deviceReplay.PressKey VK_MENU
deviceReplay.PressKey VK_F
deviceReplay.PressKey VK_O
Wait 2
' ** Closing the menu
deviceReplay.PressKey VK_ESCAPE
deviceReplay.SendString "Open menu was closed."
Set deviceReplay = Nothing
PressNKeys方法
描述
模拟通过键盘多次按下一个按键并立即释放。
语法
object.PressNKey( key, N )
参数
object : Mercury.DeviceReplay对象。
key : 按键的数值码。可查阅后面的“Key Codes 参考”。
N:重复的次数。
返回值
无。
例子
例1 – 美国的州
Option Explicit
Const VK_RETURN = 28 : Const VK_F = 33 : Const VK_O = 24
Const VK_TAB = 15 : Const VK_F5 = 63
Const VK_CAPITAL = 58 : Const VK_NUMLOCK = 69
Const VK_SUBTRACT = 74 : Const VK_MULTIPLY = 55
Const VK_MENU = 56
Dim deviceReplay
Private Sub SetupKeyboard()
Const CLASS_NAME = "Microsoft.VisualBasic.Devices.Keyboard"
Const ASSEMBLY = "Microsoft.VisualBasic"
Dim Keyboard
Set Keyboard = DotNetFactory.CreateInstance( CLASS_NAME, ASSEMBLY )
If CBool( Keyboard.CapsLock ) Then
deviceReplay.PressKey VK_CAPITAL
End If
If CBool( Keyboard.NumLock ) = False Then
deviceReplay.PressKey VK_NUMLOCK
End If
Set Keyboard = Nothing
End Sub
Private Sub SetupNotepad()
deviceReplay.PressKey VK_MENU
deviceReplay.PressKey VK_O
deviceReplay.PressKey VK_F
deviceReplay.SendString "Courier New"
deviceReplay.PressKey VK_TAB
deviceReplay.PressKey VK_TAB
deviceReplay.SendString "14"
deviceReplay.PressKey VK_RETURN
Wait 1
End Sub
Private Sub PrintRow( ByVal state, ByVal usps, byVal capital )
deviceReplay.SendString state
deviceReplay.PressKey VK_TAB
If Len( state ) < 8 Then
deviceReplay.PressKey VK_TAB
End If
deviceReplay.SendString usps
deviceReplay.PressKey VK_TAB
deviceReplay.SendString capital
deviceReplay.PressKey VK_RETURN
End Sub
Set deviceReplay = CreateObject( "Mercury.DeviceReplay" )
SystemUtil.Run "notepad.exe", "", "", "open", 3
Window( "nativeclass:=Notepad", "index:=0" ).Activate micLeftBtn
' ** Setup Notepad - Font courier new, size 14,
' ** NUM-LOCK pressed and CAPS-LOCK unpressed
Call SetupKeyboard()
Call SetupNotepad()
' ** inserting date
deviceReplay.PressKey VK_F5
deviceReplay.PressKey VK_RETURN
' ** Inserting Title
deviceReplay.PressNKeys VK_TAB, 3
deviceReplay.SendString "
deviceReplay.PressKey VK_RETURN
deviceReplay.PressNKeys VK_TAB, 3
deviceReplay.PressNKeys VK_MULTIPLY, Len( "
deviceReplay.PressNKeys VK_RETURN, 2
' ** Table Headers
deviceReplay.SendString "State"
deviceReplay.PressKey VK_TAB
deviceReplay.PressKey VK_TAB
deviceReplay.SendString "USPS"
deviceReplay.PressKey VK_TAB
deviceReplay.SendString "Capital"
deviceReplay.PressKey VK_RETURN
deviceReplay.PressNKeys VK_SUBTRACT, 31
deviceReplay.PressKey VK_RETURN
' ** Print Data
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "Georgia", "GA", "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "Indiana", "IN", "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "Virginia", "VA", "
Call PrintRow( "Washington", "WA", "
Call PrintRow( "
Call PrintRow( "
Call PrintRow( "
Set deviceReplay = Nothing
例2 – 拉丁文和字符
Option Explicit
Const VK_NUMPAD0 = 82
Const VK_NUMPAD1 = 79
Const VK_NUMPAD2 = 80
Const VK_NUMPAD3 = 81
Const VK_NUMPAD4 = 75
Const VK_NUMPAD5 = 76
Const VK_NUMPAD6 = 77
Const VK_NUMPAD7 = 71
Const VK_NUMPAD8 = 72
Const VK_NUMPAD9 = 73
Const VK_MENU = 56
Const VK_SHIFT = 42
Const VK_RETURN = 28
Const VK_F = 33
Const VK_O = 24
Const VK_TAB = 15
Const VK_F5 = 63
Const VK_NUMLOCK = 69
Dim deviceReplay
Private Sub SetupKeyboard()
Const CLASS_NAME = "Microsoft.VisualBasic.Devices.Keyboard"
Const ASSEMBLY = "Microsoft.VisualBasic"
Dim Keyboard
Set Keyboard = DotNetFactory.CreateInstance( CLASS_NAME, ASSEMBLY )
If CBool( Keyboard.CapsLock ) Then
deviceReplay.PressKey VK_CAPITAL
End If
If CBool( Keyboard.NumLock ) = False Then
deviceReplay.PressKey VK_NUMLOCK
End If
Set Keyboard = Nothing
End Sub
Private Sub SetupNotepad()
deviceReplay.PressKey VK_MENU
deviceReplay.PressKey VK_O
deviceReplay.PressKey VK_F
deviceReplay.SendString "Courier New"
deviceReplay.PressKey VK_TAB
deviceReplay.PressKey VK_TAB
deviceReplay.SendString "14"
deviceReplay.PressKey VK_RETURN
Wait 1
End Sub
Private Sub PrintCharacter( ByVal code )
Dim i, digit
deviceReplay.KeyDown VK_MENU
For i = 1 To Len( code )
digit = Mid( code, i, 1 )
Execute "deviceReplay.PressKey VK_NUMPAD" & digit
Next
deviceReplay.KeyUp VK_MENU
deviceReplay.PressKey VK_RETURN
End Sub
Set deviceReplay = CreateObject( "Mercury.DeviceReplay" )
SystemUtil.Run "notepad.exe", "", "", "open", 3
Window( "nativeclass:=Notepad", "index:=0" ).Activate micLeftBtn
' ** Setup Notepad - Font courier new, size 14,
' ** NUM-LOCK pressed and CAPS-LOCK unpressed
Call SetupKeyboard()
Call SetupNotepad()
' ** inserting date
deviceReplay.PressKey VK_F5
deviceReplay.PressKey VK_RETURN
' ** a grave character
deviceReplay.SendString "A grave: "
Call PrintCharacter( "0192" )
' ** O circumflex character
deviceReplay.SendString "O circumflex: "
Call PrintCharacter( "0212" )
' ** s caron character
deviceReplay.SendString "s caron: "
Call PrintCharacter( "0154" )
' ** n tilde character
deviceReplay.SendString "n tilde: "
Call PrintCharacter( "164" )
' ** Y umlaut character
deviceReplay.SendString "Y umlaut: "
Call PrintCharacter( "0159" )
' ** c cedila character
deviceReplay.SendString "c cedila: "
Call PrintCharacter( "0231" )
' ** O with accent character
deviceReplay.SendString "O with accent: "
Call PrintCharacter( "0211" )
' ** Inverted question mark character
deviceReplay.SendString "Inverted question mark: "
Call PrintCharacter( "168" )
' ** Euro character
deviceReplay.SendString "Euro: "
Call PrintCharacter( "0128" )
' ** i with accent character
deviceReplay.SendString "i with accent : "
Call PrintCharacter( "0237" )
' ** Male Sign character
deviceReplay.SendString "Male Sign: "
Call PrintCharacter( "11" )
' ** AE ligature character
deviceReplay.SendString "AE ligature: "
Call PrintCharacter( "0198" )
' ** aa character
deviceReplay.SendString "aa: "
Call PrintCharacter( "0197" )
' ** oethel character
deviceReplay.SendString "oethel: "
Call PrintCharacter( "0156" )
' ** Eth character
deviceReplay.SendString "Eth: "
Call PrintCharacter( "0208" )
' ** Uppercase Sigma character
deviceReplay.SendString "Uppercase Sigma: "
Call PrintCharacter( "228" )
Set deviceReplay = Nothing
文章来源于领测软件测试网 https://www.ltesting.net/