The Java Native API does not support setting global hotkeys for Applications. To implement a global hotkey, it needs to be implemented in a JNI manner, which involves writing C/C + + code, which is a bit difficult for most javaer who are unfamiliar with C + +. Fortunately, someone from abroad has already implemented it and published it as a third-party Java package, so we can easily set a global hotkey without writing any C + + code.
Jintellitype official website seems to be not available at present, here to download: Http://pan.baidu.com/s/1kTIDxgN.
I realized the demo source download: Http://pan.baidu.com/s/1c0nJ8OC.
Jintellitype is composed of two parts, part of the Java write Jintellityp jar file, and the other part is a compiled DLL file written in C/s + +, there are two DLL files, respectively, 32-bit and 64-bit system. In my use of the jintellitype process, the Jintellitype jar file build into the project, do not know where the DLL file, I tried to run, according to the error prompt, The DLL file needs to be placed under the project Com.melloware.jintellitype package. It is recommended to add two 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.
Stick to My Little demo code:
1 PackageCom.jebysun.globlehotkey;2 3 Importjava.awt.Insets;4 Importjava.awt.event.ActionEvent;5 ImportJava.awt.event.ActionListener;6 7 ImportJavax.swing.JButton;8 ImportJavax.swing.JFrame;9 ImportJavax.swing.JOptionPane;Ten One ImportCom.melloware.jintellitype.HotkeyListener; A ImportCom.melloware.jintellitype.JIntellitype; - - /** the * Use Jintellitype to implement global hotkey settings - * @authorJeby Sun - * - */ + Public classGloblehotkeydemoextendsJFrame { - + Private Static Final LongSerialversionuid = 1L; A at //defines a hotkey identifier that distinguishes a user-pressed hotkey from event handling when setting multiple Hotkeys - Public Static Final intFunc_key_mark = 1; - Public Static Final intExit_key_mark = 0; - - JButton msgbtn; - JButton exitbtn; in - PublicGloblehotkeydemo () { to This. Settitle ("Global Hotkey Settings"); + This. setbounds (100, 100, 600, 400); - This. setlayout (NULL); the This. Setdefaultcloseoperation (jframe.exit_on_close); * $MSGBTN =NewJButton ("Pop-up Box (alt+s)");Panax Notoginseng //Set button Margins -Msgbtn.setmargin (NewInsets (0,0,0,0)); theMsgbtn.setfocusable (false); +Msgbtn.setbounds (20, 20, 120, 30); AMsgbtn.addactionlistener (NewActionListener () { the @Override + Public voidactionperformed (ActionEvent e) { - showmessage (); $ } $ }); - This. Add (msgbtn); - theEXITBTN =NewJButton ("Exit (ALT+Q)"); -Exitbtn.setmargin (NewInsets (0,0,0,0));WuyiExitbtn.setfocusable (false); theExitbtn.setbounds (160, 20, 120, 30); -Exitbtn.addactionlistener (NewActionListener () { Wu @Override - Public voidactionperformed (ActionEvent e) { AboutSystem.exit (0); $ } - }); - This. Add (exitbtn); - A //The First step: 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 main hotkey defined +Jintellitype.getinstance (). RegisterHotKey (Func_key_mark, Jintellitype.mod_alt, (int) ' S '); theJintellitype.getinstance (). RegisterHotKey (Exit_key_mark, Jintellitype.mod_alt, (int) ' Q '); - $ //Step Two: Add a hotkey listener theJintellitype.getinstance (). Addhotkeylistener (NewHotkeylistener () { the the @Override the Public voidOnhotkey (intMarkcode) { - Switch(markcode) { in CaseFunc_key_mark: the showmessage (); the Break; About CaseExit_key_mark: theSystem.exit (0); the Break; the } + } - }); the Bayi This. setvisible (true); the } the - Public voidShowMessage () { -Joptionpane.showmessagedialog (NULL, "Even if the window is minimized, press the shortcut key Alt+s can also pop up the prompt box Oh!" "," pop-up box title ", joptionpane.information_message); the } the the the - Public Static voidMain (string[] args) { the NewGloblehotkeydemo (); the } the}
In fact, the use of Jintellitype is very simple, on 3 steps:
The first step: Add jar packages and DLL files;
Step Two: Register the hotkey;
The third step: adding a hotkey listener to implement the interface method;
Java set global Hotkey--third party package Jintellitype implementation