Donald
Donald

Reputation: 11

Java KeyListener does not work

I'm trying to do a program whit a JFrame window that get the users input from the keyboard. So if the user types a word the statement class should see if the word is in there and then print out .

As it is now i can't get the statement class to work whit the keyboard class. I don' really wanna use this way to get keyboard input so i you people know any better way that would be grate.

Problem: Fix so the statement class get keyboard input.

This is the statement class:

public class Statements {

Keyboard input;

public Statements(Keyboard input){
    this.input = input; 
}

public void tick() {

    if(input.up){
        System.out.println("inventory");
    }
}

This is the keyboard class, it's a bad way of creating a key listener if you want to use all the characters on the keyboard. But it's the only way i know how.

public class Keyboard implements KeyListener {

private boolean[] keys = new boolean[200];
public boolean up, down, left, right, i;

public void update(){
    up = keys[KeyEvent.VK_UP] || keys[KeyEvent.VK_W];
    down = keys[KeyEvent.VK_DOWN] || keys[KeyEvent.VK_S];
    left = keys[KeyEvent.VK_LEFT] || keys[KeyEvent.VK_A];
    right = keys[KeyEvent.VK_RIGHT] || keys[KeyEvent.VK_D];
    //all the char on the keyboard
    ...
    ...
    i = keys[KeyEvent.VK_I];
}

public void keyPressed(KeyEvent e) {
    keys[e.getKeyCode()] = true;
}

public void keyReleased(KeyEvent e) {
    keys[e.getKeyCode()] = false;
}

public void keyTyped(KeyEvent e) {

}

}

Main class

private static final long serialVersionUID = 1L;

public static final int WIDTH = 100;
public static final int HEIGHT = WIDTH / 16 * 9;
public static final int SCALE = 3;

private boolean running = false;

private Thread thread;
private JFrame frame;
private Keyboard key;
private Statments stat;

public Main() {
    Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
    setPreferredSize(size);

    frame = new JFrame();
    key = new Keyboard();
    stat = new Statments(key);

    addKeyListener(key);


}

public synchronized void start() {
    running = true;
    thread = new Thread(this, "Display");
    thread.start();
}

public synchronized void stop() {
    running = false;
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public void run() {
    while (running) {
        frame.requestFocus();
        tick();
    }
    stop();
}

public void tick(){
    key.update();
    stat.tick();
}

public static void main(String args[]) {
    Main main = new Main();
    main.frame.setResizable(false);
    main.frame.setTitle("game");
    main.frame.add(main);
    main.frame.pack();
    main.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.frame.setLocationRelativeTo(null);
    main.frame.setVisible(true);

    main.start();
}

}

Upvotes: 0

Views: 162

Answers (1)

Maxim Shoustin
Maxim Shoustin

Reputation: 77930

You need to add KeyListener to every component that you used in JFrame to make it work and

setFocusable(true); as well.

However if you want to add KeyListener to JFrame, create custom KeyEventDispatcher and register it to KeyboardFocusManager, like:

KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
kfm.addKeyEventDispatcher(new MyKeyEventDispatcher());

(I would use JPanel and add all components to JPanel instead to JFrame)

Upvotes: 1

Related Questions