CosmicGiant
CosmicGiant

Reputation: 6439

Using Java, How to detect keypresses without using GUI components?

Using Java, is it possible to detect user actions, such as key-presses, mouse-button-presses, and/or mouse's screen location, without the use of GUI elements?

If it is, how could I implement it?

Otherwise, what are the alternatives?


The only StackOverflow source i found on this matter is this question, which is unanswered.

And the only sources I found outside StackOverflow on this matter point to an "Invisible GUI" solution, wish is something i really want to avoid.

Upvotes: 6

Views: 5253

Answers (2)

Alan0925
Alan0925

Reputation: 9

Use the java.awt.event.KeyListener class. You will have to write three methods in which you can write which key you want to be detected, and whatever you want to have happen when the key is pressed.

Upvotes: -3

AlexR
AlexR

Reputation: 115398

It can be implemented using JNI and/or JNA but this cannot be really called "java implementation" because you will have to write platform specific native code.

Alternative solution I tried is to use full screen transparent widow that is listening to all events of mouse and keyboard and "forwards" them to the real application using class Robot. I tried this approach. It works well with one limitation: there is a problem to support "mouse over" events of applications: the mouse is not moving over the real application. It is moving over the transparent java window.

Upvotes: 5

Related Questions