Results 1 to 4 of 4
Like Tree2Likes
  • 2 Post By doWhile

Thread: Adding a key listener problem

  1. #1
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Red face Adding a key listener problem

    Hey, im struggling trying to figure out how to get it to work. I have followed a number of examples online and added the as far as i can figure out all that i need to. But when i press a key nothing happens. Any advice would be helpful. PS. Im using eclipse which is why the stubs are kicking around....

    Java Code:
    public class testCanvas extends JPanel implements ,KeyListener
    {
            testCanvas()
    	{
    	
    	this.addKeyListener(listener);
    	}
    
            KeyListener listener = new KeyListener()
    	{
    	@Override
    	public void keyPressed(KeyEvent e) {
    		// TODO Auto-generated method stub
    		  dumpInfo("Pressed", e);
    		 }
    	
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
    		 dumpInfo("Pressed", e);
    		
    	}
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    		 dumpInfo("Pressed", e);
    		
    	}
    
           private void dumpInfo(String s, KeyEvent e) 
    	{
            System.out.println(s);
            int code = e.getKeyCode();
            System.out.println("\tCode: " + KeyEvent.getKeyText(code));
            System.out.println("\tChar: " + e.getKeyChar());
            int mods = e.getModifiersEx();
            System.out.println("\tMods: "
                + KeyEvent.getModifiersExText(mods));
            System.out.println("\tLocation: "
                + location(e.getKeyLocation()));
            System.out.println("\tAction? " + e.isActionKey());
          }
    	
    	private String location(int location)
            {
            switch (location) {
            case KeyEvent.KEY_LOCATION_LEFT:
              return "Left";
            case KeyEvent.KEY_LOCATION_RIGHT:
              return "Right";
            case KeyEvent.KEY_LOCATION_NUMPAD:
              return "NumPad";
            case KeyEvent.KEY_LOCATION_STANDARD:
              return "Standard";
            case KeyEvent.KEY_LOCATION_UNKNOWN:
            default:
              return "Unknown";
            }
            
            }
    	};
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Does the component with the listeners have the focus? And is it enabled to receive focus?
    I forget the method calls to make. Search on the forum for code examples.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

  4. #4
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Default

    Thanks, the key binding was far easier and i have it working now. Thanks everyone :)

Similar Threads

  1. Problem with key listener
    By Mikelmao in forum New To Java
    Replies: 1
    Last Post: 03-14-2011, 02:02 PM
  2. Adding a listener
    By IWI in forum Advanced Java
    Replies: 0
    Last Post: 12-31-2010, 03:05 PM
  3. CheckedTreeDialog adding Handler/Listener
    By negroscuro in forum SWT / JFace
    Replies: 5
    Last Post: 03-30-2009, 06:30 AM
  4. #key listener problem
    By mij1_7 in forum New To Java
    Replies: 2
    Last Post: 02-14-2009, 09:02 PM
  5. Adding listener to non-Java object?
    By cruxblack in forum Advanced Java
    Replies: 5
    Last Post: 07-30-2007, 02:19 AM

Posting Permissions

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