1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| 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;
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);
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(); } }
|