Skip to content

Commit

Permalink
Version 3.0, added raw input support, merged native libraries, added
Browse files Browse the repository at this point in the history
functions to list connected keyboards / mice, see #12, fixed some bugs
  • Loading branch information
kristian committed Jun 11, 2017
1 parent 210ba4c commit cca5d4d
Show file tree
Hide file tree
Showing 17 changed files with 861 additions and 746 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ You can include `system-hook` from this GitHub repository by adding this depende
<dependency>
<groupId>lc.kra.system</groupId>
<artifactId>system-hook</artifactId>
<version>2.6</version>
<version>3.0</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 2.6.{build}
version: 3.0.{build}

branches:
only:
Expand Down
13 changes: 3 additions & 10 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,9 @@ SOFTWARE.
</target>

<target name="compile" depends="initialize" description="Compiles JNI source files.">
<fail message="Property ant.native.library not set. Please set ant.native.library to 'keyboard' or 'mouse'.">
<condition>
<not>
<isset property="ant.native.library" />
</not>
</condition>
</fail>
<property name="ant.native.library.result" value="${ant.native.library}hook-${ant.os}-${ant.os.arch}.dll" />
<property name="ant.native.library" value="systemhook-${ant.os}-${ant.os.arch}.dll" />

<cc objdir="${ant.dir.objects}" outfile="${ant.dir.library}/${ant.native.library.result}" subsystem="console" debug="${ant.native.debug}">
<cc objdir="${ant.dir.objects}" outfile="${ant.dir.library}/${ant.native.library}" subsystem="console" debug="${ant.native.debug}">
<compiler name="${ant.native.compiler}" />
<compilerarg value="-std=c99" />

Expand All @@ -138,7 +131,7 @@ SOFTWARE.
<linkerarg value="-static" if="ant.os.windows" />
<linkerarg value="-add-stdcall-alias" if="ant.os.windows" />

<fileset dir="${ant.dir.native}/${ant.native.library}/${ant.os}">
<fileset dir="${ant.dir.native}/${ant.os}">
<include name="**/*.c" />
</fileset>
</cc>
Expand Down
9 changes: 2 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>lc.kra.system</groupId>
<artifactId>system-hook</artifactId>
<version>2.6</version>
<version>3.0</version>
<description>Global Keyboard / Mouse Hook for Java applications.</description>
<url>https://github.com/kristian/system-hook</url>

Expand Down Expand Up @@ -103,12 +103,7 @@
</goals>
<configuration>
<target>
<ant>
<property name="ant.native.library" value="keyboard"/>
</ant>
<ant>
<property name="ant.native.library" value="mouse"/>
</ant>
<ant/>
</target>
</configuration>
</execution>
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/lc/kra/system/LibraryLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,30 @@
import java.util.zip.Checksum;

