您好我正在使用JNativeHook为应用程序添加全局快捷方式,该应用程序在屏幕的右上角显示通知类型jframe。目前我的代码在Eclipse中运行时程序运行得非常好。但是,在导出到可运行的jar文件后,应用程序崩溃了。经过一些谷歌搜索后,我发现我需要将Jar启动器添加到首选项中安全窗格的辅助功能部分。
但是现在我遇到了一个新问题。我的程序运行但不接受我的全局快捷方式(按下时没有任何反应)。有什么我做错了吗?
以下是我用来添加JNativeHook和keybinds的代码。
主档案:
GlobalScreen.getInstance().addNativeKeyListener(new KeyManager());
的KeyManager:
private String periodName = new String();
@Override
public void nativeKeyPressed(NativeKeyEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getKeyCode() == NativeKeyEvent.VK_5 && NativeInputEvent.getModifiersText(arg0.getModifiers()).equals(
"Ctrl")) {
Calendar setup = Calendar.getInstance();
GregorianCalendar cal = new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), setup.get(Calendar.HOUR_OF_DAY), setup.get(Calendar.MINUTE), 0);
GregorianCalendar[] timesM = {new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 9, 40, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH),10, 30, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 11, 55, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 11, 20, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 12, 15, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 13, 15, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 14, 10, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 15, 05, 0), null};
GregorianCalendar[] timesT = {new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 9, 30, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 10, 20, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 10, 35, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 11, 00, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 11, 50, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 12, 40, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 14, 15, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 15, 05, 0), null};
GregorianCalendar[] timesW = {new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 9, 30, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 10, 20, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 10, 35, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 11, 00, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 11, 50, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 12, 40, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 14, 15, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 15, 05, 0), null};
GregorianCalendar[] timesTh = {new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 9, 30, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 10, 20, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 10, 35, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 11, 00, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 11, 50, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 12, 40, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 14, 15, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 15, 05, 0), null};
GregorianCalendar[] timesF = {new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 9, 35, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 10, 30, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 11, 50, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 11, 15, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 12, 30, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 13, 25, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 14, 15, 0), new GregorianCalendar(setup.get(Calendar.MONTH), setup.get(Calendar.DAY_OF_MONTH), 15, 05, 0), null};
System.out.println("Hour of day" + setup.get(Calendar.HOUR_OF_DAY));
GregorianCalendar[] a = null;
System.out.println("Pressed");
Calendar currentDay = Calendar.getInstance();
System.out.println(currentDay.get(Calendar.DAY_OF_WEEK));
Boolean wasFound = false;
switch (currentDay.get(Calendar.DAY_OF_WEEK)) {
case Calendar.MONDAY:
a = timesM;
a[8] = cal;
System.out.println("Monday");
System.out.println(a.length);
for (int i = 0; i < a.length - 1; i++) {
System.out.println(cal.before(a[i]));
if (cal.before(a[i])) {
periodName = lincoln.getCurrentPeriod(0, i++);
System.out.println(periodName);
wasFound = true;
break;
}
}
break;
case Calendar.TUESDAY:
a = timesT;
a[8] = cal;
for (int i = 0; i < a.length - 1; i++) {
if (cal.before(a[i])) {
periodName = lincoln.getCurrentPeriod(0, i++);
System.out.println(periodName);
wasFound = true;
break;
}
}
break;
case Calendar.WEDNESDAY:
a = timesW;
a[8] = cal;
for (int i = 0; i < a.length - 1; i++) {
if (cal.before(a[i])) {
periodName = lincoln.getCurrentPeriod(0, i++);
System.out.println(periodName);
wasFound = true;
break;
}
}
break;
case Calendar.THURSDAY:
a = timesTh;
a[8] = cal;
for (int i = 0; i < a.length - 1; i++) {
if (cal.before(a[i])) {
periodName = lincoln.getCurrentPeriod(0, i++);
System.out.println(periodName);
wasFound = true;
break;
}
}
break;
case Calendar.FRIDAY:
a = timesF;
a[8] = cal;
for (int i = 0; i < a.length - 1; i++) {
if (cal.before(a[i])) {
periodName = lincoln.getCurrentPeriod(0, i++);
System.out.println(periodName);
wasFound = true;
break;
}
}
break;
default:
System.out.println("Weekends");
periodName = "Weekends";
break;
}
if (!wasFound || periodName == "Weekends") {
Alert alert = new Alert("School is not currently on.", "Please check during a valid time.");
}
else {
Alert alert = new Alert(periodName, "Yet to be added");
}
}
}
@Override
public void nativeKeyReleased(NativeKeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void nativeKeyTyped(NativeKeyEvent arg0) {
// TODO Auto-generated method stub
}
最后提醒:
JOptionPane.showMessageDialog(null, "Works!");
final Timer timer = new Timer(10000, this);
frame = new JFrame();
frame.setBounds(100, 100, 250, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel(Title);
frame.getContentPane().add(label, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel(information);
frame.getContentPane().add(lblNewLabel, BorderLayout.CENTER);
JButton btnDismiss = new JButton("Dismiss");
btnDismiss.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
timer.stop();
}
});
frame.getContentPane().add(btnDismiss, BorderLayout.SOUTH);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
frame.setLocation( (int) (ge.getMaximumWindowBounds().getWidth() - frame.getWidth()), 25);
frame.setUndecorated(true);
frame.setVisible(true);
timer.start();
答案 0 :(得分:0)
你以新的和有趣的方式滥用事件修饰符;)尝试以下方法:
if (arg0.getKeyCode() == NativeKeyEvent.VK_5 && arg0.getModifiers() & NativeInputEvent.CTRL_MASK) {
如果您正在使用带有Swing / AWT的JNativeHook,请阅读working with swing wiki部分,了解如何与其进行正确的互动。
解决无障碍问题;在OS X&lt; 10.9,需要在系统首选项中检查辅助功能复选框。在10.9及更高版本中,您必须单独注册请求可访问性访问的每个应用程序。大多数情况下,这只需要是java运行时,除非你使用包装器启动Java(就像eclipse那样)。