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?