TolikPylypchuk / SharpHook

SharpHook provides a cross-platform global keyboard and mouse hook, event simulation, and text entry simulation for .NET
https://sharphook.tolik.io
MIT License
342 stars 32 forks source link

Intermittently not getting release events (Windows 11) #61

Closed TolikPylypchuk closed 1 year ago

TolikPylypchuk commented 1 year ago

Discussed in https://github.com/TolikPylypchuk/SharpHook/discussions/60

Originally posted by **tkefauver** September 1, 2023 TLDR: Is there a way with Sharphook to get an array, list, etc. of the current down keys? (as a workaround to this) I'm using the `SimpleGlobalHook` with `RunAsync` and storing a list of down keys where I add the key (if not a repeat) in a `KeyPressed` handler and then remove it in a `KeyReleased` handler. Intermittently a key will get 'stuck' where I never receive the release for a press and it seems to be completely random (could be any input or modifier key). I don't think its a problem w/ SharpHook but just can't figure out what's causing it! These are the factors that maybe involved: 1. I'm testing on a dell laptop and thinking maybe it could related to using the Fn key (to use Home/End keys from the laptop arrows for example) and I **do** know that Fnby itself doesn't trigger a `KeyPressed` or `KeyReleased` which maybe unrelated but I figure its worth mentioning. 2. It seems to happen when I'm doing a lot of typing or keyboard navigating but that's all I know. 3. This is in an AvaloniaUI app (on Windows 11 22H2) and am going to ask a similar question on there github too because Avalonia's `HotkeyManager` maybe suppressing the `KeyReleased` from one of its `KeyGesture`s internally somehow but my `SimpleGlobalHook` handles all the shortcuts and I would think it wouldn't be able to suppress globally but since its off the same main thread maybe it could be blocking it? Its so intermittent though so doesn't seem likely. So my questions I suppose are these: 1. Is every down event supposed to have a corresponding up? Does pressing Shift for example when a key is down change its release `KeyCode` maybe? 2. Can other applications be suppressing the `KeyRelease` somehow? (like a Visual Studio thing? I don't have any global hot keys) 3. Since it seems to happen when I'm doing a lot of typing could the OS be blocking my handlers because they're taking too long? (They don't do **that** much processing, just comparing input to a few shortcuts right now and read [here](https://github.com/TolikPylypchuk/libuiohook/blob/dfd2647140a37c245e63ea74e3ce97a3d4833399/demo/demo_hook.c#L56-L58) about blocking but this is only a release issue so not sure if that info is a factor here) This has been a lingering bug for MONTHS so any insights would be appreciated 🙏
TolikPylypchuk commented 1 year ago

Originally answered in the discussion:

Hi! Thanks for asking! This looks like a problem with libuiohook, or even Windows itself. It sounds similar to issue #59 - what's described there is that a modifier key can get stuck in the pressed state. I will look into it.

As for your comments:

  1. The Fn key is a hardware key, and as far as I know, even the OS doesn't handle it.
  2. I think it's somehow related to message processing in Windows, but I'm not sure.
  3. I don't think Avalonia has anything to do here, to be honest.

As for your questions:

  1. Yes, when you press and release a key, it should emit a down event, and then an up event with the same key code.
  2. Yes, they can - SharpHook provides this possibility as well. But I don't think that you have a global hook in your system which randomly suppresses key up events without your knowledge :)
  3. I don't think it's necessarily related to your handler taking too long. If you want to test it out, you can use TaskPoolGlobalHook - unlike SimpleGlobalHook, it will run your event handlers in a separate thread, so it can take as long as it needs.
TolikPylypchuk commented 1 year ago

I could reproduce this issue (typing really fast and seeing that keys sometimes remained in the pressed state), but I don't know why that happens and how to fix that. Googling doesn't help either. I think this is a problem with Windows itself, but not sure.

So, I don't really know what to do about this issue. I'll try googling some more and trying to debug it further, but no promises about the result.

TolikPylypchuk commented 1 year ago

Hi @tkefauver! I may have fixed the issue in the latest build of SharpHook - it's hard to know since the issue itself is hard to reproduce. Could you please download the latest libuiohook binary and replace it directly in the bin folder of your application to verify that it works for you? If the issue is indeed fixed, then I'll release version 5.1.0 soon (hopefully).

You can get the latest libuiohook binary from the latest GitHub Actions artifact. Download libuiohook-build and inside it windows/<your arch>/bin/uiohook.dll is the file you need.

tkefauver commented 1 year ago

@TolikPylypchuk Adding it now and will let you know how it goes... Fingers crossed!

TolikPylypchuk commented 1 year ago

I released version 5.1.0 today which should contain the fix for this issue (at least it's not reproducible for me anymore), so this issue can be closed. If it doesn't fix the issue after all, then we can reopen this issue.

tkefauver commented 1 year ago

@TolikPylypchuk Yes I'm almost sure its fixed now! Thank you so much for working it out 🏆