jnativehook icon indicating copy to clipboard operation
jnativehook copied to clipboard

Debugging in nativeKeyPressed lags mouse

Open NiPfi opened this issue 9 years ago • 7 comments

Whenever I use breakpoints in a method called in the nativeKeyPressed method in my NativeKeyListener the mouse becomes extremely unresponsive which makes debugging difficult. When the NativHook is unregistered beforehand, this does not occur but it can't be re-registered as the program just stops executing without throwing any exception.

Steps to reproduce lag

  1. Set breakpoint in nativeKeyPressed or any method called therein and debug
  2. Press Key

Steps to reproduce exit w/o exception:

  1. register NativeHook and add NativeKeyListener In the nativeKeyPressed method:
  2. unregister NativeHook
  3. register NativeHook

I was not able to find any documentation on how to suspend the NativeHook or something similar to prevent this issue

NiPfi avatar Dec 15 '16 10:12 NiPfi

Hi,

What dispatcher are you using? (Are you calling setEventDispatcher)?

What platform and architecture?

kwhat avatar Dec 16 '16 03:12 kwhat

I did not call the 'setEventDispatcher' method, so it's the default one.

I ran it on a Windows 7 64bit machine

NiPfi avatar Dec 16 '16 13:12 NiPfi

Interesting, I suspect there is no easy way to fix this issue but I have long suspect it maybe a problem. Basically the JVM debugger is pausing the execution of native code and because the library is sitting in the input event delivery queue, you get input lag while the OS freaks out and times out the library. This is why you cant block the delivery thread when you use the VoidDispatchService. The only way I can think of the prevent this would be to detect when a debugger gets attached and unregister the hook, which may defeat the purpose of debugging...

kwhat avatar Dec 18 '16 19:12 kwhat

I just stumbled over this. I'd be nice to add it to an FAQ or something similar. I was wondering if my application caused it, because i was messing around with it for a while.

yenon avatar Mar 09 '17 22:03 yenon

This issue is still happening but slightly differently for me. If the native hook is registered, any debugger steps anywhere in the code cause extreme mouse cursor lag for 5 - 10 seconds before it recovers and the mouse will move fluently again. It's quite annoying. I'm using the latest IntelliJ IDEA 2018.2.4 on Windows 10 Pro and the latest maven dependency of jnativehook (v2.1.0). It would be great if this could be fixed at some point.

BullyWiiPlaza avatar Oct 07 '18 16:10 BullyWiiPlaza

The issue is that the debugger is going to pause a process so you can you-know debug it. Because the library creates a direct route from the operating systems event delivery queue to java, and the vm is paused execution, the operating system does not get a return from the function callback it gave the library. The OS is waiting for some timeout period before trying again. This behavior is different on different OS versions and flavors. For example, This does not affect linux (yet) because you cannot sit that low in event delivery from X11. On Windows, the behavior for >= XP would basically be to freeze. After that, because of the abuse of SetWindowsHookEx and this freezing problem putting a lot of blame on MS, they started changing the way this function works with Vista. Some of these changes include stopping the offending program, which in this case, may cause the hook to re-register itself. There is no formal documentation updates from Microsoft about these ABI changes. Basically if you want to "solve" this problem, the hook would need to run in another process as like a daemon and be async with java.exe. Its a huge pain in the ass any way you look at it and for a bug that only affects the debugger, I just don't have time to fix it all. I don't even have time to fix the toolchains to fix the current release... 2.1.0 has a lot of bugs that are fixed in the src tree. Being a seasoned developer has not stacked up like I would have expected it to, and looking around at the offices I work in you really don't see anyone writing code over 40 and it isn't because we are all made millions of dollars in startup money.

kwhat avatar Oct 10 '18 19:10 kwhat

If you, or anyone else can give me some C or Java code that can run when the process is hooked and/or unhooked by the debugger, I can fix this fairly easily. I doubt this will be possible in Java and it is unlikely to work in C for this problem but it is worth a mention.

kwhat avatar Oct 10 '18 19:10 kwhat