Giter VIP home page Giter VIP logo

mousehook's Introduction

Java (low-level) System Hook Build Status

Java (low-level) System Hook provides a very light-weight global keyboard and mouse listener for Java. Generally keyboard and mouse events in Java only work, 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.

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.

Using the GlobalKeyboardHook class a GlobalKeyListener or the adapter class GlobalKeyAdapter can be registered to listen to keyPressed and keyReleased events like so:

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.listKeyboards().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(); }
	}
}

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:

import java.util.Map.Entry;

import lc.kra.system.mouse.GlobalMouseHook;
import lc.kra.system.mouse.event.GlobalMouseAdapter;
import lc.kra.system.mouse.event.GlobalMouseEvent;

public class GlobalMouseExample {
	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 
		GlobalMouseHook mouseHook = new GlobalMouseHook(); // add true to the constructor, to switch to raw input mode

		System.out.println("Global mouse hook successfully started, press [middle] mouse button to shutdown. Connected mice:");
		for(Entry<Long,String> mouse:GlobalMouseHook.listMice().entrySet())
			System.out.format("%d: %s\n", mouse.getKey(), mouse.getValue());
		
		mouseHook.addMouseListener(new GlobalMouseAdapter() {
			@Override public void mousePressed(GlobalMouseEvent event)  {
				System.out.println(event);
				if((event.getButtons()&GlobalMouseEvent.BUTTON_LEFT)!=GlobalMouseEvent.BUTTON_NO
				&& (event.getButtons()&GlobalMouseEvent.BUTTON_RIGHT)!=GlobalMouseEvent.BUTTON_NO)
					System.out.println("Both mouse buttons are currenlty pressed!");
				if(event.getButton()==GlobalMouseEvent.BUTTON_MIDDLE)
					run = false;
			}
			@Override public void mouseReleased(GlobalMouseEvent event)  {
				System.out.println(event); }
			@Override public void mouseMoved(GlobalMouseEvent event) {
				System.out.println(event); }
			@Override public void mouseWheel(GlobalMouseEvent event) {
				System.out.println(event); }
		});
		
		try {
			while(run) Thread.sleep(128);
		} catch(InterruptedException e) { /* nothing to do here */ }
		  finally { mouseHook.shutdownHook(); }
	}
}

Please find some background information about this framework on my blog.

Usage

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 for pre-bundled Java archives (JAR).

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.

Maven Dependency

You can include system-hook from this GitHub repository by adding this dependency to your pom.xml:

<dependency>
  <groupId>lc.kra.system</groupId>
  <artifactId>system-hook</artifactId>
  <version>3.2</version>
</dependency>

Additionally you will have to add the following repository to your pom.xml:

<repositories>
  <repository>
    <id>system-hook-mvn-repo</id>
    <url>https://raw.github.com/kristian/system-hook/mvn-repo/</url>
    <snapshots>
      <enabled>true</enabled>
      <updatePolicy>always</updatePolicy>
    </snapshots>
  </repository>
</repositories>

Build

To build system-hook on your machine, checkout the repository, cd into it, and call:

mvn clean install

(A C99 compatible compiler / linker bundle is required to build the native libraries, see MSYS2)

License

The code is available under the terms of the MIT License.

mousehook's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.