JNativeHook proof of concept
authorTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 18 Aug 2019 17:29:24 +0000 (19:29 +0200)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 18 Aug 2019 17:29:24 +0000 (19:29 +0200)
lib/jnativehook-2.1.0.jar [new file with mode: 0644]
src/kaka/cakelight/CakeLight.java

diff --git a/lib/jnativehook-2.1.0.jar b/lib/jnativehook-2.1.0.jar
new file mode 100644 (file)
index 0000000..f9cd3b7
Binary files /dev/null and b/lib/jnativehook-2.1.0.jar differ
index 3769dce..c2f896d 100644 (file)
@@ -1,5 +1,12 @@
 package kaka.cakelight;
 
+import org.jnativehook.GlobalScreen;
+import org.jnativehook.NativeHookException;
+import org.jnativehook.keyboard.NativeKeyAdapter;
+import org.jnativehook.keyboard.NativeKeyEvent;
+import org.jnativehook.mouse.NativeMouseEvent;
+import org.jnativehook.mouse.NativeMouseMotionAdapter;
+
 public class CakeLight {
     private Configuration config;
     private Mode mode;
@@ -27,6 +34,7 @@ public class CakeLight {
 
     public void startLoop() {
         Console.start(this, config);
+        initNativeHook();
         // TODO
 //        FrameGrabber grabber = FrameGrabber.from(config);
 //        grabber.prepare();
@@ -41,6 +49,26 @@ public class CakeLight {
 //     saveFile(data, "/home/kaka/test.img");
     }
 
+    private void initNativeHook() {
+        try {
+            GlobalScreen.registerNativeHook();
+            GlobalScreen.addNativeKeyListener(new NativeKeyAdapter() {
+                @Override
+                public void nativeKeyPressed(NativeKeyEvent e) {
+                    System.out.println("key code = " + e.getKeyCode() + ", key text = '" + NativeKeyEvent.getKeyText(e.getKeyCode()) + "'");
+                }
+            });
+            GlobalScreen.addNativeMouseMotionListener(new NativeMouseMotionAdapter() {
+                @Override
+                public void nativeMouseMoved(NativeMouseEvent e) {
+                    System.out.println("mouse point = " + e.getPoint());
+                }
+            });
+        } catch (NativeHookException e) {
+            e.printStackTrace();
+        }
+    }
+
     public void turnOff() {
         cleanup();
         ledController.onFrame(LedFrame.from(config).fillColor(0, 0, 0));