Pillow123
Pillow123

Reputation: 11

Problems with understanding KeyEvent in Java

I am currently trying to make a program where three buttons are entered, and each time something different happens for them.

I know that I have to use keyPressed, but it's really confusing me because when I run my program, it doesn't wait for me to enter anything.

I've just been following online guides since I'm pretty new to programming in general, so if you have a better way of doing it all together then please do say so.

import java.awt.event.KeyEvent;

public class Trial {

  public static void main(String[] args) {
    System.out.println("Welcome to the Medical Registration Form program.");
    System.out.println("To enter a new patient's details, press 'N'");
    System.out.println("To access an existing pateient's details, press 'S'");
    System.out.println("To see all patient deatils currently saved, press 'P'");
  }

  public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();
    if (keyCode == KeyEvent.VK_S) {
      System.out.println("You pressed a valid button");  
    } else {
      System.out.println("You pressed a bad button!");
      e.consume();
    }
  }
}

Upvotes: 1

Views: 84

Answers (1)

Nasser Tahani
Nasser Tahani

Reputation: 735

If you want to use the console, follow this snippet code:

public class Demo{

public static void main(String[] args) {
    // TODO code application logic here

    System.out.println("Welcome to the Medical Registration Form program.");
    System.out.println("To enter a new patient's details, press 'N'");
    System.out.println("To access an existing pateient's details, press 'S'");
    System.out.println("To see all patient deatils currently saved, press 'P'");
    Scanner scan = new Scanner(System.in);
    String input = scan.next();

    if(input.matches("S")){
        System.out.println("You pressed a valid button");  
    } else {
        System.out.println("You pressed a bad button!");

    }
}

Otherwise, extend from Jframe and implements KeyListener as follows:

public class Demo extends JFrame implements KeyListener{

public Demo(){

    this.addKeyListener(this);
}

public static void main(String[] args) {
    // TODO code application logic here

    System.out.println("Welcome to the Medical Registration Form program.");
    System.out.println("To enter a new patient's details, press 'N'");
    System.out.println("To access an existing pateient's details, press 'S'");
    System.out.println("To see all patient deatils currently saved, press 'P'");
    Demo demo = new Demo();
}   

@Override
public void keyPressed(KeyEvent e) {

    int keyCode = e.getKeyCode();
    if(keyCode == KeyEvent.VK_S){
        System.out.println("You pressed a valid button");  
    } else {
        System.out.println("You pressed a bad button!");
        e.consume();
    }
}

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

}

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

}

Upvotes: 1

Related Questions