1

I am trying to get user keyboard input but I can't seem to get it to work without using the console.

What I am trying to achieve is to have the program to capture a user entering 12345 from the keyboard, without having to enter it in the console. And then return the integer captured.

public class InputWithoutConsoleTest {

public static void main(String[] args) {
    System.out.println(scanNumber());;
}

private static int scanNumber() {
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        return Integer.parseInt(br.readLine());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return 0;
}
}

Edit: The input source is a barcode scanner. It will scan a barcode and sends it to the program in the format of 1234567890{enter}. How can I capture this input in java?

4

3 回答 3

5

Jnativehook is a nice library which provides global keyboard (and mouse) input (at least I think this is what you mean by "without having to enter it in the console"). You don't need a GUI or anything else for it to work, so it is independent of the keyboard focus or other things that could prevent your application from getting keyboard input. With jnativehook you can simply register a global key listener which will be notified of any key-press happening in the system, like this (based on the examples from the project's wiki):

public class GlobalKeyListenerExample implements NativeKeyListener {
    public void nativeKeyPressed(NativeKeyEvent e) {
            System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
    }

    public void nativeKeyReleased(NativeKeyEvent e) {
            System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
    }

    public void nativeKeyTyped(NativeKeyEvent e) {
            System.out.println("Key Typed: " + e.getKeyText(e.getKeyCode()));
    }

    public static void main(String[] args) {
        try {
            GlobalScreen.registerNativeHook();

            GlobalKeyListenerExample listener = new GlobalKeyListenerExample()
            GlobalScreen.getInstance().addNativeKeyListener(listener);

        } catch (NativeHookException ex) {
            System.err.println(ex.getMessage());
        }
    }
}

Note: jnativehook uses platform specific native code to get global access to keyboard and mouse events, but it provides this for Windows, Mac OS and Linux, so it should work for most cases. If there are problems with your platform, report it on the project's bug tracker.

于 2013-03-30T19:29:43.943 回答
0

I can easily capture keyboard input into JTextField or JTextArea

于 2013-03-30T19:28:21.470 回答
-1

Java is sandboxed so I don't think you can just record keystrokes outside of a window. You would be much better off using a different language for this.

If you are really insistent on doing it in Java, you will probably have to do something with Swing like Hovercraft Full of Eels is talking about.

Something I am imagining is an invisible window, that is being focused/brought to the front would capture your keystrokes. I'm not entirely sure if this is possible in Java.

If you are okay with having some sort of visible window as long as it isn't the console, you can just create a Jframe and add a TextBox with swing.

于 2013-03-30T19:00:02.287 回答