通过JIntellitype给Java程序注册系统热键

参考:https://blog.csdn.net/iteye_11213/article/details/81886200

JIntellitype提供了简单的调用方法去注册系统热键。操作简单,示例明确,下载后阅读一下readme.txt文档,就可以使用了。

使用时首先要定义一个实现了HotkeyListener接口的类,实现其onHotKey(int)方法,这个方法接收一个数字,作为标志

然后就可以使用JIntellitype.getInstance().registerHotKey(int,int,int)方法注册需要的热键了,其中第一个参数将是触发时传递个onHotKey方法的参数,所以两者要保持统一,第二个参数为ctrl、alt、shift等的组合结果,第三个一般为组合键的字母。
示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.iflysse.swing;
import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;
/**
* @author ZYWANG
*/
public class HotkeyTest
{
public static void main(String[] args) throws InterruptedException
{
//注册一个ctrl+alt+a的热键
JIntellitype.getInstance().registerHotKey(100, JIntellitype.MOD_CONTROL+JIntellitype.MOD_ALT, (int)'T');
//添加热键监听器
JIntellitype.getInstance().addHotKeyListener(new HotkeyListener()
{
@Override
public void onHotKey(int arg0)
{
System.out.println(arg0);//打印参数
//JIntellitype.getInstance().unregisterHotKey(arg0);//用于移除热键注册的方法
System.exit(0);
}
});
Thread.sleep(10000000);
}
}

JIntellitype下载地址:http://melloware.com/download/ (打开页面,找到JIntellitype)
下载地址:https://code.google.com/archive/p/jintellitype/downloads,注意需要科学上网。
附件中提供了一个jintellitype-1.3.4-dist版本备用