B4J Question Error in send key

omidaghakhani1368

Well-Known Member
Licensed User
Hi.
I write below code to send key of string "omid"
But when i run it,send key not omid string

B4X:
Sub AppStart (Form1 As Form, Args() As String)
Dim s1 As String = "omid"
For i = 0 To s1.Length - 1
    SendKey(Asc(s1.CharAt(i)))
Next
End Sub

Sub SendKey(code As Int)
    robot.RunMethod("keyPress", Array As Object(code))
    Sleep(100)
    robot.RunMethod("keyRelease", Array As Object(code))
End Sub
 

Daestrum

Well-Known Member
Licensed User
I played around with the robot keypress routine a while back.

You have to send the uppercase letter to print the lowercase one ( odd I know ), ie ("KeyPress",array("S")) will print 's'

If you really want uppercase 'S' then you have send character code 0x10 (shift) then 'S' to produce an 'S'

I just put the codes into a map then read by name the character code I needed ie
B4X:
' Key Event key codes.
    m.put("VK_UNDEFINED",0x0)
    m.put("VK_ENTER",0x0A)
    m.put("VK_BACKSPACE",0x08)
    m.put("VK_TAB",0x09)
    'm.put("VK_CANCEL",0x03)
    m.put("VK_CLEAR",0x0C)
    m.put("VK_PAUSE",0x13)
    m.put("VK_ESCAPE",0x1B)
    m.put("VK_SPACE",0x20)
    m.put("VK_DELETE",0x7F)
    m.put("VK_PRINTSCREEN",0x9A)
    m.put("VK_INSERT",0x9B)
    m.put("VK_HELP",0x9C)
    m.put("VK_SHIFT",0x10)
.....
Some characters actually require you to send 3 key strokes, then 3 more to release the keys.

Hope it helps.

btw, if you changed your code from s1.CharAt(i) to s1.ToUpperCase.CharAt(i) it would print
 
Last edited:

omidaghakhani1368

Well-Known Member
Licensed User
I played around with the robot keypress routine a while back.

You have to send the uppercase letter to print the lowercase one ( odd I know ), ie ("KeyPress",array("S")) will print 's'

If you really want uppercase 'S' then you have send character code 0x10 (shift) then 'S' to produce an 'S'

I just put the codes into a map then read by name the character code I needed ie
B4X:
' Key Event key codes.
    m.put("VK_UNDEFINED",0x0)
    m.put("VK_ENTER",0x0A)
    m.put("VK_BACKSPACE",0x08)
    m.put("VK_TAB",0x09)
    'm.put("VK_CANCEL",0x03)
    m.put("VK_CLEAR",0x0C)
    m.put("VK_PAUSE",0x13)
    m.put("VK_ESCAPE",0x1B)
    m.put("VK_SPACE",0x20)
    m.put("VK_DELETE",0x7F)
    m.put("VK_PRINTSCREEN",0x9A)
    m.put("VK_INSERT",0x9B)
    m.put("VK_HELP",0x9C)
    m.put("VK_SHIFT",0x10)
.....
Some characters actually require you to send 3 key strokes, then 3 more to release the keys.

Hope it helps.

btw, if you changed your code from s1.CharAt(i) to s1.ToUpperCase.CharAt(i) it would print
Thank you.I will test it
 

rboeck

Well-Known Member
Licensed User
Hi,

i had the same problem and use an different way: first i put the whole string in the clipboard with fx.Clipboard.setstring (st); then i use
Keystroke(86,False,True) = Ctrl + V, then Keystroke (35, False, False) - this is the End Key. So i think its much easier....
 

omidaghakhani1368

Well-Known Member
Licensed User
Hi,

i had the same problem and use an different way: first i put the whole string in the clipboard with fx.Clipboard.setstring (st); then i use
Keystroke(86,False,True) = Ctrl + V, then Keystroke (35, False, False) - this is the End Key. So i think its much easier....
I love programmer who use easy way to themselves problem :)
 

omidaghakhani1368

Well-Known Member
Licensed User
Hi,

i had the same problem and use an different way: first i put the whole string in the clipboard with fx.Clipboard.setstring (st); then i use
Keystroke(86,False,True) = Ctrl + V, then Keystroke (35, False, False) - this is the End Key. So i think its much easier....
how send 86,False,True parameter?My function send key accept one parameter
 

rboeck

Well-Known Member
Licensed User
Sorry, i did not check; i used a function to send combined strokes oin one statement:

B4X:
Sub Keystroke(x As Int,sh As Boolean,ctrl As Boolean )

    If ctrl Then
        Dim z As Int=17
        robot.RunMethod("keyPress",Array As Object(z))
        Sleep(10)
    End If

    If sh Then
        Dim y As Int =16
        robot.RunMethod("keyPress",Array As Object(y))
        Sleep(10)
    End If
   
    robot.RunMethod("keyPress",Array As Object(x))
    Sleep(10)
    robot.RunMethod("keyRelease",Array As Object(x))
   
    If sh Then
        robot.RunMethod("keyRelease",Array As Object(y))
    End If
   
    If ctrl Then
        Dim z As Int=17
        robot.RunMethod("keyRelease",Array As Object(z))
        Sleep(10)
    End If
   
