0

The code below is a testable class that should print out some text when control+A has been pushed on the keyboard, and will also display an image in the system tray. This is all dependent on the system tray being supported by your operating system.

My issue is that the text is not being printed out when I push control+A, it is only printed when I press the item in the system tray.

/**
 *
 * @author Tyluur
 * @since Aug 23, 2013
 */
public class Testable {

public static void main(String... args) {
    registerTrayItems();
}

private static void registerTrayItems() {
    if (SystemTray.isSupported()) {
        SystemTray tray = SystemTray.getSystemTray();
        TrayIcon icon = null;
        MenuShortcut shortcut = new MenuShortcut(KeyEvent.VK_A);
        MenuItem menuItem = new MenuItem("Toggle", shortcut);
        menuItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.err.println("The action has been called!");
            }
        });
        PopupMenu popup = new PopupMenu();
        popup.add(menuItem);
        try {
            icon = new TrayIcon(new ImageIcon(new URL("http://i.imgur.com/xQoz2TN.png")).getImage(), "Typer", popup);
            tray.add(icon);
        } catch (MalformedURLException | AWTException e) {
            e.printStackTrace();
        }
    }       
}

}
Tyluur
  • 95
  • 2
  • 10
  • `"What am I doing wrong?"` -- for one, not posting an [sscce](http://sscce.org). Please post code that we can test, modify and run. – Hovercraft Full Of Eels Aug 23 '13 at 22:17
  • It has been updated with an SSCCE. – Tyluur Aug 23 '13 at 22:19
  • 1
    Hell, why are you asking the same question twice? We told you in your previous question that Java won't work this way, that KeyListeners don't work for applications that don't have the focus, and the answer, nor surprisingly, is still the same! – Hovercraft Full Of Eels Aug 23 '13 at 22:30

2 Answers2

2

The reason your code is not working is because Java is not 'globally' listening for your key event, but only when the menu has your focus and is shown.

This is also the reason why there is no possibility to write a pure Java keylogger. Java only allows you to capture window-directed messages.

A workaround would be to implement one of these options:

  • Use JNI/JNA/whatever native wrapper to access global key hooking
  • Use an invisible window, always being on top and not shown in the system tray that captures the events. I would not suggest using this one as it may either not work like a charm or annoys your user.

The top approach is not a hard one but will require you to use native access and therefor your application becomes platform-specific.

Good luck!

Bart Pelle
  • 747
  • 1
  • 5
  • 18
0

From the API, it looks like you should try this constructor for the MenuItem()

MenuShortcut shortcut = new MenuShortcut(KeyEvent.VK_A, false);

as the default is to require that the Shift key be pressed too.

n0741337
  • 2,474
  • 2
  • 15
  • 15
  • I changed the VK_A to VK_F12 since VK_A is a commonly used hotkey by many programs and added the false argument but there is no difference. – Tyluur Aug 23 '13 at 22:24
  • Having run your code now, I agree with @BartPelle. But this should have fixed needing the shift key too. – n0741337 Aug 23 '13 at 22:38