-2

So I'm trying to "log" keys that are being pressed. The problem is when I run my code, the program just auto stops and sends me back to where I can code.

I use Eclipse


    import org.jnativehook.keyboard.NativeKeyEvent;
    import org.jnativehook.keyboard.NativeKeyListener;

    public class Listener implements NativeKeyListener {
        public static void main(String[] args) {
            System.out.print("Program started");
        }

        @Override
        public void nativeKeyPressed(NativeKeyEvent arg0) {
            System.out.print("A key was pressed");
        }

        @Override
        public void nativeKeyReleased(NativeKeyEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void nativeKeyTyped(NativeKeyEvent arg0) {
            // TODO Auto-generated method stub

        }   
    }

  • 6
    Your main method logs a string and immediately terminates. Where do you expect it to listen to key events? – Florian S. Jun 27 '17 at 23:28
  • 3
    Were you intending to register your `Listener` class with a some kind of monitor? Something like [this](http://www.programcreek.com/java-api-examples/index.php?api=org.jnativehook.keyboard.NativeKeyListener) maybe – MadProgrammer Jun 27 '17 at 23:30
  • Being a JVM I would not do more than printing `Program started`, after all why would I do more than it? – Jean-Baptiste Yunès Dec 19 '17 at 19:14

1 Answers1

1

You need to add the listener to the GlobalScreen object and keep the program running.

Alex Barker
  • 4,316
  • 4
  • 28
  • 47