Where to add keylistener with FullScreen?

Hello all, i went through the wiki for some help on fullscreen and keyboard input. I looked at the code and tested it out.
Now that i’ve somewhat understood how fullscreen works, thanks to the wonderful and clear comments made, i began tinkering around the code. (IE Displaying random objects, pictures, etc…) But the thing that I can’t seem to get right is the keylistener.

I’m also using my Deitel java book for reference, but everything they do requires swing and they added “addKeyListener(this)” within the constructor of the class.
I looked through the fullscreen code and saw that well although some of it is similar to swing, i don’t think it really uses it. (I’m a newbie to java ). I was wondering where i Should implement “addKeyListener(this)” because so far i can’t get keyboard input to work and its frustrating me !!

Thanks!

You can probably add it to the frame you used to create the fullscreen exclusive mode?

Kev

do you mean replace ‘this’ with mainFrame? or use “addKeyListener(this)” when i construct a fullscreen window?

Yep, it might work :slight_smile:

Kev

cough…which one please…? Am still stuck

Damn, I’m sorry… didn’t make that too clear,

You want to add the KeyListener to the Frame. So if your frames is called “mainFrame” then at some point you’ve got to do mainFrame.addKeyListener(myListener).

Something like (excuse pseduo code, I’m not a decent pooter right now)



public class MyKeyListener implements KeyListener {
    public void keyPressed(KeyEvent e) {
       // do what you want here
    }
 
    public void keyReleased(KeyEvent e) {}

    public void keyTyped(KeyEvent e) {}
}

public class MyFullScreenFrame extends Frame { // or extends JFrame
    public MyFullScreenFrame() {
         super("Demo 1");

         // make this window fullscreen

         addKeyListener(new MyKeyListener());
    }
}


Sorry if its not dead on… shout if you need more help…

Kev

Hum…wow…
all i had to do was type “mainFrame.addKeyListener(this);”
but i dont’ understand why when i typed “addKeyListener(this);”, it did not accomplish the same thing? Perhaps the compiler doesn’t know what to add to exactly ?

Thanks very much kev.

There is a lot more to it than ‘typing’ …

Where did you put the code. Obviously not in a member function of mainFrame.

Well before I tried adding “addKeyListener(this)” to area where we create the frame. When i added “mainFrame.addKeyListener(this)” to the part I initialized other objects which is also after the creation of the mainFrame, my key listener started working.
Also i was wondering:
When i tried the keyboard input from the wiki, my vk_arrow keys do not work? Left, Right, Down, Up generate the same key instance and i’m not sure why. VK_ESCAPE works perfectly, but its these virtual arrow keys that make my program annoying. Sigh…

I’m not a big fan of Dietel; I hate their books. There are about two basic ways to handle events in swing; implementing the listener intreface or using the Component method enableEvents() + process_Event().

EventListeners in swing is a pretty big topic. It would be a really good idea to read over the swing tutorial in the java tute; particularly this part:

http://java.sun.com/docs/books/tutorial/uiswing/events/index.html

Lots of examples to look at and play with.