技术开发 频道

揭秘QTP的DeviceReplay对象

  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 "United States of America"

  deviceReplay.PressKey VK_RETURN

  deviceReplay.PressNKeys VK_TAB, 3

  deviceReplay.PressNKeys VK_MULTIPLY, Len( "United States of America" )

  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( "Alabama", "AL", "Montgomery" )

  Call PrintRow( "Alaska", "AK", "Juneau" )

  Call PrintRow( "Arizona", "AZ", "Phoenix" )

  Call PrintRow( "Arkansas", "AR", "Little Rock" )

  Call PrintRow( "California", "CA", "Sacramento" )

  Call PrintRow( "Colorado", "CO", "Denver" )

  Call PrintRow( "Connecticut", "CT", "Hartford" )

  Call PrintRow( "Delaware", "DE", "Dover" )

  Call PrintRow( "Florida", "FL", "Tallahassee" )

  Call PrintRow( "Georgia", "GA", "Atlanta" )

  Call PrintRow( "Hawaii", "HA", "Honolulu" )

  Call PrintRow( "Idaho", "ID", "Boise" )

  Call PrintRow( "Illinois", "IL", "Springfield" )

  Call PrintRow( "Indiana", "IN", "Indianapolis" )

  Call PrintRow( "Iowa", "IA", "Des Moines" )

  Call PrintRow( "Kansas", "KS", "Topeka" )

  Call PrintRow( "Kentucky", "KY", "Frankfort" )

  Call PrintRow( "Louisiana", "LA", "Baton Rouge" )

  Call PrintRow( "Maine", "ME", "Augusta" )

  Call PrintRow( "Maryland", "MD", "Annapolis" )

  Call PrintRow( "Massachusetts", "MA", "Boston" )

  Call PrintRow( "Michigan", "MI", "Lansing" )

  Call PrintRow( "Minnesota", "MN", "Saint Paul" )

  Call PrintRow( "Mississippi", "MS", "Jackson" )

  Call PrintRow( "Missouri", "MO", "Jefferson City" )

  Call PrintRow( "Montana", "MT", "Helena" )

  Call PrintRow( "Nebraska", "NE", "Lincoln" )

  Call PrintRow( "Nevada", "NV", "Carson City" )

  Call PrintRow( "New Hampshire", "NH", "Concord" )

  Call PrintRow( "New Jersey", "NJ", "Trenton" )

  Call PrintRow( "New Mexico", "NM", "Santa Fe" )

  Call PrintRow( "New York", "NY", "Albany" )

  Call PrintRow( "North Carolina", "NC", "Raleigh" )

  Call PrintRow( "North Dakota", "ND", "Bismarck" )

  Call PrintRow( "Ohio", "OH", "Columbus" )

  Call PrintRow( "Oklahoma", "OK", "Oklahoma City" )

  Call PrintRow( "Oregon", "OR", "Salem" )

  Call PrintRow( "Pennsylvania", "PA", "Harrisburg" )

  Call PrintRow( "Rhode Island", "RI", "Providence" )

  Call PrintRow( "South Carolina", "SC", "Columbia" )

  Call PrintRow( "South Dakota", "SD", "Pierre" )

  Call PrintRow( "Tennessee", "TN", "Nashville" )

  Call PrintRow( "Texas", "TX", "Austin" )

  Call PrintRow( "Utah", "UT", "Salt Lake City" )

  Call PrintRow( "Vermont", "VT", "Montpelier" )

  Call PrintRow( "Virginia", "VA", "Richmond" )

  Call PrintRow( "Washington", "WA", "Olympia" )

  Call PrintRow( "West Virginia", "WV", "Charleston" )

  Call PrintRow( "Wisconsin", "WI", "Madison" )

  Call PrintRow( "Wyoming", "WY", "Cheyenne" )

  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

  DragAndDrop方法

  描述

  用于执行从一点拖动到另外一点的操作。

  语法

  object.DragAndDrop( dragX, dragY, dropX, dropY, Button )

  参数

  object : Mercury.DeviceReplay对象。

  dragX :起点坐标的X轴的值。

  dragY :起点坐标的Y轴的值。

  dropX :终点坐标的X轴的值。

  dropY :终点坐标的Y轴的值。

  Button :可能的值包括

  LEFT_MOUSE_BUTTON = 0

  MIDDLE_MOUSE_BUTTON = 1

  RIGHT_MOUSE_BUTTON = 2

  返回值

  无。

  提示

  可以组合使用MouseDown、MouseMove和MouseUp方法。

0
相关文章