java - MenuShortcut KeyEvent not working -
the code below testable class should print out text when control+a has been pushed on keyboard, , display image in system tray. dependent on system tray being supported operating system.
my issue text not being printed out when push control+a, printed when press item in 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(); } } } }
the reason code not working because java not 'globally' listening key event, when menu has focus , shown.
this reason why there no possibility write pure java keylogger. java allows capture window-directed messages.
a workaround implement 1 of these options:
- use jni/jna/whatever native wrapper access global key hooking
- use invisible window, being on top , not shown in system tray captures events. not suggest using 1 may either not work charm or annoys user.
the top approach not hard 1 require use native access , therefor application becomes platform-specific.
good luck!
Comments
Post a Comment