• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 931
  • Last Modified:

Detecting mouse and keyboard events


I'm using JDK 1.4 and SWT to build an application.
Is there a way of detecting mouse and keyboard events on the operating system level in Java?

I want to detect if the user has not used the mouse or the keyboard for five minutes and trigger
an event in that case.

I'm quite sure this is not possible but has anyone tried to do the same?

4 Solutions
not possible without using native code and JNI
mbutuAuthor Commented:
And there is no library I can download or feature of SWT I can use?
There is no such native API in SWT as I know.

You have to write your own DLL to do that otherwise.
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Mayank SAssociate Director - Product EngineeringCommented:
Yup, you need native code for that.
I think you can with jdk 1.5 by the java.awt.MouseInfo class. The trick is get mouse location every n secs by thread, if it's the same location in five minite then trigger an event.
Mayank SAssociate Director - Product EngineeringCommented:
But remember that 1.5 is still in the beta version....
mbutuAuthor Commented:
Ok. java.awt.MouseInfo seems to be good enough. If I need to know if the mouse is idle.
But I also have to know if the keyboard is idle. I guess there's nothing in JDK 1.5 for that.

Mayank SAssociate Director - Product EngineeringCommented:
As far as I know, MouseInfo has methods getNumberOfButtons () and getPointerInfo (). getNumberOfButtons () is not needed for your case. Using the static getPointerInfo () method, you get a PointerInfo () object which has a getLocation () method to return you the Point object containing the current co-ordinates of the mouse. But the question remains - how will you call this method when a mouse-click happens outside your application? You cannot capture that mouse-click still.... using these co-ordinates, you can just know if the mouse is currently positioned outside your GUI or not. But how will you detect the click?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now