public class LibraryLoader {
private static final String LIBRARY_NAME = "systemhook";

private static boolean libraryLoad;

/**
* Tries to laod the library with the given name
*
* @param name The name of the library to load
* @throws UnsatisfiedLinkError Thrown in case loading the library fails
*/
public static void loadLibrary(String name) throws UnsatisfiedLinkError {
String libName = System.getProperty(name+".lib.name", System.mapLibraryName(name).replaceAll("\\.jnilib$", "\\.dylib")),
libPath = System.getProperty(name+".lib.path");
public static synchronized void loadLibrary() throws UnsatisfiedLinkError {
if(libraryLoad) return; //library already loaded
String libName = System.getProperty(LIBRARY_NAME+".lib.name", System.mapLibraryName(LIBRARY_NAME).replaceAll("\\.jnilib$", "\\.dylib")),
libPath = System.getProperty(LIBRARY_NAME+".lib.path");

try {
if(libPath==null)
System.loadLibrary(libName);
else System.load(new File(libPath, libName).getAbsolutePath());
libraryLoad = true;
return;
} catch(UnsatisfiedLinkError e) { /* do nothing, try next */ }

libName = System.mapLibraryName(name+'-'+getOperatingSystemName()+'-'+getOperatingSystemArchitecture())
libName = System.mapLibraryName(LIBRARY_NAME+'-'+getOperatingSystemName()+'-'+getOperatingSystemArchitecture())
.replaceAll("\\.jnilib$", "\\.dylib"); // for JDK < 1.7
String libNameExtension = libName.substring(libName.lastIndexOf('.')),
libResourcePath = LibraryLoader.class.getPackage().getName().replace('.', '/')+"/lib/"+libName;
Expand All @@ -58,7 +64,7 @@ public static void loadLibrary(String name) throws UnsatisfiedLinkError {
try {
if((inputStream=LibraryLoader.class.getClassLoader().getResourceAsStream(libResourcePath))==null)
throw new FileNotFoundException("lib: "+libName+" not found in lib directory");
File tempFile = File.createTempFile(name+"-", libNameExtension);
File tempFile = File.createTempFile(LIBRARY_NAME+"-", libNameExtension);

Checksum checksum = new CRC32();
outputStream = new FileOutputStream(tempFile);
Expand All @@ -69,12 +75,13 @@ public static void loadLibrary(String name) throws UnsatisfiedLinkError {
}
outputStream.close();

File libFile = new File(tempFile.getParentFile(), name+"+"+checksum.getValue()+libNameExtension);
File libFile = new File(tempFile.getParentFile(), LIBRARY_NAME+"+"+checksum.getValue()+libNameExtension);
if(!libFile.exists())
tempFile.renameTo(libFile);
else tempFile.delete();

System.load(libFile.getAbsolutePath());
libraryLoad = true;
} catch(IOException e) {
throw new UnsatisfiedLinkError(e.getMessage());
} finally {
Expand Down
52 changes: 39 additions & 13 deletions src/main/java/lc/kra/system/keyboard/GlobalKeyboardHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static lc.kra.system.keyboard.event.GlobalKeyEvent.VK_SHIFT;

import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingQueue;
Expand All @@ -49,7 +50,7 @@ public class GlobalKeyboardHook {

private BlockingQueue<GlobalKeyEvent> inputBuffer =
new LinkedBlockingQueue<GlobalKeyEvent>();
private boolean libraryLoad, menuPressed, shiftPressed, controlPressed, extendedKey;
private boolean menuPressed, shiftPressed, controlPressed, extendedKey;

private List<GlobalKeyListener> listeners = new CopyOnWriteArrayList<GlobalKeyListener>();
private Thread eventDispatcher = new Thread() {{
Expand All @@ -69,7 +70,7 @@ public void run() {
} catch(InterruptedException e) { /* thread got interrupted, break */ }
}
};

/**
* Instantiate a new GlobalKeyboardHook.
*
Expand All @@ -82,17 +83,28 @@ public void run() {
* @throws UnsatisfiedLinkError Thrown if loading the native library failed
* @throws RuntimeException Thrown if registering the low-level keyboard hook failed
*/
public GlobalKeyboardHook() throws UnsatisfiedLinkError {
if(!libraryLoad) { LibraryLoader.loadLibrary("keyboardhook"); libraryLoad = true; }
public GlobalKeyboardHook() throws UnsatisfiedLinkError { this(false); }

/**
* Instantiate a new GlobalKeyboardHook.
*
* @see #GlobalKeyboardHook()
*
* @param raw Use raw input, instead of a low-level system hook. Raw input will provide additional information of the device
* @throws UnsatisfiedLinkError Thrown if loading the native library failed
* @throws RuntimeException Thrown if registering the low-level keyboard hook failed
*/
public GlobalKeyboardHook(boolean raw) throws UnsatisfiedLinkError {
LibraryLoader.loadLibrary(); // load the library, in case it's not already loaded

// register a keyboard hook (throws a RuntimeException in case something goes wrong)
keyboardHook = new NativeKeyboardHook() {
keyboardHook = new NativeKeyboardHook(raw) {
/**
* Handle the input virtualKeyCode and transitionState, create event and add it to the inputBuffer
*/
@Override public void handleKey(int virtualKeyCode, int transitionState, char keyChar) {
@Override public void handleKey(int virtualKeyCode, int transitionState, char keyChar, long deviceHandle) {
switchControlKeys(virtualKeyCode, transitionState);
inputBuffer.add(new GlobalKeyEvent(this, virtualKeyCode, transitionState, keyChar, menuPressed, shiftPressed, controlPressed, extendedKey));
inputBuffer.add(new GlobalKeyEvent(this, virtualKeyCode, transitionState, keyChar, menuPressed, shiftPressed, controlPressed, extendedKey, deviceHandle));
}
};

Expand Down Expand Up @@ -153,13 +165,25 @@ public void shutdownHook() {
}
}

private abstract class NativeKeyboardHook extends Thread {
/**
* Lists all connected keyboards
*
* @return A map of device handles and display names
*/
public static Map<Long,String> listKeybords() throws UnsatisfiedLinkError {
LibraryLoader.loadLibrary(); // load the library, in case it's not already loaded
return NativeKeyboardHook.listDevices();
}

private static abstract class NativeKeyboardHook extends Thread {
private int status;
private boolean raw;

public NativeKeyboardHook() {
public NativeKeyboardHook(boolean raw) {
super("Global Keyboard Hook Thread");
setDaemon(false); setPriority(MAX_PRIORITY);
synchronized(this) {
this.raw = raw;
try { start(); wait(); }
catch (InterruptedException e) {
throw new RuntimeException(e);
Expand All @@ -171,15 +195,17 @@ public NativeKeyboardHook() {
}

@Override public void run() {
status = registerHook();
status = registerHook(raw);
synchronized(this) {
notifyAll(); }
}

public native final int registerHook();
public native final int registerHook(boolean raw);
public native final void unregisterHook();

public abstract void handleKey(int virtualKeyCode, int transitionState, char keyChar);
public static native final Map<Long,String> listDevices();

public abstract void handleKey(int virtualKeyCode, int transitionState, char keyChar, long deviceHandle);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,9 @@ public class GlobalKeyEvent extends EventObject {
shiftPressed,
controlPressed,
extendedKey;
private long deviceHandle;

public GlobalKeyEvent(Object source, int virtualKeyCode, int transitionState, char keyChar, boolean menuPressed, boolean shiftPressed, boolean controlPressed, boolean extendedKey) {
public GlobalKeyEvent(Object source, int virtualKeyCode, int transitionState, char keyChar, boolean menuPressed, boolean shiftPressed, boolean controlPressed, boolean extendedKey, long deviceHandle) {
super(source);
this.virtualKeyCode = virtualKeyCode;
this.transitionState = transitionState;
Expand All @@ -229,6 +230,7 @@ public GlobalKeyEvent(Object source, int virtualKeyCode, int transitionState, ch
this.shiftPressed = shiftPressed;
this.controlPressed = controlPressed;
this.extendedKey = extendedKey;
this.deviceHandle = deviceHandle;
}

/**
Expand Down Expand Up @@ -269,6 +271,11 @@ public GlobalKeyEvent(Object source, int virtualKeyCode, int transitionState, ch
*/
public boolean isExtendedKey() { return extendedKey; }

/**
* Returns the handle of the keyboard the key was pressed on.
*/
public long getDeviceHandle() { return deviceHandle; }

/**
* Returns a String representation of this GlobalKeyEvent.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,55 @@
/**
* Copyright (c) 2016 Kristian Kraljic
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package lc.kra.system.keyboard.example;

import lc.kra.system.keyboard.GlobalKeyboardHook;
import lc.kra.system.keyboard.event.GlobalKeyAdapter;
import lc.kra.system.keyboard.event.GlobalKeyEvent;

public class GlobalKeyboardExample {
private static boolean run = true;
public static void main(String[] args) {
// might throw a UnsatisfiedLinkError if the native library fails to load or a RuntimeException if hooking fails
GlobalKeyboardHook keyboardHook = new GlobalKeyboardHook();

System.out.println("Global keyboard hook successfully started, press [escape] key to shutdown.");
keyboardHook.addKeyListener(new GlobalKeyAdapter() {
@Override public void keyPressed(GlobalKeyEvent event) {
System.out.println(event);
if(event.getVirtualKeyCode()==GlobalKeyEvent.VK_ESCAPE)
run = false;
}
@Override public void keyReleased(GlobalKeyEvent event) {
System.out.println(event); }
});

try {
while(run) Thread.sleep(128);
} catch(InterruptedException e) { /* nothing to do here */ }
finally { keyboardHook.shutdownHook(); }
}
/**
* Copyright (c) 2016 Kristian Kraljic
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package lc.kra.system.keyboard.example;

import java.util.Map.Entry;

import lc.kra.system.keyboard.GlobalKeyboardHook;
import lc.kra.system.keyboard.event.GlobalKeyAdapter;
import lc.kra.system.keyboard.event.GlobalKeyEvent;

public class GlobalKeyboardExample {
private static boolean run = true;
public static void main(String[] args) {
// might throw a UnsatisfiedLinkError if the native library fails to load or a RuntimeException if hooking fails
GlobalKeyboardHook keyboardHook = new GlobalKeyboardHook(true); // use false here to switch to hook instead of raw input

System.out.println("Global keyboard hook successfully started, press [escape] key to shutdown. Connected keyboards:");
for(Entry<Long,String> keyboard:GlobalKeyboardHook.listKeybords().entrySet())
System.out.format("%d: %s\n", keyboard.getKey(), keyboard.getValue());

keyboardHook.addKeyListener(new GlobalKeyAdapter() {
@Override public void keyPressed(GlobalKeyEvent event) {
System.out.println(event);
if(event.getVirtualKeyCode()==GlobalKeyEvent.VK_ESCAPE)
run = false;
}
@Override public void keyReleased(GlobalKeyEvent event) {
System.out.println(event); }
});

try {
while(run) Thread.sleep(128);
} catch(InterruptedException e) { /* nothing to do here */ }
finally { keyboardHook.shutdownHook(); }
}
}
Loading

0 comments on commit cca5d4d

Please sign in to comment.