用c的jintellitype-1.3.9包设置全局键盘监听,可是被监听到的按键就不能用了,怎么办

qq_39790161 2017-08-28 02:47:56

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;

/**
* 利用JIntellitype实现全局热键设置
*
* @author Jeby Sun
*
*/
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;
public static final int EXIT_KEY_X = 3;
public static final int EXIT_KEY_Z = 4;

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);


// 第一步:注册热键,第一个参数表示该热键的标识,第二个参数表示组合键,如果没有则为0,第三个参数为定义的主要热键
JIntellitype.getInstance().registerHotKey(FUNC_KEY_MARK, JIntellitype.MOD_ALT, (int) 'S');
JIntellitype.getInstance().registerHotKey(EXIT_KEY_MARK, JIntellitype.MOD_ALT, (int) 'Q');
JIntellitype.getInstance().registerHotKey(3, 0, (int) 'Z');
// JIntellitype.getInstance()
// 第二步:添加热键监听器
JIntellitype.getInstance().addHotKeyListener(new HotkeyListener() {

@Override
public void onHotKey(int markCode) {
System.out.println((char)markCode);
switch (markCode) {
case FUNC_KEY_MARK:
showMessage();


break;
case EXIT_KEY_MARK:
System.exit(0);
break;
case 3:
System.out.print('z');
break;
}
}
});

this.setVisible(true);
}

public void showMessage() {
JOptionPane.showMessageDialog(null, "就算把窗口最小化,按快捷键Alt+S也可以弹出提示框哦!", "弹出框标题", JOptionPane.INFORMATION_MESSAGE);
}

public static void main(String[] args) {
new GlobleHotKeyDemo();
}
}




注册过热键后,可以进行全局监听,可是监听到的热键在焦点窗口就不能用了,没有注册过的按键还能正常使用
...全文
212 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
奥斯卡连裆裤 2018-11-10
  • 打赏
  • 举报
回复
触发监听之后解除注册按键,然后模拟按键,再注册按键。
flying_1314 2017-09-08
  • 打赏
  • 举报
回复
楼主解决了吗?

58,452

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