Java原生API并不支持為應(yīng)用程序設(shè)置全局熱鍵。要實(shí)現(xiàn)全局熱鍵,需要用JNI方式實(shí)現(xiàn),這就涉及到編寫C/C++代碼,這對于大多數(shù)不熟悉C/C++的javaer來說,有點(diǎn)困難。不過幸好,國外有人已經(jīng)實(shí)現(xiàn)了,發(fā)布成第三方j(luò)ava包,借此,我們可以很方便的設(shè)置全局熱鍵而不用編寫任何C/C++代碼。
jintellitype官網(wǎng)貌似目前訪問不到,這里提供下載:http://pan.baidu.com/s/1kTIDxgN。
我實(shí)現(xiàn)的演示Demo源碼下載:http://pan.baidu.com/s/1c0nJ8OC。
jintellitype由兩部分組成,一部分是java寫的jintellityp的jar文件,另一部分是C/C++寫的已編譯好的dll文件,有兩個(gè)dll文件,分別是32位和64位系統(tǒng)的。在我使用jintellitype的過程中,把jintellitype的jar文件Build進(jìn)項(xiàng)目后,不知道dll文件放哪,我試著運(yùn)行,根據(jù)錯(cuò)誤提示,原來需要把dll文件放到項(xiàng)目com.melloware.jintellitype包下。建議同時(shí)把兩個(gè)dll文件都加進(jìn)去,這樣,你的程序就可以同時(shí)兼容32位和64位系統(tǒng),而你不需要任何額外的處理。
貼上我的小demo代碼:
1 package com.jebysun.globlehotkey; 2 3 import java.awt.Insets; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 7 import javax.swing.JButton; 8 import javax.swing.JFrame; 9 import javax.swing.JOptionPane;10 11 import com.melloware.jintellitype.HotkeyListener;12 import com.melloware.jintellitype.JIntellitype;13 14 /**15 * 利用JIntellitype實(shí)現(xiàn)全局熱鍵設(shè)置16 * @author Jeby Sun17 *18 */19 public class GlobleHotKeyDemo extends JFrame {20 21 PRivate static final long serialVersionUID = 1L;22 23 //定義熱鍵標(biāo)識,用于在設(shè)置多個(gè)熱鍵時(shí),在事件處理中區(qū)分用戶按下的熱鍵24 public static final int FUNC_KEY_MARK = 1;25 public static final int EXIT_KEY_MARK = 0;26 27 JButton msgBtn;28 JButton exitBtn;29 30 public GlobleHotKeyDemo() {31 this.setTitle("全局熱鍵設(shè)置");32 this.setBounds(100, 100, 600, 400);33 this.setLayout(null);34 this.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);35 36 msgBtn = new JButton("彈出框(Alt+S)");37 //設(shè)置按鈕邊距38 msgBtn.setMargin(new Insets(0,0,0,0));39 msgBtn.setFocusable(false);40 msgBtn.setBounds(20, 20, 120, 30);41 msgBtn.addActionListener(new ActionListener() {42 @Override43 public void actionPerformed(ActionEvent e) {44 showMessage();45 }46 });47 this.add(msgBtn);48 49 exitBtn = new JButton("退出(Alt+Q)");50 exitBtn.setMargin(new Insets(0,0,0,0));51 exitBtn.setFocusable(false);52 exitBtn.setBounds(160, 20, 120, 30);53 exitBtn.addActionListener(new ActionListener() {54 @Override55 public void actionPerformed(ActionEvent e) {56 System.exit(0);57 }58 });59 this.add(exitBtn);60 61 //第一步:注冊熱鍵,第一個(gè)參數(shù)表示該熱鍵的標(biāo)識,第二個(gè)參數(shù)表示組合鍵,如果沒有則為0,第三個(gè)參數(shù)為定義的主要熱鍵62 JIntellitype.getInstance().registerHotKey(FUNC_KEY_MARK, JIntellitype.MOD_ALT, (int)'S'); 63 JIntellitype.getInstance().registerHotKey(EXIT_KEY_MARK, JIntellitype.MOD_ALT, (int)'Q'); 64 65 //第二步:添加熱鍵監(jiān)聽器66 JIntellitype.getInstance().addHotKeyListener(new HotkeyListener() {67 68 @Override69 public void onHotKey(int markCode) {70 switch (markCode) { 71 case FUNC_KEY_MARK: 72 showMessage();73 break; 74 case EXIT_KEY_MARK: 75 System.exit(0);76 break; 77 } 78 }79 }); 80 81 this.setVisible(true);82 }83 84 public void showMessage() {85 JOptionPane.showMessageDialog(null, "就算把窗口最小化,按快捷鍵Alt+S也可以彈出提示框哦!", "彈出框標(biāo)題", JOptionPane.INFORMATION_MESSAGE);86 }87 88 89 90 public static void main(String[] args) {91 new GlobleHotKeyDemo();92 }93 }其實(shí),jintellitype的使用非常簡單,就3個(gè)步驟:
第一步:添加jar包和dll文件;
第二步:注冊熱鍵;
第三步:添加熱鍵監(jiān)聽器,實(shí)現(xiàn)接口的方法;
新聞熱點(diǎn)
疑難解答
圖片精選