Results 1 to 6 of 6
Thread: KeyListener Issues
- 02-18-2011, 10:33 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 31
- Rep Power
- 0
KeyListener Issues
hello, I have a class for a 3D camera that I would like to be able to move and rotate in 3D by use of keyboard input. I've implemented KeyListener, added such a listener in my constructor, but no matter what I put in the KeyPressed() KeyReleased() or KeyTyped() methods, nothing changes... can't even get a line of output.
Here's the relevant part of my class:
Any ideas on what I can do to simply call the test() method? If I can do that then I can do the rest. Any thoughts appreciatdJava Code:import java.awt.*; import javax.swing.*; import java.util.*; import java.awt.event.*; import java.awt.Event.*; public class camera extends JPanel implements KeyListener{ public camera() {this.addKeyListener(this);} // most of class omitted as irrelevant public void test() {System.out.println("you got key input");} public void keyPressed(KeyEvent e) { test(); } public void keyReleased(KeyEvent e) { test(); } public void keyTyped(KeyEvent e) { test(); } }
- 02-18-2011, 11:26 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
The Camera (better name) component has to be focussable and have focus in order to recieve keyboard input.
The Component API docs include a link to the How to Use the Focus Subsystem in Oracle's Tutorial which includes a paragraph near the start on how to ensure that a particular component gains the focus the first time a window is activated.
If you get stuck post a runnable example of the problem.
- 02-18-2011, 11:33 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 31
- Rep Power
- 0
Ok, this is a camera class I developed myself, it is not related to any api... but that doesn't matter any ways; I was asking if there is anything wrong in the above code that prevents even a basic output from being called when a key is pressed.
- 02-18-2011, 11:41 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
I was asking if there is anything wrong in the above code that prevents even a basic output from being called when a key is pressed
My mistake then. I had assumed you were asking for "ideas on what I can do to simply call the test() method" in response to key strokes.
You can rest assured that there is nothing in the code you posted that prevents output (basic or complex) from being performed in response to a key press.
- 02-19-2011, 03:34 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 31
- Rep Power
- 0
OK... got it so it keyboard input working so long as it extends JFrame
-
Similar Threads
-
Help with keylistener?
By Kaizo in forum New To JavaReplies: 4Last Post: 12-11-2010, 12:55 AM -
keyListener not doing anything
By imorio in forum AWT / SwingReplies: 10Last Post: 08-17-2010, 10:46 PM -
KeyListener - Is this what I need?
By dbashby in forum New To JavaReplies: 26Last Post: 04-18-2009, 04:14 PM -
KeyListener Example
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:46 PM -
how to add a KeyListener
By leonard in forum New To JavaReplies: 1Last Post: 08-06-2007, 04:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks