[un-clickable window PIC] Chapter 11.   Detecting User Input

 

A program can respond to the keyboard and mouse by utilizing Java's numerous java.io input stream and reader classes, or the key and mouse listeners in java.awt.event. So, what's the need for J/Invoke?

The crucial limitation is that key and mouse data can only be collected if the Java application is the active window. There's (almost) no way for a Java program to listen to input being directed towards other windows or to the Desktop.

This ability to monitor (or if you prefer, 'snoop on') user activity is very useful for a wide range of applications, including contextual help, screensavers, and logging tools.

Hacks 11-1 and 11-2 are concerned with monitoring the position of the mouse and moving it, no matter where it is on screen. This functionality is supported by Java's MouseInfo and Robot classes. We start using J/Invoke in Hack 11-3 to poll for mouse and keyboard state information. Hack 11-4 is about detecting the absence of user activity (i.e. no mouse clicks, moves, or keypresses).

The Hack 11-3 technique isn't ideal due to the difficulty of determining a good polling interval. Hacks 11-5 and 11-6 introduce Win32 keyboard and mouse hooks, which trigger callback functions when a key is pressed or the mouse state changes. Hack 11-6 also shows how a clicked mouse position can be converted into more useful information: the name of the window activated by the click.

[signal window PIC] The last two hacks are concerned with low-level signal handling triggered by the user pressing ctrl-c or ctrl-break: Hack 11-7 discusses how termination signals can be caught, while Hack 11-8 looks at modifying the meaning of a ctrl-break signal.

 

Downloads


Navigation:


Dr. Andrew Davison
E-mail: ad@coe.psu.ac.th
Back to my home page