软件编程
位置:首页>> 软件编程>> java编程>> 第三方包jintellitype实现Java设置全局热键

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

作者:hebedich  发布时间:2023-09-25 10:33:50 

标签: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;

/**
* 利用JIntellitype实现全局热键设置
* @author Jeby Sun
*
*/
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文件;

第二步:注册热键;

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

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com