Browse Source

Refactoring out interfaces; adjusting POM for Central upload.

Matt Clark 3 years ago
parent
commit
4e039d8ff4

+ 39 - 1
pom.xml

@@ -3,13 +3,23 @@
 	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 	<modelVersion>4.0.0</modelVersion>
 
+	<organization>
+		<name>MClarkDev.com</name>
+		<url>https://www.mclarkdev.com/tools/KeyHook</url>
+	</organization>
+
 	<groupId>com.mclarkdev.tools</groupId>
 	<artifactId>KeyHook</artifactId>
-	<version>1.0</version>
+	<version>1.1</version>
 	<packaging>jar</packaging>
 
 	<name>KeyHook</name>
 	<url>http://www.mclarkdev.com/tools/KeyHook</url>
+	<description>A minimal, Java based, global key press listener.</description>
+
+	<scm>
+		<url>https://git.mclarkdev.com/MClarkDev.com--Public/KeyHook.git</url>
+	</scm>
 
 	<properties>
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -19,6 +29,34 @@
 
 	<build>
 		<sourceDirectory>src/main/java</sourceDirectory>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-source-plugin</artifactId>
+				<version>3.1.0</version>
+				<executions>
+					<execution>
+						<id>attach-sources</id>
+						<goals>
+							<goal>jar</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-javadoc-plugin</artifactId>
+				<version>3.1.1</version>
+				<executions>
+					<execution>
+						<id>attach-javadocs</id>
+						<goals>
+							<goal>jar</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+		</plugins>
 	</build>
 
 	<dependencies>

+ 14 - 0
src/main/java/com/mclarkdev/tools/KeyHook/KeyboardListener.java

@@ -0,0 +1,14 @@
+package com.mclarkdev.tools.KeyHook;
+
+import java.util.ArrayList;
+
+/**
+ * Implement this listener to handle global key press events.
+ * 
+ * @author Matt Clark
+ *
+ */
+public interface KeyboardListener {
+
+	public void onKeysChanged(ArrayList<Integer> keySet);
+}

+ 23 - 20
src/main/java/com/mclarkdev/tools/KeyHook/KeyboardManager.java

@@ -3,34 +3,37 @@ package com.mclarkdev.tools.KeyHook;
 import java.util.ArrayList;
 import java.util.HashSet;
 
-import com.mclarkdev.tools.KeyHook.KeyboardHook.KeyListener;
 import com.sun.jna.platform.win32.WinUser.KBDLLHOOKSTRUCT;
 
+/**
+ * The KeyboardManager handles the key press events processed from the
+ * NativeKeyHook and maintains a map of all the keys currently pressed.
+ * Attaching a listener to this manager will cause each keyboard event to return
+ * a map of all keys currently pressed.
+ * 
+ * @author Matt Clark
+ *
+ */
 public class KeyboardManager {
 
-	public interface EventListener {
+	private static final KeyboardManager keyboardManager = new KeyboardManager();
 
-		public void onEvent(ArrayList<Integer> keyMap);
-	}
-
-	private static KeyboardManager keyboardManager;
+	private final HashSet<Integer> keysDown = new HashSet<>();
 
-	private final HashSet<Integer> keysDown;
-
-	private EventListener listener;
+	private KeyboardListener listener;
 
 	private KeyboardManager() {
 
-		keysDown = new HashSet<>();
-		KeyboardHook.getInstance().attachListener(keyListener);
+		NativeKeyHook.getHook()//
+				.attachListener(keyListener);
 	}
 
-	public void setListener(EventListener listener) {
+	public void setListener(KeyboardListener listener) {
 
 		this.listener = listener;
 	}
 
-	private KeyListener keyListener = new KeyListener() {
+	private NativeKeyListener keyListener = new NativeKeyListener() {
 
 		public void onKeyUp(KBDLLHOOKSTRUCT event) {
 
@@ -41,7 +44,9 @@ public class KeyboardManager {
 				keys = new ArrayList<>(keysDown);
 			}
 
-			listener.onEvent(keys);
+			if (listener != null) {
+				listener.onKeysChanged(keys);
+			}
 		}
 
 		public void onKeyDown(KBDLLHOOKSTRUCT event) {
@@ -53,15 +58,13 @@ public class KeyboardManager {
 				keys = new ArrayList<>(keysDown);
 			}
 
-			listener.onEvent(keys);
+			if (listener != null) {
+				listener.onKeysChanged(keys);
+			}
 		}
 	};
 
-	public static KeyboardManager getInstance() {
-
-		if (keyboardManager == null) {
-			keyboardManager = new KeyboardManager();
-		}
+	public static KeyboardManager getManager() {
 
 		return keyboardManager;
 	}

+ 15 - 15
src/main/java/com/mclarkdev/tools/KeyHook/KeyboardHook.java → src/main/java/com/mclarkdev/tools/KeyHook/NativeKeyHook.java

@@ -5,27 +5,27 @@ import com.sun.jna.platform.win32.User32;
 import com.sun.jna.platform.win32.WinDef.LRESULT;
 import com.sun.jna.platform.win32.WinDef.WPARAM;
 
-public class KeyboardHook {
+/**
+ * The NativeKeyHook sets up the Windows keyboard hooks and passes the events
+ * through to the KeyboardManager.
+ * 
+ * @author Matt Clark
+ *
+ */
+public class NativeKeyHook {
 
-	public interface KeyListener {
+	private static NativeKeyHook keyboardHook;
 
-		public void onKeyUp(User32.KBDLLHOOKSTRUCT lParam);
-
-		public void onKeyDown(User32.KBDLLHOOKSTRUCT lParam);
-	}
-
-	private static KeyboardHook keyboardHook;
-
-	private KeyListener listener;
+	private NativeKeyListener listener;
 
 	private User32.HHOOK hHook;
 
-	private KeyboardHook() {
+	private NativeKeyHook() {
 
 		(new Thread(runThread)).start();
 	}
 
-	public void attachListener(KeyListener listener) {
+	public void attachListener(NativeKeyListener listener) {
 
 		if (this.listener != null) {
 			throw new IllegalArgumentException("listener already attached");
@@ -56,7 +56,7 @@ public class KeyboardHook {
 		public void run() {
 
 			// register the hook
-			KeyboardHook.this.hHook = User32.INSTANCE.SetWindowsHookEx(//
+			NativeKeyHook.this.hHook = User32.INSTANCE.SetWindowsHookEx(//
 					User32.WH_KEYBOARD_LL, callback, Kernel32.INSTANCE.GetModuleHandle(null), 0);
 
 			// look for messages forever
@@ -74,10 +74,10 @@ public class KeyboardHook {
 		}
 	};
 
-	public static KeyboardHook getInstance() {
+	protected static NativeKeyHook getHook() {
 
 		if (keyboardHook == null) {
-			keyboardHook = new KeyboardHook();
+			keyboardHook = new NativeKeyHook();
 		}
 
 		return keyboardHook;

+ 16 - 0
src/main/java/com/mclarkdev/tools/KeyHook/NativeKeyListener.java

@@ -0,0 +1,16 @@
+package com.mclarkdev.tools.KeyHook;
+
+import com.sun.jna.platform.win32.User32;
+
+/**
+ * The NativeKeyListener handles the native key press events from Windows.
+ * 
+ * @author Matt Clark
+ *
+ */
+public interface NativeKeyListener {
+
+	public void onKeyUp(User32.KBDLLHOOKSTRUCT lParam);
+
+	public void onKeyDown(User32.KBDLLHOOKSTRUCT lParam);
+}