B4J Question Simulate keypress+combos

ThRuST

Well-Known Member
Licensed User
Longtime User
Hi all, I found this native Java code how to simulate keypress which I want to convert to B4J, because I need a automatic keypress of CTRL+V keypress and I need a solution without the jAWTRobot library.

Any solution needs to support SDK v9.0.4


You can find the native Java code here
 

ThRuST

Well-Known Member
Licensed User
Longtime User
Keyboard scan codes might be useful to make key press combos work. You'll find a keytable schematic here
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Why are you trying to do this without the jAWTRobot library? Wasn't it designed specifically for this?
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
I am looking for an alternative solution just to make it work, any way possible. If I didn't experience any issue with pasting on Mac I wouldn't have posted this.
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Here's a way that works but only for single keypress. I need the key combo CTRL+V

B4X:
Dim robot As JavaObject
    Dim jo As JavaObject
    jo.InitializeStatic("com.sun.glass.ui.Application")
    robot = jo.RunMethodJO("GetApplication",Null).RunMethodJO("createRobot",Null)

    Dim i As Int =  65
  
    robot.RunMethod("keyPress", Array As Object(i))
    Sleep(50)
    robot.RunMethod("keyRelease", Array As Object(i))

Output
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
On Mac however, it's different. see the image below

upload_2019-1-5_18-1-32.png


On PC that's the "Windows" button in vmWare

winkeyb.JPG
 
Last edited:
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
This code only pastes "a" even though it's supposed to paste "@a"

B4X:
TextArea1.RequestFocus
   
    Dim robot1 As JavaObject
    Dim robot2 As JavaObject
    Dim jo1 As JavaObject
    Dim jo2 As JavaObject
    jo1.InitializeStatic("com.sun.glass.ui.Application")
    jo2.InitializeStatic("com.sun.glass.ui.Application")
    robot1 = jo1.RunMethodJO("GetApplication",Null).RunMethodJO("createRobot",Null)
    robot2 = jo2.RunMethodJO("GetApplication",Null).RunMethodJO("createRobot",Null)

    Dim i As Int =  65
    Dim j As Int =  66
 
    robot1.RunMethod("keyPress", Array As Object(i))
    robot2.RunMethod("keyPress", Array As Object(j))
    Sleep(50)
    robot1.RunMethod("keyRelease", Array As Object(i))
    robot2.RunMethod("keyRelease", Array As Object(j))

Output
 
Last edited:
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Same as

B4X:
TextArea1.RequestFocus
 
    Dim robot1 As JavaObject
    Dim robot2 As JavaObject
    Dim jo1 As JavaObject
    Dim jo2 As JavaObject
    jo1.InitializeStatic("com.sun.glass.ui.Application")
    jo2.InitializeStatic("com.sun.glass.ui.Application")
    robot1 = jo1.RunMethodJO("GetApplication",Null).RunMethodJO("createRobot",Null)
    robot2 = jo2.RunMethodJO("GetApplication",Null).RunMethodJO("createRobot",Null)

    Dim i As Int =  65
    Dim j As Int =  66
 
    robot1.RunMethod("keyPress", Array As Object(i))
    robot1.RunMethod("keyRelease", Array As Object(i))
 
    robot2.RunMethod("keyPress", Array As Object(j))
    robot2.RunMethod("keyRelease", Array As Object(j))

Output
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
B4X:
Sub AppStart (Form1 As Form, Args() As String)
 MainForm = Form1
 'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
 MainForm.Show
 textarea1.Initialize("")
 
   MainForm.RootPane.AddNode(textarea1,10,10,100,100)
 Dim robot1 As JavaObject
 Dim jo1 As JavaObject
 Dim keycodes As JavaObject
 keycodes.InitializeStatic("javafx.scene.input.KeyCode")
 jo1.InitializeStatic("com.sun.glass.ui.Application")
 robot1 = jo1.RunMethodJO("GetApplication",Null).RunMethodJO("createRobot",Null)
 textarea1.RequestFocus

' you have to think how you would type the characters
 robot1.RunMethod("keyPress", Array(keycodes.GetField("SHIFT")))
 robot1.RunMethod("keyPress", Array(keycodes.GetField("BACK_QUOTE")))
 robot1.RunMethod("keyRelease", Array(keycodes.GetField("BACK_QUOTE")))
 robot1.RunMethod("keyRelease", Array(keycodes.GetField("SHIFT")))
 robot1.RunMethod("keyPress", Array(keycodes.GetField("A")))
 robot1.RunMethod("keyRelease", Array(keycodes.GetField("A")))
End Sub
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Very nice :) Apple key is COMMAND and CTRL is CONTROL, however when I tried this it didn't work, can you confirm it plz

B4X:
Dim robot1 As JavaObject
    Dim jo1 As JavaObject
    Dim keycodes As JavaObject
    keycodes.InitializeStatic("javafx.scene.input.KeyCode")
    jo1.InitializeStatic("com.sun.glass.ui.Application")
    robot1 = jo1.RunMethodJO("GetApplication",Null).RunMethodJO("createRobot",Null)
    TextArea1.RequestFocus

    ' you have to think how you would type the characters
    robot1.RunMethod("keyPress", Array(keycodes.GetField("CONTROL")))
    robot1.RunMethod("keyPress", Array(keycodes.GetField("V")))
    robot1.RunMethod("keyRelease", Array(keycodes.GetField("CONTROL")))
    robot1.RunMethod("keyRelease", Array(keycodes.GetField("V")))
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
try reversing the last 2 lines
I think you need to ensure you release keys in the opposite order they are pressed
eg,
CONTROL > V > release V > release CONTROL
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
I wrote a little test application that you can try. You can make neccessary adjustments and post an updated version.

Here it is :) Should be compatible with Java 11. It did not work with SDK 9.0.4.
 

Attachments

  • AutoKeyPress v1.1.zip
    354.3 KB · Views: 277
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
works for me only had to change lowercase v to uppercase v

Sounds stupid but to get lowercase v you need shift > V
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
oh I see. Do you know how to make the code compatible with SDK v9.0.4? I have updated the test application with uppercase V so that should work with Java 11 :)
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
oh I need to implement SHIFT as well to make it work? I taught CTRL+v was the way it was supposed to work.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Just trying it on java 8 and getting same error
 
Upvote 0
Top