Im making a program that has to call on a method when space is pressed,note it is the first time I'm doing a keylistener

To do this i did

public void keyPressed (KeyEvent e)
    {
        int key =e.getKeyCode ();
        int space = 32;

    if (key==space)
    {
    }
}

and I get this error

The abstract method "void keyTyped(java.awt.event.KeyEvent $1);", inherited from type "java.awt.event.KeyListener", is not implemented in the non-abstract class "Program".

What am I forgetting?


Btw, I'm using java 1.4.2, as my school currantly doesant want to update program on server

Recommended Answers

All 8 Replies

First of all ,use code tag whenever you put a code.

As for your solution you need to add an empty implementation of keyTyped() in your class since you have inherited java.awt.event.KeyListener ,you will need to implement all methods specified in it.

First of all ,use code tag whenever you put a code.

As for your solution you need to add an empty implementation of keyTyped() in your class since you have inherited java.awt.event.KeyListener ,you will need to implement all methods specified in it.

I'm really happy now becouse my code accually compiles, I did what you said so now I have this

public void keyPressed (KeyEvent c)
    {
        int key = c.getKeyCode ();
        System.out.println (key);
        int space = 32;
        if (key == space)
        {
            System.out.println ("Space was pressed");
        }
    }

    public void keyReleased (KeyEvent e)
    {

    }


    public void keyTyped (KeyEvent e)
    {

    }

But for some reason it wont react, I found out on google that space is apparently number 32 or somthing? Is this correct?

I'm really happy now becouse my code accually compiles, I did what you said so now I have this

public void keyPressed (KeyEvent c)
    {
        int key = c.getKeyCode ();
        System.out.println (key);
        int space = 32;
        if (key == space)
        {
            System.out.println ("Space was pressed");
        }
    }

    public void keyReleased (KeyEvent e)
    {

    }


    public void keyTyped (KeyEvent e)
    {

    }

But for some reason it wont react, I found out on google that space is apparently number 32 or somthing? Is this correct?

I also added a System.out.println to see the numbers of differant keys, but nothing prints out...

Space is decimal 32, yes, but far better to use a char literal to avoid any possible confusion or error (remember that char is a numeric type):

if (key == ' ') ...

Space is decimal 32, yes, but far better to use a char literal to avoid any possible confusion or error (remember that char is a numeric type):

if (key == ' ') ...

My program wont respond though, it compiles and everything, I even added a printout to see if it is even recognizing keystrokes, but its not even printing anything out

public void keyPressed (KeyEvent e)
    {
        int key = e.getKeyCode ();

        System.out.println ("key pressed" + key);//seeing if it accually gets any keyinput
      
        if (key == KeyEvent.VK_SPACE)
        {
            System.out.println ("Space was pressed");
        }
    }

Just noticed you are using keyPressed rather than keyTyped, so you have a keyCode not a keyChar. KeyCodes relate to the physical keyboard, rather than to actual characters, so, for example you get keyChar 'A' from keyCodes <shift key> followed by <a>. Because <space> is a physical key, the previous code will work OK, but it would fail for 'A'. The correct way to test for a keyCode being the space key is

if (keyCode == KeyEvent.VK_SPACE)

Your lack of keyboard events is probably a focus issue - your listener needs to be attached to the component that has keyboard focus. This is often a problem! There is a better way to handle keyboard input like this, it's called Keyboard Mappings, see
http://java.sun.com/products/jfc/tsc/special_report/kestrel/keybindings.html
... but it's quite a bit to adsorb at first, so you may prefer to sort out which component has the keyboard focus in your app.

Just noticed you are using keyPressed rather than keyTyped, so you have a keyCode not a keyChar. KeyCodes relate to the physical keyboard, rather than to actual characters, so, for example you get keyChar 'A' from keyCodes <shift key> followed by <a>. Because <space> is a physical key, the previous code will work OK, but it would fail for 'A'. The correct way to test for a keyCode being the space key is

if (keyCode == KeyEvent.VK_SPACE)

Your lack of keyboard events is probably a focus issue - your listener needs to be attached to the component that has keyboard focus. This is often a problem! There is a better way to handle keyboard input like this, it's called Keyboard Mappings, see
http://java.sun.com/products/jfc/tsc/special_report/kestrel/keybindings.html
... but it's quite a bit to adsorb at first, so you may prefer to sort out which component has the keyboard focus in your app.

Thanks alot, I did some google searches on the focusing issue
and i got results

I just added this to my program works perfect

this.setFocusable(true);   // Allow this panel to get focus.
this.addKeyListener(this); // listen to our own key events.

thanks alot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.