The Java native API does not support setting global hotkeys for applications. To implement global hotkeys, you need to use JNI, which involves writing C/C++ code, which is a bit difficult for most javaers who are not familiar with C/C++. Fortunately, someone abroad has already implemented it and published it as a third-party java package. With this, we can easily set global hotkeys without writing any C/C++ code.
It seems that the official website of jintellitype is currently inaccessible. Download is available here: //www.VeVB.COm/softs/217788.html.
Jintellitype consists of two parts, one is the jintellityp jar file written in Java, and the other is the compiled dll file written in C/C++. There are two dll files, one for 32-bit and 64-bit systems. In the process of using jintellitype, after building the jintellitype jar file into the project, I didn't know where to put the dll file. I tried to run it. According to the error message, it turned out that the dll file needed to be placed under the project com.melloware.jintellitype package. It is recommended to add both dll files at the same time, so that your program can be compatible with both 32-bit and 64-bit systems, and you do not need any additional processing.
Paste my small demo code:
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; /** * Use JIntellitype to implement global hotkey settings* @author Jeby Sun * */ public class GlobleHotKeyDemo extends JFrame { private static final long serialVersionUID = 1L; //Define hotkey identification for When setting multiple hotkeys, distinguish the hotkeys pressed by the user in event handling public static final int FUNC_KEY_MARK = 1; public static final int EXIT_KEY_MARK = 0; JButton msgBtn; JButton exitBtn; public GlobleHotKeyDemo() { this.setTitle("Global hotkey settings"); this.setBounds(100, 100, 600, 400); this.setLayout(null); this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); msgBtn = new JButton("Pop-up box (Alt+S)"); //Set button margins 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("Exit (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); //Step 1: Register the hotkey. The first parameter represents the identification of the hotkey. The second parameter represents the key combination. If not, it is 0. The third parameter is the main hotkey defined. JIntellitype.getInstance() .registerHotKey(FUNC_KEY_MARK, JIntellitype.MOD_ALT, (int)'S'); JIntellitype.getInstance().registerHotKey(EXIT_KEY_MARK, JIntellitype.MOD_ALT, (int)'Q'); //Step 2: Add a hotkey listener 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, "Even if you minimize the window, press the Alt+S shortcut key to pop up the prompt box! ", "Pop-up box title", JOptionPane.INFORMATION_MESSAGE); } public static void main(String[] args) { new GlobleHotKeyDemo(); } }
In fact, using jintellitype is very simple, just 3 steps:
Step 1: Add jar packages and dll files;
Step 2: Register hot keys;
Step 3: Add a hotkey listener and implement the interface method;