Disabling keyboard/mouse in Java - 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.

Related

how to set a specific position though command line?

String cmd = "start calc.exe";
Process process = Runtime.getRuntime().exec(codeString);
I can call calculator out, but I wish to specify a accurate position like (200,300).
how can I rewrite my cmd String?
I know that java.awt.window can set a window or frame to the specific position.
Is there any method I can use to fill frame or window with my process?
There is no clean pure java solution because JDK does not provide API that can control non-java windows. So, if you want to can use JNI/JNA.
But I can suggest you a patch that will typically work.
Windows OS allows moving windows using keyboard. Try the following manually:
Win+R
type calc and press enter
press alt+space
press M
press enter
now use arrows to move the window. Press ESC to exit this mode.
All these actions can be implemented using java.awt.Robot.
So, you can run calculator and then immediately move its window where you want.
Well, this is not clear solution, but very simple one.
Expected Problems:
Alt+space is mapped to other, custom application
Other window that started together with calc overlaps it.
User will see that window is created somewhere and then quickly moved.
So, everything depends on how important all this for you. This solution is good as an exercise or demo but bad for real commercial application.

Java Swing: How to distinguish events triggered by user?

I'd like to update GUI elements (JComboBox, JLabel, etc.) from code which shouldn't trigger change event. Is it possible to find out from java.awt.event.ActionEvent or java.awt.event.ItemEvent if the change was caused by an user or by running code like this?
combo.setSelectedItem("my item")
The answer is: no.
But in some cases you can try to analyze the current InputEvent. To get it, use EventQueue.getCurrentEvent(). For example if user has triggered the change on clicking of another component, you can compare the component of the input event and the component of the action event (OK I know: it's unsafe. But in some cases it can help to avoid incrementing of application complexity).
For a button you can get the event modifiers:
int buttonModifiers = evt.getModifiers();
If the button event was generated with a call to doClick() the modifier is 0, otherwise not.
By the way, you can find out such differences relatively easy by logging / printing using evt.toString()
.

Mouse press and release outside of window

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.

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.

Inject a keystroke in java

I'm looking for a way to inject a keystroke into the OS keyboard input buffer,
like when you click a button the program inserts one (or more) keyboard strokes. I wanted to do this in java because I want to run this in (win,linux and osx). I guess that I'll have to make use of the JNI, do anyone have some ideas?
Thanks all stackoverflowers ;)
My guess is that the java.awt.Robot class will do this for you:
new Robot().keyPress(...);
http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html#keyPress(int)
java.awt.Robot "is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed."
Check java Robot . I believe this is what you are looking for.
Also check this out. Example

Categories