Working of Robot class - java

I know that Robot class in java is used to generateInputevents such as mouseMove,press and release and also keyevents ,i know only the functionality i.e if i call mouseMove(x,y) mousepointer will move to the (x,y) position.I don't know ,Actually what happens inside i.e what are the steps take by JVM to interact with os to move the mouse pointer and other events?

The nice thing about the Java library is that a lot of it is opensource. A quick glance through it shows that on Unix-ish platforms it creates a XToolkit and then there's a lot of interfacing with the X11 library to send keyboard and mouse events.
In particular, all of the events are eventually dispatched to sun.awt.X11.XRobotPeer, who then dispatches them to awt_Robot.c who then calls various X11 functions to produce particular events.

Related

Is there a way to block mouse and key press events to a certain program / window?

I have an application written in Java, but I am also using the JNI to execute C++ written by myself. I would now like to generate mouse and key press events programmatically. For reasons, events from the "real mouse" interfere with those that are generated. Is there a way to completely block any mouse and key events being sent to that application/window?
The program is running on Ubuntu 20 and hence it uses the X Window system.
Edit: A solution which intercepts mouse/keyboard events on a very low-level and simply ignores them is equally appreciated. I simply don't want them to reach the windows created from Java code.

Capture Keystrokes when program is not active

I code in java. I wrote a keylistener for the frame and it prints all the keystrokes when the frame is active, but when i minimize it or deactivate it, the program obviously stops and no keystrokes are printed. I wanted to make a small game where i enter a key and using the robot class, it presses another set of keys but this game is in flash. any idea as to how i would capture keystrokes when window is deactivated.
edit: I only code in java so is it possible using only java or at most combining it with native machine... i use windows
By its nature, Java is sandboxed by the JVM, so you will have to incorporate some kind of native methods. There already exists a very flexible and helpful library to accomplish this under open source, called JNativeHook. It's very easy to hook in, especially if you're already familiar with Swing event handlers. Same basic concept, except it leverages native code written in C. It supports all of the basic operating systems (Windows, Mac, *Nix).

java.awt.Robot: how to send mouse/keyboard events to a specific window? with cross-platform support?

So from this question In Java Swing how do you get a Win32 window handle (hwnd) reference to a window? it appears that I can get the window32 handle .
would it be possible for java.awt.Robot to send mouse/keyboard events to that window handle?
sometimes when I am sending keys via Robot, if the window gets minimized, it will start typing into other background irrelevant windows that are open. I want to prevent this by allowing Robot to send keys and mouse events to that specific window of interest.
Would it be possible to achieve the same deal in Mac and Linux as well? be able to send Robot events to those respective specific window handles?
This is a classic problem with Robot. As they have quoted in the other thread, its not possible with pure AWT/Swing. You have to get into sun's internal API or use native code. There is not getting around that problem.
It is exactly because of the problem that you have i.e. make it work across OS's is why Java has not exposed such a control.
It would be useful to know what you are using this for.

Mac OSX Java: Receive mouse events when not activated

I am writing a Java Swing application that needs to have a window receive mouse movement events when the application is not activated - think of it like a global always-on-top toolbar that animates when the mouse passes over it.
From my research so far, I have seen that the Mac Java JRE only passes events when the application has focus.
It does not appear to be a limitation of the OS, so I was hoping that there was a system property, an application package property or a system call that enabled non-activated event handling. Failing that, some method of globally capturing mouse movement events and passing them in to the Java application.
Thanks for any suggestions...
Edit: One further question: Once mouse move events have been captured, how do you feed them into Swing so that they are treated in the same was as native OS mouse events -- by finding the component under the mouse and sending a MouseEvent to it...
This isn't possible with pure Java.
You will need JNI and to write a global keylistener (or a keyboard hook) in C++ or another language.
Here are some topics about Global KeyListener:
Coderanch.
My own topic on Stack Overflow
Stack Overflow
Keyboard hooks in Mac OS X
On the last topic: this was a given solution for OSX.

Screen reading / mouse clicking under Linux / X Window System?

I'm learning about AI and (just for fun and practice, not profit or anything evil) I'd like to write a little bot to play a rinky-dink Flash game for me.
As a Microsoft hater, I'm embarrassed to say that I know and have used the required function calls some time ago under Windows, programming in Delphi. But these days I try to do everything under Linux and I don't seem to have any idea.
Requirements:
to grab part of the screen's contents in image or bitmap form, periodically or on programmatic request;
to simulate mouse pointer moves and left clicks under programmatic control;
keyboard grabbing might come in handy too.
Ideally, I'd like to do this from a Clojure application running in the JVM, so a Java compatible library would be the cat's meow. But failing that, I could write myself a C program to mediate between X Window and my app via local TCP/IP.
You can always rely upon the good old java.awt.Robot class, which, i think, has all the features you're asking :
moving mouse
capturing a Rectangle on screen
clicking mouse

Categories