Skip to main content
gamedev.net
🔒 Locked

[java] Can't get key events

Started by Luctus Nov 18, 2004 at 9:26 PM 0 replies 950+ views
Original Post
Luctus
Luctus
I can't get any key events in the applet I'm writing, funny thing is that mouse events works perfectly. The code looks like:

public class Controller
    extends MouseAdapter
    implements KeyListener
{
    public void mousePressed( MouseEvent e ) 
    {
	System.out.println( "mouse down" );
    }
	    
    public void keyTyped( KeyEvent e )
    {
	System.out.println( "key typed" );
    }

    public void keyPressed( KeyEvent e )
    {
       System.out.println( "key down" );
    }

    public void keyReleased( KeyEvent e ) 
    {
       System.out.println( "key up" );
    }
}


Related code in applet class

// View extends JPanel
m_view = new View();

Controller c = new Controller();
m_view.addMouseListener( c );
m_view.addKeyListener( c );


Anyone can see anything wrong with this?
-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams
Luctus
Luctus
Ok, that's odd..

After setting the View as focusable with a subsequent call to requestFocusInWindow() (seems like swing component handles this different than awt components..) I get the key events..but requestFocusInWindow() always returns false, which according to the java api results in the component not getting focus. Still, it gets focus just fine..go figure
Quote:

from the java api specification
This method returns a boolean value. If false is returned, the request is guaranteed to fail.
-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.