#Keyboard listener

11 messages · Page 1 of 1 (latest)

valid shoal
#

I am trying to get my program to listen for keyboard inputs, but I can't get it to work properly. My code is

import java.awt.event.KeyEvent;

public class test {
        
    public static void main(String[] args){

    KeyListener keyboard = new KeyListener() {
        
        @Override
        public void keyPressed(KeyEvent event) {
            System.out.println("yes");
        }
        @Override
        public void keyReleased(KeyEvent event) {
            }
        @Override
        public void keyTyped(KeyEvent event) {
            }    
    };
//this is so the program doesn't end
        for (int i = 1; i > 0; i++) {
          System.out.print("");
}
    }
}```

The program runs fine, but the listener doesn't work. Do you know why? 

Thanks!
lethal coralBOT
#

This post has been reserved for your question.

Hey @valid shoal! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

pastel venture
#

Listeners are part of the AWT api. AWT handles UI, the key listener only works when you make an UI Application

#

if you want user input, you can poll it using a Scanner and System.in from the console.

lethal coralBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

valid shoal
#

I do plan to implement this into a game, so I will need to take it while you are using the JFrame. My code, minus everything unneccessary:

import java.awt.event.KeyEvent;
import java.awt.*;
import javax.swing.*;
public class test{
    
    public static void main(String[] args) {
        JFrame GameBoard = new JFrame("Game");
        GameBoard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GameBoard.setSize(800,500);
        GameBoard.setLocationRelativeTo(null);
        GameBoard.setVisible(true);
        KeyListener keyboard = new KeyListener() {
        
        @Override
        public void keyPressed(KeyEvent event) {
            System.out.println(event);
        }
        @Override
        public void keyReleased(KeyEvent event) {
            }
        @Override
        public void keyTyped(KeyEvent event) {
            }    
    };
    while (1 == 1) {
        try {
        Thread.sleep(1000);
    } catch (Exception e) {
        System.out.println(e);
    }
    
    }
    }
}```
This still doesn't work and doesn't output anything to the terminal when I press keys
I don't understand what I did wrong, does anyone know?

Thanks!
river basalt
#

don't forget to add key listener to jframe

valid shoal
#

how do I do that?

river basalt
#

GameBoard.addKeyListener(keyboard);

valid shoal
#

Thank you!