Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
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  
  Console KeyListener  (Read 3083 times)
0 Members and 1 Guest are viewing this topic.
Offline darkprophet

Senior Member




Go Go Gadget Arms


« Posted 2005-01-19 20:34:11 »

Hi all,
I was wondering if there is some Reader/Buffer combination that gives the same result as a KeyListener. Ie, when a key is being pressed.

Basically, i have this command line application that is waiting for the user to input a command ( a play button from a video recorder ), but a video recorder doesn't expect you to press enter afterwards Smiley

So say after the user press 'p', the reader would return..

DP

Friends don't let friends make MMORPGs.

Blog | Volatile-Engine
Offline kevglass

JGO Kernel


Medals: 159
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2005-01-20 05:17:55 »

I'm not sure if theres a buffer/reader combo but have you tried adding a KeyListener to the AWT event queue. It might still pick up events even without the window?

Kev

Offline darkprophet

Senior Member




Go Go Gadget Arms


« Reply #2 - Posted 2005-01-20 07:43:33 »

Sorry for being very naive here, but how would I accomplish this?

I know JFrames have a .setKeyListener(KeyListener) method. You said something about the AWT event queue, how would i be able to do that?

I googled for some stuff, but they all use JFrame/Canvas...etc

Thank you, DP

Friends don't let friends make MMORPGs.

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

JGO Kernel


Medals: 159
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #3 - Posted 2005-01-20 09:11:04 »

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Toolkit.html#addAWTEventListener(java.awt.event.AWTEventListener,%20long)

Should get you every event that comes through awt. Mask out for keyboard events. I'm not 100% sure whether you might have to initialise AWT (create a button or something) before trying to use it...

Let me know, I have some console level stuff coming up that I'd like to use it.. hmm.. console based 4k game! Wink

Kev

Offline darkprophet

Senior Member




Go Go Gadget Arms


« Reply #4 - Posted 2005-01-20 10:32:19 »

seems like this is impossible to do!  Cry

Have a look at this snippet:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public class TestConsoleKeyInputListener {
     
      public TestConsoleKeyInputListener() {
            Toolkit defaultTookKit = Toolkit.getDefaultToolkit();
            defaultTookKit.addAWTEventListener(new AWTEventListener() {
                  public void eventDispatched(AWTEvent event) {
                        System.out.println("HELLO");
                  }
            }, AWTEvent.KEY_EVENT_MASK);
           
            AWTEventListener[] listeners = defaultTookKit.getAWTEventListeners();
            System.out.println(listeners.length);
      }
     
      public static void main(String[] args) {
            new TestConsoleKeyInputListener();
      }
}


It returns 0! Meaning that the listener cannot be added. This is ofcourse with the "-Djava.awt.headless=true" flag on. Which is essentially what this application is, headless. But if i take that flag off, it returns 1. which is the expected value...

Even without the headless flag, it still doesn't register key strokes like it should.

However, doing this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public class TestConsoleKeyInputListener extends JFrame{
     
      public TestConsoleKeyInputListener() {
            Toolkit defaultTookKit = Toolkit.getDefaultToolkit();
            defaultTookKit.addAWTEventListener(new AWTEventListener() {
                  public void eventDispatched(AWTEvent event) {
                        System.out.println("HELLO");
                  }
            }, AWTEvent.KEY_EVENT_MASK);
           
            AWTEventListener[] listeners = defaultTookKit.getAWTEventListeners();
            System.out.println(listeners.length);
      }
     
      public static void main(String[] args) {
            JFrame frame = new TestConsoleKeyInputListener();
            frame.setSize(0, 0);
            frame.pack();
            frame.show();
      }
}


Works fine! So i guess its time to make a "HeadlessFrame"!

DP

Friends don't let friends make MMORPGs.

Blog | Volatile-Engine
Offline kevglass

JGO Kernel


Medals: 159
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #5 - Posted 2005-01-20 10:34:55 »

What happens if you don't show or pack the frame?

Kev

Offline darkprophet

Senior Member




Go Go Gadget Arms


« Reply #6 - Posted 2005-01-20 15:18:56 »

nope, it needs a Component for the AWT event queue to start up. And its just too much hassle. And since i am developing on a pure linux arch, im going to be using this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
FileInputReader reader = null;

try {
  reader = new FileInputReader("/dev/tty0");
}catch (FileNotFoundException fnfe) {
  // file not found
}

int nChar = 0;
try {
  nChar = reader.read();
}catch (IOException ioe) {
  // couldn't read
}


Thats the work around...

DP

Friends don't let friends make MMORPGs.

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

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (32 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (40 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!