Skip to content

Commit ab57f60

Browse files
committed
Initial commit, Version 2.0, MIT License
0 parents  commit ab57f60

21 files changed

+2190
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/target/
2+
.settings/
3+
.classpath
4+
.project
5+
.cproject
6+
*.o
7+
*.obj
8+
*.lib
9+
*.so
10+
*.dll

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2015 Kristian Kraljic
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.

README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
![Java (low-level) System Hook](https://raw.github.com/kristian/system-hook/assets/system-hook-logo.png) [![Build Status](https://ci.appveyor.com/api/projects/status/github/kristian/system-hook?branch=master&svg=true)](https://ci.appveyor.com/project/kristian/system-hook)
2+
3+
Java (low-level) System Hook provides a very light-weight global keyboard and mouse listener for Java. Generally keyboard and mouse events in Java work, only if the registered component is in focus. For example, in case any window looses its focus (e.g. when minimized), it stops receiving any more keyboard or mouse events. Through a low-level system-wide hook the *global keyboard / mouse hook* is able to deliver those events regardless.
4+
5+
The *Java (low-level) System Hook* comes bundled with native libraries (for Windows 32 & 64 bit) to register the hooks via the Java Native Interface (JNI). The libraries are load dynamically depending on the version and architecture of your operating system. The libraries to track the keyboard and mouse events can be loaded and used separately.
6+
7+
Using the `GlobalKeyboardHook` class a `GlobalKeyListener` or the adapter class `GlobalKeyAdapter` can be registered to listen to `keyPressed` and `keyReleased` events like so:
8+
```java
9+
import lc.kra.system.keyboard.GlobalKeyboardHook;
10+
import lc.kra.system.keyboard.event.GlobalKeyAdapter;
11+
import lc.kra.system.keyboard.event.GlobalKeyEvent;
12+
13+
public class GlobalKeyboardExample {
14+
private static boolean run = true;
15+
public static void main(String[] args) {
16+
// might throw a UnsatisfiedLinkError if the native library fails to load or a RuntimeException if hooking fails
17+
GlobalKeyboardHook keyboardHook = new GlobalKeyboardHook();
18+
19+
System.out.println("Global keyboard hook successfully started, press [escape] key to shutdown.");
20+
keyboardHook.addKeyListener(new GlobalKeyAdapter() {
21+
@Override public void keyPressed(GlobalKeyEvent event) {
22+
System.out.println(event);
23+
if(event.getVirtualKeyCode()==GlobalKeyEvent.VK_ESCAPE)
24+
run = false;
25+
}
26+
@Override public void keyReleased(GlobalKeyEvent event) {
27+
System.out.println(event); }
28+
});
29+
30+
try {
31+
while(run) Thread.sleep(128);
32+
} catch(InterruptedException e) { /* nothing to do here */ }
33+
finally { keyboardHook.shutdownHook(); }
34+
}
35+
}
36+
```
37+
38+
Using the `GlobalMouseHook` class a `GlobalMouseListener` or the adapter class `GlobalMouseAdapter` can be registered to listen to `mousePressed`, `mouseReleased`, `mouseMoved` and `mouseWheel` events like so:
39+
```java
40+
import lc.kra.system.mouse.GlobalMouseHook;
41+
import lc.kra.system.mouse.event.GlobalMouseAdapter;
42+
import lc.kra.system.mouse.event.GlobalMouseEvent;
43+
44+
public class GlobalMouseExample {
45+
private static boolean run = true;
46+
public static void main(String[] args) {
47+
// might throw a UnsatisfiedLinkError if the native library fails to load or a RuntimeException if hooking fails
48+
GlobalMouseHook mouseHook = new GlobalMouseHook();
49+
50+
System.out.println("Global mouse hook successfully started, press [middle] mouse button to shutdown.");
51+
mouseHook.addMouseListener(new GlobalMouseAdapter() {
52+
@Override public void mousePressed(GlobalMouseEvent event) {
53+
System.out.println(event);
54+
if((event.getButtons()&GlobalMouseEvent.BUTTON_LEFT)!=GlobalMouseEvent.BUTTON_NO
55+
&& (event.getButtons()&GlobalMouseEvent.BUTTON_RIGHT)!=GlobalMouseEvent.BUTTON_NO)
56+
System.out.println("Both mouse buttons are currenlty pressed!");
57+
if(event.getButton()==GlobalMouseEvent.BUTTON_MIDDLE)
58+
run = false;
59+
}
60+
@Override public void mouseReleased(GlobalMouseEvent event) {
61+
System.out.println(event); }
62+
@Override public void mouseMoved(GlobalMouseEvent event) {
63+
System.out.println(event); }
64+
@Override public void mouseWheel(GlobalMouseEvent event) {
65+
System.out.println(event); }
66+
});
67+
68+
try {
69+
while(run) Thread.sleep(128);
70+
} catch(InterruptedException e) { /* nothing to do here */ }
71+
finally { mouseHook.shutdownHook(); }
72+
}
73+
}
74+
```
75+
76+
Please find some background information about this framework [on my blog](http://kra.lc/blog/2016/02/java-global-system-hook/).
77+
78+
Usage
79+
-----
80+
81+
Feel free to use the classes `lc.kra.system.keyboard.GobalKeyboardHook` and `lc.kra.system.mouse.GlobalMouseHook` by coping the `.java` files to your project. Alternatively check the [**releases section**](https://github.com/kristian/system-hook/releases) for pre-bundled Java archives (JAR).
82+
83+
The `LibraryLoader` will first attempt to load the native libraries from the `java.library.path` and fall back checking the archives `/lc/kra/system/lib` package, if no libraries where found.
84+
85+
### Maven Dependency
86+
You can include `system-hook` from this GitHub repository by adding this dependency to your `pom.xml`:
87+
88+
```xml
89+
<dependency>
90+
<groupId>lc.kra.system</groupId>
91+
<artifactId>system-hook</artifactId>
92+
<version>2.0</version>
93+
</dependency>
94+
```
95+
96+
Additionally you will have to add the following repository to your `pom.xml`:
97+
98+
```xml
99+
<repositories>
100+
<repository>
101+
<id>system-hook-mvn-repo</id>
102+
<url>https://raw.github.com/kristian/system-hook/mvn-repo/</url>
103+
<snapshots>
104+
<enabled>true</enabled>
105+
<updatePolicy>always</updatePolicy>
106+
</snapshots>
107+
</repository>
108+
</repositories>
109+
```
110+
111+
Build
112+
-----
113+
114+
To build `system-hook` on your machine, checkout the repository, `cd` into it, and call:
115+
```
116+
mvn clean install
117+
```
118+
(A `C99` compatible compiler / linker bundle is required to build the native libraries, see [MSYS2](http://sourceforge.net/projects/msys2/))
119+
120+
License
121+
-------
122+
123+
The code is available under the terms of the [MIT License](http://opensource.org/licenses/MIT).

appveyor.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
version: 2.0.{build}
2+
3+
branches:
4+
only:
5+
- master
6+
7+
os: MinGW
8+
9+
platform:
10+
- x86
11+
- x64
12+
13+
configuration:
14+
- Debug
15+
- Release
16+
17+
install:
18+
- ps: |
19+
Add-Type -AssemblyName System.IO.Compression.FileSystem
20+
If(!(Test-Path -Path "C:\maven")) {
21+
(new-object System.Net.WebClient).DownloadFile(
22+
'http://www.apache.org/dist/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.zip',
23+
'C:\maven-bin.zip'
24+
)
25+
[System.IO.Compression.ZipFile]::ExtractToDirectory("C:\maven-bin.zip", "C:\maven")
26+
}
27+
- cmd: SET PATH=C:\maven\apache-maven-3.3.9\bin;%JAVA_HOME%\bin;%PATH%
28+
- cmd: SET MAVEN_OPTS=-Xmx4g
29+
- cmd: SET JAVA_OPTS=-Xmx4g
30+
- ps: |
31+
If($env:PLATFORM -ieq "x86") {
32+
$env:PATH="C:\msys64\mingw32\bin;"+$env:path
33+
$env:MSYSTEM="MINGW32"
34+
$env:ANT_ARCH="x86"
35+
} Else {
36+
$env:PATH="C:\msys64\mingw64\bin;"+$env:path
37+
$env:MSYSTEM="MINGW64"
38+
$env:ANT_ARCH="amd64"
39+
}
40+
41+
build_script:
42+
- ps: |
43+
if($env:CONFIGURATION -ne "Debug")
44+
{ mvn clean install --batch-mode -DskipTest "-Dant.os.arch=$env:ANT_ARCH" }
45+
else { mvn clean install --batch-mode -DskipTest "-Dant.os.arch=$env:ANT_ARCH" "-Dant.native.debug=true" }
46+
47+
test_script:
48+
- mvn test --batch-mode "-Dant.os.arch=$env:ANT_ARCH"
49+
50+
artifacts:
51+
- path: target\*.jar
52+
name: Java archive (JAR)
53+
- path: '**\*.dll'
54+
name: Native library
55+
56+
cache:
57+
- C:\maven
58+
- C:\Users\appveyor\.m2

build.xml

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Copyright (c) 2016 Kristian Kraljic
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
-->
24+
25+
<project name="system-hook" basedir="." default="compile">
26+
<property environment="env" />
27+
28+
<property name="ant.dir.native" value="${basedir}/src/main/native" />
29+
<property name="ant.dir.objects" value="${project.build.directory}/objects" />
30+
<property name="ant.dir.library" value="${project.build.directory}/classes/lc/kra/system/lib" />
31+
32+
<target name="initialize" description="Initialize local build environment.">
33+
<echo>Initializing local environment...</echo>
34+
35+
<!-- Ant -->
36+
<property name="settings.localRepository" value="${user.home}/.m2/repository" />
37+
<taskdef name="cc" classname="net.sf.antcontrib.cpptasks.CCTask">
38+
<classpath>
39+
<fileset dir="${settings.localRepository}">
40+
<include name="ant-contrib/cpptasks/1.0b5/cpptasks-1.0b5.jar"/>
41+
</fileset>
42+
</classpath>
43+
</taskdef>
44+
45+
<!-- JNI includes -->
46+
<condition property="ant.jni.include" value="${env.JAVA_HOME}/include">
47+
<available file="${env.JAVA_HOME}/include" type="dir" />
48+
</condition>
49+
<condition property="ant.jni.include" value="${java.home}/include">
50+
<available file="${java.home}/include" type="dir" />
51+
</condition>
52+
53+
<fail message="Could not find ant.jni.include folder. Please set your JAVA_HOME environment variable to the root of your JDK directory or manually set the ant.jni.include to the location of the Java JNI header files.">
54+
<condition>
55+
<not>
56+
<or>
57+
<isset property="ant.jni.include" />
58+
<available file="${ant.jni.include}" type="dir" />
59+
</or>
60+
</not>
61+
</condition>
62+
</fail>
63+
64+
<!-- OS & OS Arch -->
65+
<condition property="ant.os" value="windows">
66+
<or>
67+
<os family="winnt" />
68+
<os family="windows" />
69+
</or>
70+
</condition>
71+
<condition property="ant.os.windows" value="true">
72+
<equals arg1="${ant.os}" arg2="windows" />
73+
</condition>
74+
75+
<condition property="ant.os.arch" value="x86">
76+
<or>
77+
<os arch="x86" />
78+
<os arch="i386" />
79+
<os arch="i486" />
80+
<os arch="i586" />
81+
<os arch="i686" />
82+
</or>
83+
</condition>
84+
<condition property="ant.os.arch" value="amd64">
85+
<or>
86+
<os arch="amd64" />
87+
<os arch="x86-64" />
88+
<os arch="x86_64" />
89+
<os arch="k8" />
90+
</or>
91+
</condition>
92+
<property name="ant.os.arch" value="${os.arch}" />
93+
<condition property="os.arch.x86" value="true">
94+
<equals arg1="${os.arch}" arg2="x86" />
95+
</condition>
96+
<condition property="os.arch.amd64" value="true">
97+
<equals arg1="${os.arch}" arg2="amd64" />
98+
</condition>
99+
100+
<!-- Native Build -->
101+
<property name="ant.native.compiler" value="gcc" />
102+
<property name="ant.native.linker" value="${ant.native.compiler}" />
103+
</target>
104+
105+
<target name="compile" depends="initialize" description="Compiles JNI source files.">
106+
<fail message="Property ant.native.library not set. Please set ant.native.library to 'keyboard' or 'mouse'.">
107+
<condition>
108+
<not>
109+
<isset property="ant.native.library" />
110+
</not>
111+
</condition>
112+
</fail>
113+
<property name="ant.native.library.result" value="${ant.native.library}hook-${ant.os}-${ant.os.arch}.dll" />
114+
115+
<cc objdir="${ant.dir.objects}" outfile="${ant.dir.library}/${ant.native.library.result}" subsystem="console" debug="${ant.native.debug}">
116+
<compiler name="${ant.native.compiler}" />
117+
<compilerarg value="-std=c99" />
118+
119+
<compilerarg value="-O0" unless="${ant.native.debug}" />
120+
<compilerarg value="-O2" if="${ant.native.debug}" />
121+
<compilerarg value="-g3" unless="${ant.native.debug}" />
122+
123+
<compilerarg value="-Wall" />
124+
<compilerarg value="-Wextra" />
125+
<compilerarg value="-Wno-unused-parameter" />
126+
127+
<compilerarg value="-D_JNI_IMPLEMENTATION_" />
128+
129+
<includepath path="${ant.jni.include}" />
130+
<includepath path="${ant.jni.include}/win32" if="ant.os.windows" />
131+
132+
<defineset>
133+
<define name="DEBUG" if="ant.native.debug" />
134+
</defineset>
135+
136+
<linker name="${ant.native.linker}" />
137+
<linkerarg value="-shared" />
138+
<linkerarg value="-static" if="ant.os.windows" />
139+
140+
<fileset dir="${ant.dir.native}/${ant.native.library}/${ant.os}">
141+
<include name="**/*.c" />
142+
</fileset>
143+
</cc>
144+
</target>
145+
</project>

0 commit comments

Comments
 (0)