KeyEvents

  • Thread starter Francois Lionet
  • Start date
F

Francois Lionet

Hello all,

In my game, I have an JTextField flying above the playfield.
When the user has selected the textfield and types some text in it, the keys
are no longuer transmitted to the main application. But I need them!
I have tried to addKeyListener(myApplication), but if I do this, the key
events get stuck in the application, and are not anymore transmitted to the
text field.
In the doc, I see a "consume" field that indicates if the event should carry
on the list of listener. As it is protected I do not know what to do. I
wanted to make a subclass of keyevent and access the consumer like this, but
the problem is that many of the fields wanted by the constructor of keyevent
are unaccessible.
Can you help me and give me an idea on how to transmit the keys to the
application even when the TextField has the focus?

Thanks, Francois
 
J

John B. Matthews

"Francois Lionet said:
In my game, I have an JTextField flying above the playfield.

I don't understand "flying" in this context. Perhaps you can supply a
Short, Self Contained, Complete Example (SSCCE) that suggests what
you're trying to accomplish:

When the user has selected the textfield and types some text in it,
the keys are no longuer transmitted to the main application.

Yes, the JTextField has the keyboard focus:

But I need them! I have tried to addKeyListener(myApplication), but
if I do this, the key events get stuck in the application, and are
not anymore transmitted to the text field.

Yes, only one component can have focus at a time.
In the doc, I see a "consume" field that indicates if the event
should carry on the list of listener. As it is protected I do not
know what to do. I wanted to make a subclass of keyevent and access
the consumer like this, but the problem is that many of the fields
wanted by the constructor of keyevent are unaccessible.

I do not see a field named "consume" in JTextField:

Can you help me and give me an idea on how to transmit the keys to
the application even when the TextField has the focus?

You might look at adding a KeyListener to the JTextField:

<http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html>

as shown in this SSCCE from Sun's tutorial:

<http://java.sun.com/docs/books/tutorial/uiswing/examples/events/KeyEvent
DemoProject/src/events/KeyEventDemo.java>

Your handler can respond accordingly. For example, you might treat
VK_ENTER as a signal to return focus to the game itself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top