End Sub
 

omidaghakhani1368

Well-Known Member
Licensed User
Sorry, i did not check; i used a function to send combined strokes oin one statement:

B4X:
Sub Keystroke(x As Int,sh As Boolean,ctrl As Boolean )

    If ctrl Then
        Dim z As Int=17
        robot.RunMethod("keyPress",Array As Object(z))
        Sleep(10)
    End If

    If sh Then
        Dim y As Int =16
        robot.RunMethod("keyPress",Array As Object(y))
        Sleep(10)
    End If
  
    robot.RunMethod("keyPress",Array As Object(x))
    Sleep(10)
    robot.RunMethod("keyRelease",Array As Object(x))
  
    If sh Then
        robot.RunMethod("keyRelease",Array As Object(y))
    End If
  
    If ctrl Then
        Dim z As Int=17
        robot.RunMethod("keyRelease",Array As Object(z))
        Sleep(10)
    End If
  
End Sub
Thank you and last question is i use my project in mac but in mac not exist CTRL+V for paste clipboard and it is command+v
How use it?
 

rboeck

Well-Known Member
Licensed User
Good question; i tried my it on pc and linux; it worked without of any change; for mac i currently i cannot try out...
 

rboeck

Well-Known Member
Licensed User
I tried to get the right constants for the Robot keyPress event. In java i have found following
B4X:
robot.keyPress(KeyEvent.VK_CONTROL)
robot.keyPress(KeyEvent.VK_Z)
// CTRL+Z is now pressed (receiving application should see a "key down" event.)
robot.keyRelease(KeyEvent.VK_Z)
robot.keyRelease(KeyEvent.VK_CONTROL)
// CTRL+Z is now released (receiving application should now see a "key up" event - as well as a "key pressed" event)
For Java freaks: How can we get the Codes for KeyEvent.VK_CONTROL in B4J?
 

Daestrum

Well-Known Member
Licensed User
As I posted earlier, I just have a static module that contains the keycodes in a map. I then just call getCode for the keycode I want.
B4X:
Sub Process_Globals
    Private fx As JFX
    Private m As Map
End Sub
Sub init
    m.Initialize
