6

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.

Community
  • 1
  • 1
CosmicGiant
  • 6,275
  • 5
  • 43
  • 58
  • 1
    see also: http://stackoverflow.com/questions/9545388/how-can-i-detect-arrow-keys-in-java-console-not-in-gui – pb2q Jul 26 '12 at 16:30
  • I think it's possible to run an [`EventQueue`](http://docs.oracle.com/javase/6/docs/api/java/awt/EventQueue.html) manually. Look into the JDK sources for how `JOptionPane` is implemented, I seem to remember the way their modal dialogs that block until you dismiss them then return a result are implemented are by creating a new `EventQueue` that ignores events not aimed at the dialog. – millimoose Jul 26 '12 at 16:56
  • (That said, I've no idea if that will automagically hook into the native windowing system and start grabbing events.) – millimoose Jul 26 '12 at 16:57
  • @pb2q Nice, that link is providing a lot of insight. Unfortunetly, it seems that the link to the discussion thread is dead. I wanted to take a look at that. I didn't have time to read it all yet, but it has already provided a lot of new knowledge. Thank you very much. +1 – CosmicGiant Jul 26 '12 at 17:00

2 Answers2

5

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.

AlexR
  • 114,158
  • 16
  • 130
  • 208
-3

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.