Not sure if I asked the question right but I am using keylistener for keyboard inputs in keyboard.java. I want to call the keyboard.java file in the display.java (main class) so that when I run the display class, I press a key and print something out to test it. Here is the code for the keyboard.java file:
public class Keyboard implements KeyListener {
#Override
public void keyTyped(KeyEvent event) {
}
#Override
public void keyPressed(KeyEvent event) {
int key = event.getKeyCode();
if(key == KeyEvent.VK_E) {
System.out.println("E pressed");
}
}
#Override
public void keyReleased(KeyEvent event) {
}
}
I already typed in private Keyboard keyboard; into the display class but nothing is happening when I press the E key. I need help and I am using Eclipse (Im new to java).
Related
We areto create a plane and have its speed increase 5mph every time the user inputs the up arrow key and decrease 9mph when hitting the down key. It is supposed it report this back to the user in output box whenever whichever action is completed. How do I set it up so that the keylistener will tell the user that they have pressed the up key? Im still working on this so sorry if it is messy.
private class PolygonPanel implements MouseListener, MouseMotionListener, KeyListener
{
public void mousePressed(MouseEvent event){} //unused event
public void mouseDragged(MouseEvent event){} //unused event
public void keyReleased(KeyEvent event){}
public void keyTyped(KeyEvent event){}
//public void keyPressed(KeyEvent event){}
public void keyPressed(KeyEvent event) {
Integer planespeed=event.getKeyCode();
if (event.getKeyCode() == KeyEvent.VK_RIGHT)
{
//Not gonna use this.
}
if (event.getKeyCode() == 39)//KeyEvent.VK_LEFT
{
System.out.print("This key is up");
}
if (event.getKeyCode() == KeyEvent.VK_UP)
{
System.out.print("This key has been pressed");
//speed=planespeed+5;
}
if (event.getKeyCode() == KeyEvent.VK_DOWN)
{
//speed=planespeed-9;
}
repaint();
}
A frame doesn't detect keybaord input if it is not focused on. Therefore, in order to retrieve keyboard input of any kind, you would have to set your frame to be focusable, using a line such as setFocusable(true); in your constructor. You might also have to add a line such as addKeyListener(new PolygonPanel()); so the listener is registered at all.
I'm currently working on a little game. I'm using getKeyCode to move my character but the thing is that I don't want you to be able to keep moving if you hold in the button. Is there anyway I can use getKeyCode to only register on the first click and then won't register until I release the button and press again?
else if (event.getKeyCode()== KeyEvent.VK_UP)
{
spelare1.setLocation(spelare1.getX(),spelare1.getY()-50);
}
This is how it currently looks like.
I think you are confusing KeyEvents. VK_UP is the up arrow key. Use KeyEvent.KEY_RELEASED to react on a released key.
You can keep a boolean indicating whether the key is currently pressed. Then you can react once after each press, like in this example:
public static void main(String[] args) {
JFrame f = new JFrame();
f.addKeyListener(new KeyAdapter() {
private boolean pressed;
#Override
public void keyReleased(KeyEvent e) {
pressed = false;
}
#Override
public void keyPressed(KeyEvent e) {
if (!pressed) {
System.out.println("Key pressed: " + e.getKeyCode());
pressed = true;
}
}
});
SwingUtilities.invokeLater(new Runnable() {
public void run() {
f.setVisible(true);
}
});
}
Depending on your needs, you might want to have a separate state for each key, or just one shared state.
I have a JFrame set up with a keyListener added onto it at runtime, but when the Frame loads, there is a brief moment in which input will be received and then it just stops receiving input all together, here is the Keyboard.java code:
package uk.connorwright.rain.input;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Keyboard implements KeyListener {
private boolean[] keys = new boolean[120];
public boolean up, down, left, right;
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];
}
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
}
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}
public void keyTyped(KeyEvent e) {
}
}
This is the code in Game.java which relates to the Keyboard input:
private Keyboard key;
...
key = new Keyboard();
frame.addKeyListener(key);
....
public void update() {
key.update();
if (key.up) {
y--;
}
if (key.down) {
y++;
}
if (key.left) {
x--;
}
if (key.right) {
x++;
}
}
...
public static void main(String[] args) {
Game game = new Game();
KeyListener is a poor choice, it's responsible for generating events for a component when it is focusable AND it has focus.
Instead, you should considering using the Key Bindings API, which provides you the means to decide at what focus level it should generate key events
See How to Use Key Bindings for more details.
I haven't tested this, but I think it's probable that your KeyListener needs a component which has the keyboard focus, and a JFrame will never have that. Only subcomponents of the frame will have the focus.
From the tutorial:
https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html
Specifically, key events are fired by the component with the keyboard
focus when the user presses or releases keyboard keys. For detailed
information about focus, see How to Use the Focus Subsystem.
So no key events if you don't have focus.
I have a program which produces a JFrame and then a JPanel on top of it. For the program, I have tried implementing the KeyListener and then adding the methods (for both components), but the program does not pick any of my key strokes up. What am I doing wrong?
EDIT
This is my code. It is a part of the class which creates the JFrame. It still does not pick up the press of the ESC key.
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_ESCAPE){
System.out.println("Hi");
}else{
System.out.println("Hello");
}
}
#Override
public void keyReleased(KeyEvent e) {
}
Without your code, all I can tell you is that usually when people ask this they don't know that the interface KeyListener contain three methods as Agusti-N states in their answer here:
void keyTyped(KeyEvent)
void keyPressed(KeyEvent)
void keyReleased(KeyEvent)
If you use keyTyped and you are using event.getKeyCode() to check for the character entered, this will not work. You should use getKeyChar() for keyTyped and getKeyCode() for keyPressed and keyReleased. Otherwise you'll get null.
You should only use this if you do not have any other alternative, in most cases you want to use Key Bindings.
I want my java program to be running in the background by default, but use a keylistener to call my changewallpaper class. The changewallpaper class definently works, but the keylistener does not call the method. The keyevent will be changed later it's currently just for testing.
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class listener implements KeyListener {
public static void main(String[] args){
}
#Override
public void keyReleased(KeyEvent arg0) {
int key = arg0.getKeyCode();
if (key == KeyEvent.VK_UP) {
changewallpaper.main();
}
}
#Override
public void keyTyped(KeyEvent arg0) {
int key = arg0.getKeyCode();
if (key == KeyEvent.VK_UP) {
changewallpaper.main();
}
}
#Override
public void keyPressed(KeyEvent arg0) {
int key = arg0.getKeyCode();
if (key == KeyEvent.VK_UP) {
changewallpaper.main();
}
}
}
A KeyListener does not listen to all keyboard events indiscriminately - it only listens to events on a particular Component, when that Component has keyboard focus. You have to attach the listener to something with an addKeyListener method or similar.
See the Java How to Write a Key Listener tutorial