' Key Event key codes.
    m.put("VK_UNDEFINED",0x0)
    m.put("VK_ENTER",0x0A)
    m.put("VK_BACKSPACE",0x08)
    m.put("VK_TAB",0x09)
    'm.put("VK_CANCEL",0x03)
    m.put("VK_CLEAR",0x0C)
    m.put("VK_PAUSE",0x13)
    m.put("VK_ESCAPE",0x1B)
    m.put("VK_SPACE",0x20)
    m.put("VK_DELETE",0x7F)
    m.put("VK_PRINTSCREEN",0x9A)
    m.put("VK_INSERT",0x9B)
    m.put("VK_HELP",0x9C)
    m.put("VK_SHIFT",0x10)
    m.put("VK_CONTROL",0x11)
    m.put("VK_ALT",0x12)
    m.put("VK_ALT_GRAPH",0xFF7E)
    m.put("VK_WINDOWS",0x020C)
    m.put("VK_CONTEXT_MENU",0x020D)
    m.put("VK_CAPS_LOCK",0x14)
    m.put("VK_NUM_LOCK",0x90)
    m.put("VK_SCROLL_LOCK",0x91)
    m.put("VK_COMMAND",0x0300)
    m.put("VK_PAGE_UP",0x21)
    m.put("VK_PAGE_DOWN",0x22)
    m.put("VK_END",0x23)
    m.put("VK_HOME",0x24)
    m.put("VK_LEFT",0x25)
    m.put("VK_UP",0x26)
    m.put("VK_RIGHT",0x27)
    m.put("VK_DOWN",0x28)
    m.put("VK_COMMA",0x2C)'','
    m.put("VK_MINUS",0x2D)''-'
    m.put("VK_PERIOD",0x2E)''.'
    m.put("VK_SLASH",0x2F)''/'
    m.put("VK_SEMICOLON",0x3B)'')'
    m.put("VK_EQUALS",0x3D)''",'
    m.put("VK_OPEN_BRACKET",0x5B)''['
    m.put("VK_BACK_SLASH",0x5C)''\'
    m.put("VK_CLOSE_BRACKET",0x5D)'']'
    m.put("VK_MULTIPLY",0x6A)''*'
    m.put("VK_PLUS",0x6B)''+'
    m.put("VK_SEPARATOR",0x6C)
    m.put("VK_SUBTRACT",0x6D)
    m.put("VK_DECIMAL",0x6E)
    m.put("VK_DIVIDE",0x6F)
    m.put("VK_AMPERSAND",0x96)
    m.put("VK_ASTERISK",0x97)
    m.put("VK_DOUBLE_QUOTE",0x98)''"'
    m.put("VK_LESS",0x99)''<'
    m.put("VK_GREATER",0xa0)''>'
    m.put("VK_BRACELEFT",0xa1)''{'
    m.put("VK_BRACERIGHT",0xa2)''}'
    m.put("VK_BACK_QUOTE",0xC0)''`'
    m.put("VK_QUOTE",0xDE)''''
    m.put("VK_AT",0x0200)''@'
    m.put("VK_COLON",0x0201)'':'
    m.put("VK_CIRCUMFLEX",0x0202)''^'
    m.put("VK_DOLLAR",0x0203)''$'
    m.put("VK_EURO_SIGN",0x0204)
    m.put("VK_EXCLAMATION",0x0205)''!'
    m.put("VK_INV_EXCLAMATION",0x0206)
    m.put("VK_LEFT_PARENTHESIS",0x0207)''('
    m.put("VK_NUMBER_SIGN",0x0208)''#'
    m.put("VK_PLUS",0x0209)''+'
    m.put("VK_RIGHT_PARENTHESIS",0x020A)'')'
    m.put("VK_UNDERSCORE",0x020B)''_'
    m.put("VK_0",0x30)''0'
    m.put("VK_1",0x31)''1'
    m.put("VK_2",0x32)''2'
    m.put("VK_3",0x33)''3'
    m.put("VK_4",0x34)''4'
    m.put("VK_5",0x35)''5'
    m.put("VK_6",0x36)''6'
    m.put("VK_7",0x37)''7'
    m.put("VK_8",0x38)''8'
    m.put("VK_9",0x39)''9'
    m.put("VK_A",0x41)''A'
    m.put("VK_B",0x42)''B'
    m.put("VK_C",0x43)''C'
    m.put("VK_D",0x44)''D'
    m.put("VK_E",0x45)''E'
    m.put("VK_F",0x46)''F'
    m.put("VK_G",0x47)''G'
    m.put("VK_H",0x48)''H'
    m.put("VK_I",0x49)''I'
    m.put("VK_J",0x4A)''J'
    m.put("VK_K",0x4B)''K'
    m.put("VK_L",0x4C)''L'
    m.put("VK_M",0x4D)''M'
    m.put("VK_N",0x4E)''N'
    m.put("VK_O",0x4F)''O'
    m.put("VK_P",0x50)''P'
    m.put("VK_Q",0x51)''Q'
    m.put("VK_R",0x52)''R'
    m.put("VK_S",0x53)''S'
    m.put("VK_T",0x54)''T'
    m.put("VK_U",0x55)''U'
    m.put("VK_V",0x56)''V'
    m.put("VK_W",0x57)''W'
    m.put("VK_X",0x58)''X'
    m.put("VK_Y",0x59)''Y'
    m.put("VK_Z",0x5A)''Z'
    m.put("VK_NUMPAD0",0x60)
    m.put("VK_NUMPAD1",0x61)
    m.put("VK_NUMPAD2",0x62)
    m.put("VK_NUMPAD3",0x63)
    m.put("VK_NUMPAD4",0x64)
    m.put("VK_NUMPAD5",0x65)
    m.put("VK_NUMPAD6",0x66)
    m.put("VK_NUMPAD7",0x67)
    m.put("VK_NUMPAD8",0x68)
    m.put("VK_NUMPAD9",0x69)
    m.put("VK_F1",0x70)
    m.put("VK_F2",0x71)
    m.put("VK_F3",0x72)
    m.put("VK_F4",0x73)
    m.put("VK_F5",0x74)
    m.put("VK_F6",0x75)
    m.put("VK_F7",0x76)
    m.put("VK_F8",0x77)
    m.put("VK_F9",0x78)
    m.put("VK_F10",0x79)
    m.put("VK_F11",0x7A)
    m.put("VK_F12",0x7B)
    m.put("VK_F13",0xF000)
    m.put("VK_F14",0xF001)
    m.put("VK_F15",0xF002)
    m.put("VK_F16",0xF003)
    m.put("VK_F17",0xF004)
    m.put("VK_F18",0xF005)
    m.put("VK_F19",0xF006)
    m.put("VK_F20",0xF007)
    m.put("VK_F21",0xF008)
    m.put("VK_F22",0xF009)
    m.put("VK_F23",0xF00A)
    m.put("VK_F24",0xF00B)
End Sub
Sub getCode(s As String) As Int
    Return m.Get("VK_"&s)
End Sub
Then I just call
B4X:
keyPressSpecial(getKey("TAB"))
B4X:
Sub keyPressSpecial(i As Int)
    robot.RunMethod("keyPress",Array(i))
    robot.RunMethod("keyRelease",Array(i))
End Sub
 

rboeck

Well-Known Member
Licensed User
Sorry, i forget to write, that we have this question only for the mac, on pc and linux my code works; but i am not able to send ctrl + v on a mac.
Thanks
Reinhard
 
Top