使用 jintellitype 库在 Java 程序中实现监听 Windows 全局快捷键(热键)

文章目录

一、问题背景

有时候我们在开发 Java 程序的时候,需要监听全局快捷键或热键,例如监听 ctrl+ alt + Q 全局快捷键被按下的事件,随后做一些特定的业务。因此,我们可以使用 melloware/jintellitype 库实现快捷键的监听,官方仓库为:https://github.com/melloware/jintellitype

jintellitype 库是一个基于 JNI 实现的在 Windows 下监听全局快捷键组合的开源库,其具有 API 简单直观,使用便捷的特点,极其方便在 Java 开发中使用。

二、依赖导入

首先在项目中的 build.gradle 文件中导入 jintellitype 依赖。

groovy 复制代码
dependencies {
    // jintellitype
    implementation "com.melloware:jintellitype:1.5.0"
}

三、使用实例

1. 注册热键监听

我们使用 JIntellitype.getInstance().addHotKeyListener() 方法注册一个热键监听器,需要传递一个 HotkeyListener 对象,其只有一个 onHotKey 方法,传递 identifier 参数,此参数代表着我们向 JIntellitype 注册的热键事件的唯一标识符。HotkeyListener源码如下:

java 复制代码
public interface HotkeyListener {
	/**
	 * Event fired when a WM_HOTKEY message is received that was initiated
	 * by this application.
	 * <p>
	 * @param identifier the unique Identifier the Hotkey was assigned
	 */
	void onHotKey( int identifier );
}

JIntellitype 检测到我们注册的按键事件的时候,回调此方法。一个标准的代码如下:

java 复制代码
JIntellitype.getInstance().addHotKeyListener(keyMark -> {
          switch (keyMark) {
          }
      });

此时可以在 switch 添加我们注册的按键事件,以处理不同的业务。

2. 注册热键事件

我们需要向 JIntellitype 注册我们需要监听的按键事件,可以调用 JIntellitype.getInstance().registerHotKey() 方法进行注册,此方法有两个签名:

java 复制代码
/**
 * Registers a Hotkey with windows. This combination will be responded to by
 * all registered HotKeyListeners. Uses the JIntellitypeConstants for MOD,
 * ALT, CTRL, and WINDOWS keys.
 * <p>
 * @param identifier a unique identifier for this key combination
 * @param modifier MOD_SHIFT, MOD_ALT, MOD_CONTROL, MOD_WIN from
 *           JIntellitypeConstants, or 0 if no modifier needed
 * @param keycode the key to respond to in Ascii integer, 65 for A
 */
public void registerHotKey(int identifier, int modifier, int keycode) { }

/**
 * Registers a Hotkey with windows. This combination will be responded to by
 * all registered HotKeyListeners. Use the identifiers CTRL, SHIFT, ALT
 * and/or WIN.
 * <p>
 * @param identifier a unique identifier for this key combination
 * @param modifierAndKeyCode String with modifiers separated by + and keycode
 *           (e.g. CTRL+SHIFT+A)
 * @see #registerHotKey(int, int, int)
 * @see #registerSwingHotKey(int, int, int)
 */
public void registerHotKey(int identifier, String modifierAndKeyCode) { }

两个方法的第一个参数为 identifier,为热键事件的唯一标识符,如果有对应的事件被按下,则在 JIntellitype.getInstance().addHotKeyListener() 注册的监听者中,回调 onHotKey 方法时被传递。


public void registerHotKey(int identifier, int modifier, int keycode) 签名中,

第二个参数 modifier 为特殊控制键,例如 MOD_SHIFT 代表 shift 键, MOD_ALT 代表 alt 键, MOD_CONTROL 代表 ctrl 键, MOD_WIN 代表 win 键,如果需要传递两个功能键,则可以使用按位与运算,

而第三个参数为字符键的 ASIIC 码。例如 ctrl + E,代码为:

java 复制代码
JIntellitype.getInstance().registerHotKey(0x123, MOD_CONTROL, 'E')

public void registerHotKey(int identifier, String modifierAndKeyCode) 签名中,第二个参数 modifierAndKeyCode 为多个组合键的按键事件,其格式为 特殊控制键+字符键,且可以有多个控制键,只能有一个字符键。

例如,传递字符串 CTRL+SHIFT+A,则表示按下 ctrl 键,shift 键 和 A 键:

java 复制代码
JIntellitype.getInstance().registerHotKey(0x123, "CTRL+SHIFT+A")

四个特殊键的字符串分别为:
ctrl 键: CTRL
alt 键:ALT
shift 键:SHIFT
win 键:WIN

相关推荐
本贾尼24 分钟前
csv文件用Excel打开后出现乱码的问题及其解决方法
windows·excel
边洛洛33 分钟前
next.js项目部署流程
开发语言·前端·javascript
爱幻想-hjyp40 分钟前
【Python】uv包管理器常用命令
开发语言·python·uv
哈皮Superman1 小时前
【Research】MagicFuzzer: Scalable deadlock detection for large-scale applications
java·开发语言·数据库
lly2024061 小时前
NoSQL 简介
开发语言
千里马-horse1 小时前
Boost.Iostreams 简介
开发语言·c++·boost
yi碗汤园1 小时前
【一文了解】八大排序-冒泡排序、选择排序
开发语言·前端·算法·unity·c#·1024程序员节
是苏浙1 小时前
零基础入门C语言之深入了解指针3
c语言·开发语言
陌路201 小时前
C17值类别概念
开发语言·c++