1

I was wondering how I could make an if statement:

if("up".equals(message))
{
     System.exit(0); //What I want to change
}

Act as if a key was pressed?

Like per-say I want this if statement to make it so that when up equals the message it will output a keypress like I pressed that key in the first place. I can't explain it too good but I hope this helps more

Just for anyone in the future I used this

try {
    Robot robot = new Robot();

    // Simulate a key press
    robot.keyPress(KeyEvent.VK_A); // press a
    robot.keyRelease(KeyEvent.VK_A); // release a

} catch (AWTException e) {
    e.printStackTrace();
}

Thanks to Jdsfighter

Fusion
  • 70
  • 1
  • 10

4 Answers4

0

There are many many tuts, docos and q/as for catching key events, e.g. How can I capture keyboard events are from which keys?

Community
  • 1
  • 1
lalaland
  • 71
  • 3
0

Are you wanting to physically press a key?

If so, the method to that is as follows:

try {
    Robot robot = new Robot();

    // Simulate a key press
    robot.keyPress(KeyEvent.VK_A); // press a
    robot.keyRelease(KeyEvent.VK_A); // release a

} catch (AWTException e) {
    e.printStackTrace();
}

EDIT: Revised

In that case, here's a nice Oracle example on how to make a keylistener. From there you can use that information for whatever you need. Link

JD Davis
  • 3,517
  • 4
  • 28
  • 61
0

Are you wanting to handle Input? To handle Input you need a KeyListener.

e.g:

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;


public class InputHandler implements KeyListener {

@Override
public void keyPressed(KeyEvent e) {
    System.out.println("pressed "+e.getKeyChar());

}

@Override
public void keyReleased(KeyEvent e) {
    System.out.println("released "+e.getKeyChar());

}

@Override
public void keyTyped(KeyEvent e) {
    System.out.println("typed "+e.getKeyChar());

}
public static void main(String[] args) {
    JFrame jf = new JFrame();
    jf.addKeyListener(new InputHandler());
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setVisible(true);
}

}
0

Your context is not really clear, but i think you looking for an implementation of KeyListener. Check the attached working example and see if it helps.

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener; import javax.swing.JFrame;

public class KeyListenerExample implements KeyListener{

public static void main(String[] args) throws InterruptedException {
    new KeyListenerExample().createFrame();
}

private void createFrame(){
    JFrame jFrame = new JFrame();
    jFrame.setSize(200, 300);
    jFrame.addKeyListener(this);
    jFrame.setVisible(true);
}
public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_UP)
    {
        System.out.println("UP arrow key pressed. Going to exit.");
         System.exit(0); //What I want to change
    }else{
        System.out.println("Some key pressed");
    }       
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {       
}

}