B4J Library jAWTRobot - invoke keyboard and mouse events, etc...

hibrid0

Active Member
Licensed User
Longtime User

Hi thanks for the fix, I update and the RobotPaster2 Work fine on MAC and the program crash in Windows if I use RobotPaste2, no problem for that.
I'm testing and maybe on this version not work RobotSpecialKeyPress("enter"), I use it after RobotPaste. And this not work in windows or MAC, on the old version Work fine "enter" on Mac or Windows.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
It works for me on Windows. Are you making sure that you call RobotSpecialKeyRelease("enter") right afterwards? You should probably also call RobotDelay(10) in between KeyPress and KeyRelease. Some operating systems might not register the key stroke if the key wasn't held down for a certain number of milliseconds (I go with 10 to be safe).
 

rboeck

Well-Known Member
Licensed User
Longtime User
Hi Roycefer,
first thanks for your work! I tried many hours in the past to solve the clipboard paste problem for mac - now its working!
In detail robotPaste2 works for me on mac (10.7.5), robotpaste3 doesnt work on any system, for pc i can only use robotpaste, when i use robotpaste2 or robotpaste3 i get this RuntimeException:

java.lang.RuntimeException: java.lang.RuntimeException: java.lang.IllegalArgumentException: Invalid key code
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:471)
at anywheresoftware.b4a.keywords.Common.CallSubNew3(Common.java:425)
at b4j.example.asyncstreamsobject._astream_newstream(asyncstreamsobject.java:136)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.BA$3.run(BA.java:178)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/367522714.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1685538367.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2058534881.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: Invalid key code
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:114)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:462)
... 18 more
Caused by: java.lang.IllegalArgumentException: Invalid key code
at sun.awt.windows.WRobotPeer.keyPress(Native Method)
at java.awt.Robot.keyPress(Robot.java:354)
at butt.droid.awtRobot.AWTRobot.RobotPaste3(AWTRobot.java:565)
at b4j.example.main._astreamo_newobject(main.java:213)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
... 19 more​
 

hibrid0

Active Member
Licensed User
Longtime User

Hi I see the same problem, I solved with this SystemOperatingSystemEtc from AWTrobot, this return the operating system and I select if is Windows Use robotpaste or if is Mac use robotpaste2.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Have either of you tried this library on Linux? I think RobotPaste should work on Linux but I'd like to be sure.

Based on your feedback, I will probably combine RobotPaste and RobotPaste2 into one function (called RobotPaste) that will try-catch one method and if it doesn't work, use the other method. This will enable you to use RobotPaste on all operating systems without having to check what operating system is hosting the JVM first.

I can see that I should also add support for the meta key (this maps to the command key on Macs) in the special key combos and such.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
The library is updated to version 1.35. All the RobotPasteX methods are combined into one RobotPaste method that should work across all supported platforms (no need to check the OS before). Some meta key (the command key on Macs) support was added as well as a method called RobotSupportsMetaKey that will tell you if the operating system supports the meta key. Make sure you check that the system supports the meta key before you call RobotSpecialKeyPress("meta").

As usual, be sure to read a method's comments.
 

rboeck

Well-Known Member
Licensed User
Longtime User
Hi, i tried it now and removed the os checking code and now it's working for mac and windows with the same code. In the past i had my one sendkeys routine and there was the same code for linux and windows, only the mac didnt work. Currently i have no working linux with java to try it...
Thanks!
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Thanks for the feedback. Be sure to post to this thread any other bugs you find.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Version 1.37 sees a minor addition of the method relaunchSelfBatchFile that pretty much does what it says. It behaves like relaunchSelf except instead of launching its own jar file, it launches a batch file. This is obviously Windows-only (though I'll be adding Linux-friendly versions of both soon enough). This is useful for programs that are better launched using a batch file or for which you want a persistent cmd.exe window visible.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Version 1.4 sees the addition of RobotSpecialKeyPress/Release support for the home, end, page up and page down keys. Also, the addition of the RobotFunctionKeyPress/Release methods for activating the function keys (F1-F12).
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Yes, I saw that. Very neat. That's why I originally wrote this library. I've used it in a bunch of remote control type applications. There are also some interesting automation applications to consider. I'm currently using a JVM KVM (thanks, I'll be here all week. Be sure to tip your waitresses.) for controlling a RasPi2 that makes use of this library and a few of my other libraries. I'm just polishing it up a little and then I'll release it as an open-source project for the B4X community.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Version 1.42 sees the addition of two new methods. Robot.JVMCallingThreadName() does what it says. This is pretty useful for hunting down threading-related bugs. There's also a new Robot.JVMThrowException() method that allows you to throw custom Exceptions:
B4X:
Sub Divide(n As Double, d As Double) As Double
   If d==0 Then Robot.JVMThrowException("You blew up the universe.")
   Return n/d
End Sub
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Version 1.45 changes the Robot.JVMCallingThreadName() method to Robot.JVMCallingThreadNameGet(). It also adds the Robot.JVMCallingThreadNameSet() method which allows you to rename the calling Thread. I find this much more useful for troubleshooting threading related stuff. For example, multi-threaded Handlers and WebSocketHandlers all run in their own Threads in B4J servers. In their Initialize() methods, you can rename those Threads to something more descriptive so you can track their actions across various modules more easily. The Threading library can get and set the names of Threads it creates, but these new methods allow you to do the same for any Threads in the application.

Erel,
Do you have any plans to implement a developer-accessible UncaughtExceptionHandler for B4J like you've done for B4A? I've been playing with some solutions that I might incorporate into this library but if you're coming out with your own solution, I'd rather not waste the effort.
 

Cableguy

Expert
Licensed User
Longtime User
Hi Guys

I'm having a hard time getting the relaunchtself method to work... I used:

B4X:
Robot.relaunchSelf(File.DirApp & "\MyApp.jar","")

it starts the batch and when it should re launch the file, it return an error stating it could not access the jar file.
The message is cut off so I cannot see the full path its trying to access...
 

Cableguy

Expert
Licensed User
Longtime User
yes, win10...
Eventually I will opt out for a custom batch file, if I can figure out how to do it, in order to have a "silent" batch file, with no print out to the screen...
For now I'm just trying to get it to restart my app...
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
I can not help more because I just tested it with the same argument you do use and it was working as espected. The way you create the path is the good one.
Here is the content of the "relaunch.bat" batch file created by the Robot
B4X:
color 0a
timeout 5
start javaw -jar C:\Users\ann\b4j\Travaux\CloudKVS\CloudKVS_Client\Objects\CloudKVS_Client.jar
del "relaunch.bat"

If I remember correctly, adding an ECHO OFF should stop printing the lines (but I will have to try it to be sure)


EDIT: it is something like this
B4X:
@ECHO OFF
timeout 1 > null
start javaw -jar C:\Users\ann\b4j\Travaux\CloudKVS\CloudKVS_Client\Objects\CloudKVS_Client.jar
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
where can I find the temp batch file? If only I could check the path it tries to access...
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…