2

My aim is to do something when I press an arrow key. But it's not recognising any input.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;


class keyb extends JFrame implements KeyListener{

    public keyb(){

        setFocusable(true);
        addKeyListener(this);

    }

    public static void main(String[] args){
        System.out.println("alsfhkasld");
        keyb ff = new keyb();

        System.out.println("end");
        System.out.println("ennnn");

    }


    public void keyPressed (KeyEvent e){
        System.out.println("anything....");
        if(e.getKeyCode()==KeyEvent.VK_UP){
            System.out.println("up! UP!");
        }else if(e.getKeyCode()==KeyEvent.VK_DOWN){
            //y+=50;
        }else if(e.getKeyCode()==KeyEvent.VK_RIGHT){            
            //x+=50;
        }else if(e.getKeyCode()==KeyEvent.VK_B){
            System.out.println("B come on!");
        }

    }
    public void keyReleased (KeyEvent e){}
    public void keyTyped (KeyEvent e){}

}

There are no errors when I compile. It just ignores any key I press. If this is something to do with JFrame, for example it only works for GUI, could someone please suggest another way to read the arrow keys as input?

Bell
  • 359
  • 4
  • 9

3 Answers3

2

KeyListener only works in a GUI. If you don't want to be like everybody else and create a GUI you will need to use a library that sets the terminal into non-canonical mode and provides some abstraction over the events. The exact details will depend on the library you end up picking, and probably your operating system too. See for example What's a good Java, curses-like, library for terminal applications?

Community
  • 1
  • 1
Joni
  • 108,737
  • 14
  • 143
  • 193
1

When you actually show the JFrame by setting setVisible(true), it will work

public keyb(){

    setFocusable(true);
    addKeyListener(this);

    setVisible(true);

}

(By the way, as per java naming conventions, classes start with an upper case)

beosign
  • 433
  • 5
  • 10
0

Your implementation is incomplete. you can follow below code to implement keylister.

import java.awt.; import java.awt.event.; import javax.swing.*;

public class KeyChecker extends JFrame { JLabel keyLabel = new JLabel("Hit any key");

public KeyChecker() {
    super("Hit a Key");
    setSize(300, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.CENTER));
    KeyMonitor monitor = new KeyMonitor(this);
    setFocusable(true);
    addKeyListener(monitor);
    add(keyLabel);
    setVisible(true);
}

public static void main(String[] arguments) {
    new KeyChecker();
} }

class KeyMonitor extends KeyAdapter { KeyChecker display;

KeyMonitor(KeyChecker display) {
    this.display = display;
}

public void keyPressed(KeyEvent event) {
    display.keyLabel.setText("" + event.getKeyChar());
    display.repaint();
} }
java_bee
  • 443
  • 4
  • 5