1

For now i only need a basic message to be printed as i'll program what i'll actually need later but my problem is:

@Override
    public void init(Canvas canvas) {
    canvas.addKeyListener(this);
}

@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
        getStateMachine().setState((byte) 1);
    } else if (e.getKeyCode() == KeyEvent.VK_H) {
        // Do some other stuff here...
    }
}

@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

    }

}

This is what code i have for buttons presses. Enter does a task, but H does not.

package instructionpackagev1;

import java.awt.event.KeyEvent;

public class Instructions {

public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_H) {
        System.out.println("For now, it's a test");
    }


     }

}   

So in other words the basis is, how do i get the letter 'H' to print a line when pressed?

Piotr Rogowski
  • 3,642
  • 19
  • 24
Kyle Mills
  • 45
  • 6
  • Possible duplicate of [How do I check if the user is pressing a key?](http://stackoverflow.com/questions/18037576/how-do-i-check-if-the-user-is-pressing-a-key) – Gewure Mar 10 '17 at 09:14
  • Possible duplicate of http://stackoverflow.com/questions/616924/how-to-check-if-the-key-pressed-was-an-arrow-key-in-java-keylistener – Spartan Mar 10 '17 at 09:27

2 Answers2

1

use this

if(e.getKeyChar() == 'H')

0

Have you tried public char getKeyChar()? If not, this may be a solution for you.

ForInfinity
  • 166
  • 1
  • 12