JIntellitype一个在Java程序中注册热键的库

原创 2014年12月12日 17:54:51

项目地址:https://code.google.com/p/jintellitype/

使用Java做桌面程序不能注册系统级热键,最多只能为JFrame或者JDialog添加键盘监听,而为JFrame或JDialog添加键盘监听的方式最多只能是当该Frame或Dialog显示时能接收到键盘的监听,而不能实现像QQ的快捷键方式呼出聊天窗口,毕竟Java不是万能的在人家的地盘还是依赖于操作系统底层的实现。JIntellitype使用JNI的方式解决了在Windows平台下注册系统级热键的问题。以下是该开源项目官网上的介绍:

JIntellitype is a Java API for interacting with Microsoft Intellitype commands as well as registering for Global Hotkeys in your Java application. The API is a Java JNI library that uses a C++ DLL to do all the communication with Windows. NOTE: This library ONLY works on Windows. If you are looking for a Linux version please see JxGrabKey project on Sourceforge. If you are looking for a Mac OSX version see OSSupport on GitHub.

Have you ever wanted to have CTRL+SHIFT+G maximize your Swing application on the desktop? Even if that application did not have focus? Well, now you can! By registering a Windows Hotkey combination, your application will be alerted when the combination you select is pressed anywhere in Windows. Windows has the API call RegisterHotKey for registering a global hotkey combination so that your app receives that message no matter what else you are doing or even if your application has focus. This is a commonly requested feature in Java that has now been implemented by JIntellitype. Check out the Quick Start Guide below for an example on how to use JIntellitype.

Have you ever wanted your Java application to react to those special Play, Pause, Stop keys on some newer keyboards like Winamp and Windows Media Player do? Ever wonder how they do it? If you want your application to "listen" for those special keys, now you can withJIntellitype! Just register an IntellitypeListener and you will be notified when those messages are received. To read more about these special commands see the MSDN Documentation about the Intellitype commands. Check out the Quick Start Guide for an example on how to useJIntellitype.

例子程序:

// Initialize JIntellitype
...
JIntellitype.getInstance();
...
// OPTIONAL: check to see if an instance of this application is already
// running, use the name of the window title of this JFrame for checking
if (JIntellitype.checkInstanceAlreadyRunning("MyApp")) {
   LOG.error("An instance of this application is already running");
   System.exit(1);
}

// Assign global hotkeys to Windows+A and ALT+SHIFT+B
JIntellitype.getInstance().registerHotKey(1, JIntellitype.MOD_WIN, (int)'A');
JIntellitype.getInstance().registerHotKey(2, JIntellitype.MOD_ALT + JIntellitype.MOD_SHIFT, (int)'B');

//or you can use the Swing constants instead
JIntellitype.getInstance().registerSwingHotKey(3, Event.CTRL_MASK + Event.SHIFT_MASK, (int)'C');
        
// To unregister them just call unregisterHotKey with the unique identifier
JIntellitype.getInstance().unregisterHotKey(1);
JIntellitype.getInstance().unregisterHotKey(2);
JIntellitype.getInstance().unregisterHotKey(3);

//assign this class to be a HotKeyListener
JIntellitype.getInstance().addHotKeyListener(this);
        
// listen for hotkey
public void onHotKey(int aIdentifier) {
    if (aIdentifier == 1)
        System.out.println("WINDOWS+A hotkey pressed");
    }
}

//assign this class to be a IntellitypeListener
JIntellitype.getInstance().addIntellitypeListener(this);
        
// listen for intellitype play/pause command
public void onIntellitype(int aCommand) {
      switch (aCommand) {
        case JIntellitype.APPCOMMAND_MEDIA_PLAY_PAUSE:
           System.out.println("Play/Pause message received " + Integer.toString(aCommand));
           break;
        default:
           System.out.println("Undefined INTELLITYPE message caught " + Integer.toString(aCommand));
           break;
      }
}

// Termination, make sure to call before exiting
...
JIntellitype.getInstance().cleanUp();
System.exit(0);

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a78460750/article/details/41897077

【系列课】Springmvc4+Mybatis3+Spring4+Bootstrap3之注册

【技术架构】 把手教你整合最优雅SSM4框架: 当我们看大部分教学课堂中把SSH作为最核心教学内容。但实际互联网企业应用中,SpringMVC可以完全替代Struts系列,注解方式,通过restful风格,再配上企业实战架构SpringMVC4 , Spring 4, MyBatis3,JQuery,JQueryUI,Echarts3,Bootstrap3,Ajax堪称完美。
  • 2017年05月02日 22:59

JIntellitype使用总结

简单介绍:     因为要在java项目中注册全局热键(程序失去焦点,依然可以响应按键),而且自己本身对c的了解并不是特别深,所以无法用jni调用c程序,只好使用了JIntellitype第三方包。...
  • jjchack
  • jjchack
  • 2015-03-18 20:49:18
  • 1716

Java设置全局热键钩子——第三方包jintellitype实现

Java原生API并不支持为应用程序设置全局热键。要实现全局热键,需要用JNI方式实现,这就涉及到编写C/C++代码,这对于大多数不熟悉C/C++的javaer来说,有点困难。不过幸好,国外有人已经实...
  • lipei1220
  • lipei1220
  • 2015-09-18 21:27:16
  • 2376

程序员修炼之路(六)编写自己的工具(JAVA注册系统级快捷键源码>jintellitype<)

这俩天在公司抽空写了一个JAVA的小工具。因为现在在公司做的是测试任务,所以经常用到“ALT+PrtScn”(截图)和“CTRL+V”(粘贴)这两个组合快捷键。 因为这俩个组合键实在是远了,一个手不...
  • lanzhizhuxia
  • lanzhizhuxia
  • 2012-08-23 15:01:23
  • 4335

jintellitype

  • 2010年12月29日 22:34
  • 202KB
  • 下载

Install4j打包用的jre集成JIntellitype.dll

  • 2014年01月07日 19:06
  • 23.17MB
  • 下载

给键盘添加全局监听需要用到的包jintellitype-1.3.9.rar

  • 2017年12月28日 14:08
  • 185KB
  • 下载

jintellitype-1.3.9

  • 2014年08月13日 17:27
  • 202KB
  • 下载

系统托盘需要的导入包 jintellitype-1.3.6.jar

  • 2011年07月27日 22:48
  • 16KB
  • 下载

java全局快捷键

IntelliType是微软的键盘驱动,或者说这是微软为自己出品的键盘系列产品开发的配置工具,主要作用就是让键盘使用更加自定义化合个性化。 通过它重新配置键盘键盘分布。比如常用但是按起来不是很方...
  • lxqq0000
  • lxqq0000
  • 2014-05-06 18:26:32
  • 774
收藏助手
不良信息举报
您举报文章:JIntellitype一个在Java程序中注册热键的库
举报原因:
原因补充:

(最多只允许输入30个字)