The Java Native API does not support setting global hotkeys for Applications. To implement a global hotkey, it needs to be implemented in JNI mode, which involves writing C + + code, which is difficult for most javaer who are not familiar with C + +. But fortunately, someone abroad has been implemented, released into a third-party Java package, by which we can easily set the global hotkey without writing any C + + code.
Jintellitype website seems to be currently inaccessible, here to provide download: http://www.jb51.net/softs/217788.html.
The jintellitype consists of two parts, one is Java Jintellityp jar file, the other is a compiled DLL file written by C/s + +, with two DLL files, 32-bit and 64-bit systems respectively. In the process of using jintellitype, I put the Jintellitype jar file into the project, I do not know where the DLL file, I tried to run, according to the error prompts, The DLL file needs to be placed under the project Com.melloware.jintellitype package. It is recommended that you add two DLL files at the same time, so your program can be compatible with both 32-bit and 64-bit systems, and you don't need any extra processing.
Put on my Little 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; /** * Implement global Hotkey settings using Jintellitype * @author jeby Sun * */public class Globlehotkeydemo extends JFrame {private stat
IC final long serialversionuid = 1L;
Defines a hotkey identifier that distinguishes the user-pressed hotkey public static final int func_key_mark = 1 in event handling when setting multiple hotkeys;
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 the button margin 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 One: Register the hotkey, the first parameter represents the identification of the hotkey, the second parameter represents the key combination, if not 0, the third parameter is the defined primary hotkey jintellitype.getinstance (). RegisterHotKey (Func_key_
MARK, Jintellitype.mod_alt, (int) ' S ');
Jintellitype.getinstance (). RegisterHotKey (Exit_key_mark, Jintellitype.mod_alt, (int) ' Q '); Step Two: Add Hotkey listener jintellitype.getinstance (). Addhotkeylistener (New Hotkeylistener () {@Override Pub LIC void Onhotkey (intMarkcode) {switch (markcode) {case func_key_mark:showmessage ();
Break
Case EXIT_KEY_MARK:System.exit (0);
Break
}
}
});
This.setvisible (TRUE); The public void ShowMessage () {Joptionpane.showmessagedialog (null, "Even if the window is minimized, press the shortcut key Alt+s can also pop-up the cue box Oh!)
"," pop-up title ", Joptionpane.information_message);
public static void Main (string[] args) {new Globlehotkeydemo ();
}
}
In fact, the use of Jintellitype is very simple, 3 steps:
The first step: Add jar packages and DLL files;
The second step: registration hotkey;
The third step: Add Hotkey Listener, implement interface method;