Mouse press and release outside of window - java

I have two booleans: leftPressed and rightPressed. I need them to be set true when their respective mouse buttons are pressed and false when they are not. The location of the mouse and whether or not the program has focus should not matter. This program will not have a GUI. Is this even possible?

Definitely possible, very time consuming though. Use JNA to create a global mousehook. You would have to provide implementations for each platform you planned for it to run on, etc.. For example, here's someone who did it with windows.
I was looking for something similar at one point but found a better way to go about it in my code. However, I did run into this library. It may work out for you.

Related

Simulating a Mouse Event on A Minimized Window Java

So I want to be able to simulate mouse clicks on my window/frame, but in the background. This means I cant use the Robot class because it takes control of the OSes mouse which is not what I want (please guys, I know of and use the Robot class, it's not what I need).
I want the program to think I clicked on part of it (x,y on the frame) even when minimized (doesnt have to minimized but at least out of focus). I dont want it to take over my computer since it should ideally be a background task.
I did a little bit of research before posting and I read that I could make an Applet and then use the "reflection class"(?) and mouselisteners to invoke mouse events on the frame itself rather than through the OS. Not sure if itll work cause the explanation was pretty meh and the guy said they could communicate individually if he had problems :/.
Kind of wondering if it's possible at this point. If it cant be done in java, I know a little python, so if there's a solution for it in python, that could work to.
TLDR: Need to simulate mouseclicks on my frame, but it cant use the os mouse and should ideally work when the application is out of focus/in the background.
Thanks in advance :D

lwjgl double-click mouse events

I'm currently teaching myself LWJGL's mouse class, but there is something I still don't know how to do. I want to be able to handle double-click mouse events, but I don't know how.
It is not (AFAIK) natively supported. You have to do it yourself.
One way to do that : memorize the event date (System.curentTimeMS() or equivalent).
If the previous clic event was recent (~200 ms), it's a double-clic. else, simple clic.
Hope it helps...

Disabling keyboard/mouse in Java

I am working on a program, which allows the user to lock the computer, so no one else can use it. Is there anyway, I can disable the mouse, and specific keys on the keyboard? Thanks.
No but you can create have your program locking the screen and then using a MouseMovementListener so each time the mouse moves, you return it programatically to some point in the screen ( it would look like it doesn't move anymore )
over Linux you can use xinput enable id
you can get the id throw xinput without parameters.
Process p;
p = Runtime.getRuntime().exec("xinput disable 12");
I don't know of a portable way and I'm pretty sure Java does not supply anything like that in fact. However on Windows this can be done with BlockInput. But if you were going to code JNI/JNA you might as well use LockWorkStation.
I think you could do it if you are implementing the MouseListener interface. In the MouseClicked method you could check a boolean before actually performing any action. When you want to disable or enable the action change the state of that boolean.
If you want to lock the screen then you can use java Robot class. And if you want to block the keyboard and mouse events when Windows is locked then there is no need. Since locking the screen results in disabling all the inputs.

Play beep sound or any customized sound on all mouse click event in java

I have an application which i am going to install on Linux touch system. The touch system is not giving me the touch sound so i decided to have that feature on my application. What is the best way to do it ? I dont want to go through each and every buttons and other components and write the codes there. Is there any global way to handle that so that the sound works throughout the application when ever the screen is touched or mouse is clicked ??
Try this
Toolkit.getDefaultToolkit().beep();
To play it in all MouseEvent make your MouseEvent listener single and everytime when you need use that listener and write above code in that mouse listener.
Take this answer:
How can I play sound in Java?
... and trigger the playback in the mouse click event.
Assuming that you are using the JFC/Swing toolkit, you might want to read a bit about using the Multiplexing Look & Feel. Even though I don't know of any ready-to-use auxiliary look and feel(s) that might fit your needs, it should be possible to write your own auxiliary look and feel which behaves as you describe above ...
... after you have created your own auxiliary look and feel make sure to start your java interpreter with the "-Dswing.auxiliarylaf=your.auxiliary.look.and.feel.Classname" parameter.

Drag and drop with an image

I need to create a drag and drop system in swing where an image of the thing being dragged is attached to the cursor during the drag. In theory this is achieveable with
public Icon TransferHandler.getVisualRepresentation(Transferable t)
but there appears to be a long standing bug (here) that means this method is never called. I know I can do it by implementing my own DnD system with DragSource etc., but does anyone know of an easier workround that will get me what I need?
The method TransferHandler.getVisualRepresentation wasn't supported in java 1.4, I'm not sure if or when it was fixed. To test whether it works in a current version you could adapt this example
In the end I used the old style drag-and-drop to implement what I wanted. However I have no reason to think abrightwell's solution wouldn't work just as well - this was just the best way at the time.
You could Try putting the image on a Jlabel (in the draggesture recognizer)and set its bounds, in the droptargetListener dragover method. Alternately, hows about implementing a Mouse listener (I've not tested this latter method).
I have used the "work around" suggested towards the bottom of the bug report you have listed. It worked well enough for me. Granted I was using this with Mac OS X so I have no idea whether Winderz will support it. It would be nice if they would at least fix it to work like they intended and simply document where it will and won't work... oh well. Good Luck.

Categories