### Execute KeyPress

Example Subs of executing the Keyevents KeyPressed and KeyReleased.
The keys are executed using Java API methods as defined in Awt Robot:
API References:
Robot: http://docs.oracle.com/javase/8/docs/api/java/awt/Robot.html
KeyEvent: http://docs.oracle.com/javase/8/docs/api/java/awt/event/KeyEvent.html
KeyRobot
'Execute keyevents keypressed and keyreleased
'Parameter: Key like TAB, A, 1
'If key not found, an exception message is display in the log
'Example: KeyRobot("H"), KeyRobot("ENTER")
`Sub KeyRobot(key As String)
'Get the java keyvalue - convert key into string VK_
Dim keyvalue As Int = -1
Dim joKeyEvent As JavaObject
key = "VK_" & key
joKeyEvent.InitializeStatic("java.awt.event.KeyEvent")
Try
keyvalue = joKeyEvent.GetField(key)
Catch
Log(LastException.Message)
End Try
If keyvalue = -1 Then Return
'Execute the keypress and release events
Dim joRobot As JavaObject
joRobot.InitializeNewInstance("java.awt.Robot", Null)
Try
joRobot.RunMethod("keyPress", Array(keyvalue))
joRobot.RunMethod("keyRelease", Array(keyvalue))
Catch
Log(LastException.Message)
End Try
End Sub`
KeyRobot2
'Execute keyevents keypressed and keyreleased and handled ascii chars Uppercase and Lowercase
'Parameter: Key like TAB, A, 1
'If key not found, an exception message is display in the log
'Example: KeyRobot2("H"), KeyRobot("ENTER")
`Sub KeyRobot2(key As String)
Dim isUpperCase As Boolean = False
Dim joRobot As JavaObject
joRobot.InitializeNewInstance("java.awt.Robot", Null)
'Get the java keyvalue - convert key into string VK_
Dim keyvalue As Int = GetKeyEventValue("VK_" & key.ToUpperCase)
If keyvalue = -1 Then Return
'Check if the char is a uppercase
If key.Length = 1 Then
Dim ascvalue As Int = Asc(key)
If ascvalue >= 65 And ascvalue <= 90 Then isUpperCase = True
'Check if uppercase
If isUpperCase Then
Dim keyvalueshift As Int = GetKeyEventValue("VK_SHIFT")
joRobot.RunMethod("keyPress", Array(keyvalueshift))
joRobot.RunMethod("keyPress", Array(keyvalue))
joRobot.RunMethod("keyRelease", Array(keyvalue))
joRobot.RunMethod("keyRelease", Array(keyvalueshift))
Else
joRobot.RunMethod("keyPress", Array(keyvalue))
joRobot.RunMethod("keyRelease", Array(keyvalue))
End If
Else
joRobot.RunMethod("keyPress", Array(keyvalue))
joRobot.RunMethod("keyRelease", Array(keyvalue))
End If
End Sub`
GetKeyEventValue
'The key is defined in the KeyEvents, like VK_TAB which returns 9
'If key is not foud, -1 is returned
`Sub GetKeyEventValue(key As String) As Int
Dim result As Int = -1
Dim joKeyEvent As JavaObject
joKeyEvent.InitializeStatic("java.awt.event.KeyEvent")
Try
result = joKeyEvent.GetField(key)
Catch
Log(LastException.Message)
End Try
Return result
End Sub`

Changed: 2018-04-22 09:01, Category: KeyEvents, File: T6C211M1542.htm, ID: 1542