注:本文为原创文章,转载时请注明转载地址。 |
所谓系统级热键就是指一组快捷键,不论当前系统焦点在哪个程序中,只要按下该键,程序就能够捕捉该事件并进行相关处理。该功能在应用程序中是非常有用的,比如系统自带的
“win+L”自动锁屏,QQ中默认的“ctrl+alt+Z”自动打开当前的消息窗口等等。
Java中的事件监听机制虽然功能强大,但是当系统焦点脱离该程序时也无能为力。要实现该功能必须调用系统的钩子函数,因此在java中也必须通过jni调用来实现,但是对于不熟悉系统函数或者其他编成语言的朋友来说却是个难题。
以前实现类似的功能都是自己通过c调用系统的钩子函数然后再通过jni调用,自己写的东西只要能满足简单的需求即可,因此功能和程序结构也比较简单。后来在国外的一个网站上发现了一个组件“jintellitype”帮我们封装了绝大部分的功能,而且结构上也采用java中的事件监听机制,我们只要在程序中通过注册即可实现,下面是一个简单的例子:
- import com.melloware.jintellitype.HotkeyListener;
- import com.melloware.jintellitype.JIntellitype;
- public class HotKey implements HotkeyListener {
- static final int KEY_1 = 88;
- static final int KEY_2 = 89;
- static final int KEY_3 = 90;
- /**
- * 该方法负责监听注册的系统热键事件
- *
- * @param key:触发的热键标识
- */
- public void onHotKey(int key) {
- switch (key) {
- case KEY_1:
- System.out.println("ctrl+alt+I 按下.........");
- break;
- case KEY_2:
- System.out.println("ctrl+alt+O 按下.........");
- break;
- case KEY_3:
- System.out.println("系统退出..........");
- destroy();
- }
- }
- /**
- * 解除注册并退出
- */
- void destroy() {
- JIntellitype.getInstance().unregisterHotKey(KEY_1);
- JIntellitype.getInstance().unregisterHotKey(KEY_2);
- JIntellitype.getInstance().unregisterHotKey(KEY_3);
- System.exit(0);
- }
- /**
- * 初始化热键并注册监听事件
- */
- void initHotkey() {
- //参数KEY_1表示改组热键组合的标识,第二个参数表示组合键,如果没有则为0,该热键对应ctrl+alt+I
- JIntellitype.getInstance().registerHotKey(KEY_1, JIntellitype.MOD_CONTROL + JIntellitype.MOD_ALT,
- (int) 'I');
- JIntellitype.getInstance().registerHotKey(KEY_2, JIntellitype.MOD_CONTROL + JIntellitype.MOD_ALT,
- (int) 'O');
- JIntellitype.getInstance().registerHotKey(KEY_3, JIntellitype.MOD_CONTROL + JIntellitype.MOD_ALT,
- (int) 'X');
- JIntellitype.getInstance().addHotKeyListener(this);
- }
- public static void main(String[] args) {
- HotKey key = new HotKey();
- key.initHotkey();
- //下面模拟长时间执行的任务
- while (true) {
- try {
- Thread.sleep(10000);
- } catch (Exception ex) {
- break;
- }
- }
- }
- }
注意:
1. 注册热键时必须是大写的。像
(int) 'I'
2.
因为用到了
JIntellitype,所以遇到如何加载dll的问题。
首先,我的目标是程序只有一个exe文件。研究了一翻exe4j,没发现把dll打进exe里的方法。
退而求次,准备把dll打包到jar中,但当dll打包进jar后,系统无法加载。
解决思路:
1. 获取jar中dll文件内容:class.getResource()
2. 把内容拷贝出来
3. 加载拷贝出来的dll文件
把dll拷贝到哪里,这也是个问题,
先获取
java.library.path ,再把 dll拷贝到此路径下。这种方法不错。
static {
try {
String libpath = System.getProperty("java.library.path");
if ( libpath==null || libpath.length() == 0 ) {
throw new RuntimeException("java.library.path is null");
}
String path = null;
StringTokenizer st = new StringTokenizer(libpath, System.getProperty("path.separator"));
if ( st.hasMoreElements() ) {
path = st.nextToken();
} else {
throw new RuntimeException("can not split library path:" + libpath);
}
InputStream inputStream = Foo.class.getResource("jacob.dll").openStream();
final File dllFile = new File(new File(path), "jacob.dll");
if (!dllFile.exists()) {
FileOutputStream outputStream = new FileOutputStream(dllFile);
byte[] array = new byte[8192];
for (int i = inputStream.read(array); i != -1; i = inputStream.read(array)) {
outputStream.write(array, 0, i);
}
outputStream.close();
}
//dllFile.deleteOnExit();
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run() {
if ( dllFile.exists() ) {
boolean delete = dllFile.delete();
System.out.println("delete : " + delete);
}
}
});
} catch (Throwable e) {
throw new RuntimeException("load jacob.dll error!", e);
}
}
try {
String libpath = System.getProperty("java.library.path");
if ( libpath==null || libpath.length() == 0 ) {
throw new RuntimeException("java.library.path is null");
}
String path = null;
StringTokenizer st = new StringTokenizer(libpath, System.getProperty("path.separator"));
if ( st.hasMoreElements() ) {
path = st.nextToken();
} else {
throw new RuntimeException("can not split library path:" + libpath);
}
InputStream inputStream = Foo.class.getResource("jacob.dll").openStream();
final File dllFile = new File(new File(path), "jacob.dll");
if (!dllFile.exists()) {
FileOutputStream outputStream = new FileOutputStream(dllFile);
byte[] array = new byte[8192];
for (int i = inputStream.read(array); i != -1; i = inputStream.read(array)) {
outputStream.write(array, 0, i);
}
outputStream.close();
}
//dllFile.deleteOnExit();
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run() {
if ( dllFile.exists() ) {
boolean delete = dllFile.delete();
System.out.println("delete : " + delete);
}
}
});
} catch (Throwable e) {
throw new RuntimeException("load jacob.dll error!", e);
}
}
关于不能删除dll文件问题,要先卸载它:
//卸载dll
try {
ClassLoader classLoader = this.getClass().getClassLoader();
Field field = ClassLoader.class.getDeclaredField("nativeLibraries");
field.setAccessible(true);
Vector libs = (Vector) field.get(classLoader);
Iterator it = libs.iterator();
Object o;
while (it.hasNext()) {
o = it.next();
Method finalize = o.getClass().getDeclaredMethod("finalize", new Class[0]);
finalize.setAccessible(true);
finalize.invoke(o, new Object[0]);
}
} catch (Exception ex) {
log.error("卸载dll文件出错,需要重启服务器!", ex);
throw new RuntimeException(ex);
}
经测试,此段代码有问题,没找出问题原因,以后再研究了。。。