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);

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

JIntellitype使用总结

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

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

如何在你的java程序中注册系统级热键

所谓系统级热键就是指一组快捷键,不论当前系统焦点在哪个程序中,只要按下该键,程序就能够捕捉该事件并进行相关处理。该功能在应用程序中是非常有用的,比如系统自带的 “win+L”自动锁屏,QQ中默认...
  • ycb1689
  • ycb1689
  • 2012年07月11日 18:42
  • 807

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

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

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

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

如何在你的java程序中注册系统级热键

所谓系统级热键就是指一组快捷键,不论当前系统焦点在哪个程序中,只要按下该键,程序就能够捕捉该事件并进行相关处理。该功能在应用程序中是非常有用的,比如系统自带的  “win+L”自动锁屏,QQ中默认的...

解决java程序中注册系统级热键

  • 2015年02月04日 16:35
  • 23KB
  • 下载

使用VB在应用程序中注册热键

'窗体中Option ExplicitPrivate Sub Form_Load()Dim ret As Long    '记录原来的window程序地址    preWinProc = GetWin...

MFC&CPlus: 在MFC中给程序注册热键

本文内容 【本文源于一个实际项目应用,写于2009年,因网上有太多地址在引用本文,故重新贴出,供学习交流。】 给程序注册一个热键相当简单,只需要调用Windows的API RegisterHotK...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:JIntellitype一个在Java程序中注册热键的库
举报原因:
原因补充:

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