Morph
Morph

Reputation: 173

Get Keyboard / Mouse Input in Java

Is there a way to get the user input without GUI Listeners or Scanner class? Something like this:

if (Input.isKeyDown(KeyCode.VK_A)) {
    // Do something
}

In Unity3D you can get the Users input using this way.

Best regards Morph

Upvotes: 0

Views: 3580

Answers (3)

George Tsakiridis
George Tsakiridis

Reputation: 21

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

public class KeyPressListener implements KeyListener {

    // when a key is pressed
    public void keyPressed(KeyEvent e){

        // the key you pressed
        char k = e.getKeyChar();

        // prints out what key you pressed
        System.out.println(k);   
    }

    // when a key is typed
    public void keyTyped(KeyEvent e){

    }

    // when a key is released
    public void keyReleased(KeyEvent e){

    }
}

this will get you keyboard input

Upvotes: 1

Nuno Duarte
Nuno Duarte

Reputation: 1036

Take a look at this page: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/Input.Keys.html

You will find what you want.

Hope this helps!

EDIT: The link points to a page that contains an interface created for Input in some backends (desktop, GWT, etc.).

It may be useful for what you want.

Upvotes: 1

Guillaume
Guillaume

Reputation: 5553

You can only capture inputs in the context of your application. Else how could you know the user was not inputing on an other application?

In Java you can either use GUI (Swing or JavaFX) or console (System.in.read()) standard inputs.

Upvotes: 0

Related Questions