Joe Dawtry Joe Dawtry - 5 months ago 12
Java Question

JNativeHook multiple responses from the same action (mouse click, key type & mouse scroll)

I'm using JNativeHook to retrieve information on how active a user is, but I am having an issue where I will get multiple responses from a single action but it's not consistent. It changes between 1, 2 or 3 responses but never more than 3 and I will have to run and exit the program multiple times in order to get the different results. It can seem to work for an entire day before I find the issue has occurred again.

I have also had the problem where I have to restart my computer in order to get some sort of response from an action.

An example print out from the program:

Mouse Pressed: 1

Mouse Pressed: 1

Mouse Pressed: 1

Mouse Released: 1

Mouse Clicked: 3

Mouse Released: 1

Mouse Clicked: 3

Mouse Released: 1

Mouse Clicked: 3


I am using 3 classes, one for each: mouse listener, keyboard listener and a mouse wheel listener. I create a thread of each of these to have them run concurrently.

There are 4 main classes that deal with code that has the issue:


  1. Class that contains the threads

  2. Keyboard listener

  3. Mouse Listener

  4. Mouse Wheel Listener.



(There was a lot of code that I was going post which almost made the post unreadable so I hope it's okay to do it like this?)

I am new to using JNativeHook and I'm not sure if it is maybe creating multiple objects of the same global listener or a concurrency issue.

Answer

'Glocal GlobalScreen.registerHook method was executed across multiple class files which mean that multiple hooks were being register and causing the erroneous behaviour.