Is KeyListenening wothout focus possible?

Note: Ive earlier been critizised for not posting in the right forum, but I really have no clue where else to post this one:

Question: I would like to hear if it is possible to get notified when a key is pressed even though my program doesnt have focus?

Reason for posting: Im working on a robot that hopefully one day will be able to play poker on my command when I scream into my microphone lying on my bed like a lazy donkey :slight_smile:

I believe you can add an AWTEventListener directly to the awt event queue by using.

java.awt.Toolkit - addAWTEventListener.

You’ll have to convert the events back into KeyEvents (just a cast on the right event code).

Kev

Hmmm I have got this far as to create the AWTEventListener:


AWTEventListener l = new AWTEventListener() {
            public void eventDispatched(AWTEvent arg0) {
                  //Here I will place my actions
            }
      };

But where do you mean I should add it?


AWTEventListener listener = 
          new AWTEventListener() {
            public void eventDispatched(AWTEvent event) {
              KeyEvent ke = (KeyEvent)event;
              if (ke.getID() == KeyEvent.KEY_TYPED) {
                buffer.append(ke.getKeyChar());
            }
          }
        };
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        toolkit.addAWTEventListener(
          listener, AWTEvent.KEY_EVENT_MASK);

http://java.sun.com/developer/JDCTechTips/2003/tt0624.html

Kev

public class A {
      public static void main(String[] args) {
            AWTEventListener l = new AWTEventListener() {
                  public void eventDispatched(AWTEvent arg0) {
                        System.out.println(System.out.println());
                  }
            };
            Toolkit toolkit = Toolkit.getDefaultToolkit(); 
            toolkit.addAWTEventListener(l, AWTEvent.KEY_EVENT_MASK); 
      }

}

This code gives me this error message:

[quote]Exception in thread “main” java.lang.VerifyError: (class: main/A$1, method: eventDispatched signature: (Ljava/awt/AWTEvent;)V) Unable to pop operand off an empty stack
at main.A.main(A.java:53)
[/quote]
Do you have any clue whats wrong?

Did you compile with one version of java then run with another? Normally where VerifyErrors turn up.

Kev

I made some copy pasting from the url you sent me. Now I get no error message but I dont see anything happen when typing a key:

public class A {
    public static void main(String args[]) {
        AWTEventListener listener = 
          new AWTEventListener() {
            public void eventDispatched(AWTEvent event) {
              KeyEvent ke = (KeyEvent)event;
              if (ke.getID() == KeyEvent.KEY_TYPED) {
                System.out.println("Key typed");
            }
          }
        };
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        toolkit.addAWTEventListener(
          listener, AWTEvent.KEY_EVENT_MASK);
      }

}

Ah ha, I think you actually do need to create a component… maybe not show it… but AWT needs to get initiallised to start the event queueing.

At least I think so,

Kev

that means if I add the Listener inside a class extending a Frame is should work?

I’d think so, I take it, it doesn’t?

Kev

Well it works while the Frame is visible but it I dont use setVisible(true) og if I use hide(); after setting visibility to true then it doesnt react on anymore.

It seems that the solution only works when the Component they are attached to is in focus.

I was searching for some kind of listener that would be able to react on key pressings even though the program is in the background. Do you have any other suggestions?

JInput might solve what you want? Though I think that may have a similar restriction.

If the application is in the background (i.e. not even the focus from a dos/shell window) then any event driven system will fail. You might be getting to the point where you’ll need to be making O/S specific calls to monitor key input.

Kev

I will go read about JInput and see what that could do. Thanks again. Especially for the fast replies :slight_smile:

Do you know if it would be possible to use that awtlistener with mouseMove detection too? in that case, how?

Just need to modify the mask:



   toolkit.addAWTEventListener( 
     listener, AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK); 

or summink…

Kev

[quote]Note: Ive earlier been critizised for not posting in the right forum, but I really have no clue where else to post this one:
[/quote]
If you’re really not sure, and it’s a question about java core libraries, Clueless Newbies is a good bet. Especially since a lot of people read that with the intent of answering questions ;D

There is a new class added in 1.5 called java.awt.MouseInfo that I have been playing with. It allows you to get the system wide mouse coordinates without opening a window or anything. Unfortunately it is awkward to use and there is no way to poll the mouse buttons, but it is nice to have.

I think polling the keyboard outside of a focused window is still something you cannot do without an external library.

I assume you know about the java.awt.Robot class that you can use to generate keypresses and move the mouse with since you mentioned “Robot” above.