栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

Java原生API并不支持为应用程序设置全局热键。要实现全局热键,需要用JNI方式实现,这就涉及到编写C/C++代码,这对于大多数不熟悉C/C++的javaer来说,有点困难。不过幸好,国外有人已经实现了,发布成第三方java包,借此,我们可以很方便的设置全局热键而不用编写任何C/C++代码。

  jintellitype官网貌似目前访问不到,这里提供下载:https://www.jb51.net/softs/217788.html。

  jintellitype由两部分组成,一部分是java写的jintellityp的jar文件,另一部分是C/C++写的已编译好的dll文件,有两个dll文件,分别是32位和64位系统的。在我使用jintellitype的过程中,把jintellitype的jar文件Build进项目后,不知道dll文件放哪,我试着运行,根据错误提示,原来需要把dll文件放到项目com.melloware.jintellitype包下。建议同时把两个dll文件都加进去,这样,你的程序就可以同时兼容32位和64位系统,而你不需要任何额外的处理。

  贴上我的小demo代码:

 package com.jebysun.globlehotkey;
 
 import java.awt.Insets;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 
 import javax.swing.JButton;
 import javax.swing.Jframe;
 import javax.swing.JOptionPane;
 
 import com.melloware.jintellitype.HotkeyListener;
 import com.melloware.jintellitype.JIntellitype;
 
 
 public class GlobleHotKeyDemo extends Jframe {
 
   private static final long serialVersionUID = 1L;
   
   //定义热键标识,用于在设置多个热键时,在事件处理中区分用户按下的热键
   public static final int FUNC_KEY_MARK = 1;
   public static final int EXIT_KEY_MARK = 0;
   
   JButton msgBtn;
   JButton exitBtn;
 
   public GlobleHotKeyDemo() {
     this.setTitle("全局热键设置");
     this.setBounds(100, 100, 600, 400);
     this.setLayout(null);
     this.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
     
     msgBtn = new JButton("弹出框(Alt+S)");
     //设置按钮边距
     msgBtn.setMargin(new Insets(0,0,0,0));
     msgBtn.setFocusable(false);
     msgBtn.setBounds(20, 20, 120, 30);
     msgBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
  showMessage();
}
     });
     this.add(msgBtn);
     
     exitBtn = new JButton("退出(Alt+Q)");
     exitBtn.setMargin(new Insets(0,0,0,0));
     exitBtn.setFocusable(false);
     exitBtn.setBounds(160, 20, 120, 30);
     exitBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
  System.exit(0);
}
     });
     this.add(exitBtn);
     
     //第一步:注册热键,第一个参数表示该热键的标识,第二个参数表示组合键,如果没有则为0,第三个参数为定义的主要热键
     JIntellitype.getInstance().registerHotKey(FUNC_KEY_MARK, JIntellitype.MOD_ALT, (int)'S'); 
     JIntellitype.getInstance().registerHotKey(EXIT_KEY_MARK, JIntellitype.MOD_ALT, (int)'Q'); 
     
     //第二步:添加热键监听器
     JIntellitype.getInstance().addHotKeyListener(new HotkeyListener() {

@Override
public void onHotKey(int markCode) {
  switch (markCode) { 
  case FUNC_KEY_MARK: 
    showMessage();
    break; 
  case EXIT_KEY_MARK: 
    System.exit(0);
    break;  
  }  
}
     }); 
     
     this.setVisible(true);
   }
   
   public void showMessage() {
     JOptionPane.showMessageDialog(null, "就算把窗口最小化,按快捷键Alt+S也可以弹出提示框哦!", "弹出框标题", JOptionPane.INFORMATION_MESSAGE);
   }
   
   public static void main(String[] args) {
     new GlobleHotKeyDemo();
   }
 }

其实,jintellitype的使用非常简单,就3个步骤:

第一步:添加jar包和dll文件;

第二步:注册热键;

第三步:添加热键监听器,实现接口的方法;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/152058.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号