(example) to listen to all MouseEvents and KeyEvents in an application that you can use:
long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK + AWTEvent.KEY_EVENT_MASK; Toolkit.getDefaultToolkit().addAWTEventListener( new AWTEventListener() { public void eventDispatched(AWTEvent e) { System.out.println(e.getID()); } }, eventMask);
Since this code runs in Thread Dispatch Thread, you will need to make sure that it runs quickly so that the user interface does not respond. The above approach is used here if you want to see a working example.
See here for more information: Global Event Listeners
And this is for learning: AWT event listener
source share