Results 1 to 6 of 6
  1. #1
    xael is offline Member
    Join Date
    Sep 2010
    Posts
    31
    Rep Power
    0

    Default 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:

    Java 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();
    	}
    }
    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 appreciatd

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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.

  3. #3
    xael is offline Member
    Join Date
    Sep 2010
    Posts
    31
    Rep Power
    0

    Default

    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.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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.

  5. #5
    xael is offline Member
    Join Date
    Sep 2010
    Posts
    31
    Rep Power
    0

    Default

    OK... got it so it keyboard input working so long as it extends JFrame

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by xael View Post
    OK... got it so it keyboard input working so long as it extends JFrame
    Huh? I don't see anyone mentioning anything about JFrame in the replies above.

Similar Threads

  1. Help with keylistener?
    By Kaizo in forum New To Java
    Replies: 4
    Last Post: 12-11-2010, 01:55 AM
  2. keyListener not doing anything
    By imorio in forum AWT / Swing
    Replies: 10
    Last Post: 08-17-2010, 11:46 PM
  3. KeyListener - Is this what I need?
    By dbashby in forum New To Java
    Replies: 26
    Last Post: 04-18-2009, 05:14 PM
  4. KeyListener Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:46 PM
  5. how to add a KeyListener
    By leonard in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •