Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (736)
Games in Android Showcase (223)
games submitted by our members
Games in WIP (813)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Is KeyListenening wothout focus possible?  (Read 3686 times)
0 Members and 1 Guest are viewing this topic.
Offline Danskeren

Senior Devvie


Projects: 1


oi?


« Posted 2005-04-27 13:54:43 »

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 Smiley

Offline kevglass

« JGO Spiffy Duke »


Medals: 319
Projects: 25
Exp: 22 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2005-04-27 13:57:30 »

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

Offline Danskeren

Senior Devvie


Projects: 1


oi?


« Reply #2 - Posted 2005-04-27 14:15:27 »

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

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


But where do you mean I should add it?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kevglass

« JGO Spiffy Duke »


Medals: 319
Projects: 25
Exp: 22 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #3 - Posted 2005-04-27 14:19:23 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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

Offline Danskeren

Senior Devvie


Projects: 1


oi?


« Reply #4 - Posted 2005-04-27 14:25:59 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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)


Do you have any clue whats wrong?

Offline kevglass

« JGO Spiffy Duke »


Medals: 319
Projects: 25
Exp: 22 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #5 - Posted 2005-04-27 14:27:44 »

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

Kev

Offline Danskeren

Senior Devvie


Projects: 1


oi?


« Reply #6 - Posted 2005-04-27 14:28:32 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
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);
      }

}

Offline kevglass

« JGO Spiffy Duke »


Medals: 319
Projects: 25
Exp: 22 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #7 - Posted 2005-04-27 14:33:36 »

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

Offline Danskeren

Senior Devvie


Projects: 1


oi?


« Reply #8 - Posted 2005-04-27 14:37:47 »

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

Offline kevglass

« JGO Spiffy Duke »


Medals: 319
Projects: 25
Exp: 22 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #9 - Posted 2005-04-27 14:40:03 »

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

Kev

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danskeren

Senior Devvie


Projects: 1


oi?


« Reply #10 - Posted 2005-04-27 14:50:58 »

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?

Offline kevglass

« JGO Spiffy Duke »


Medals: 319
Projects: 25
Exp: 22 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #11 - Posted 2005-04-27 14:55:43 »

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

Offline Danskeren

Senior Devvie


Projects: 1


oi?


« Reply #12 - Posted 2005-04-27 15:03:39 »

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

Offline Danskeren

Senior Devvie


Projects: 1


oi?


« Reply #13 - Posted 2005-04-27 16:02:23 »

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

Offline kevglass

« JGO Spiffy Duke »


Medals: 319
Projects: 25
Exp: 22 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #14 - Posted 2005-04-27 16:08:36 »

Just need to modify the mask:

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


or summink..

Kev

Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #15 - Posted 2005-04-27 18:11:54 »

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:


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 Grin

malloc will be first against the wall when the revolution comes...
Offline ewg

Senior Devvie


Medals: 11


Java games rock!


« Reply #16 - Posted 2005-04-28 14:49:53 »

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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 
cybrmynd (120 views)
2017-08-02 12:28:51

cybrmynd (143 views)
2017-08-02 12:19:43

cybrmynd (138 views)
2017-08-02 12:18:09

Sralse (154 views)
2017-07-25 17:13:48

Archive (623 views)
2017-04-27 17:45:51

buddyBro (732 views)
2017-04-05 03:38:00

CopyableCougar4 (1258 views)
2017-03-24 15:39:42

theagentd (1238 views)
2017-03-24 15:32:08

Rule (1215 views)
2017-03-19 12:43:22

Rule (1268 views)
2017-03-19 12:42:17
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!