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
324 stars 32 forks source link

Simulating VcMediaNext also sends P key #81

Closed TolikPylypchuk closed 6 months ago

TolikPylypchuk commented 7 months ago

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

Originally posted by **BlackCoyote** February 3, 2024 Performing the following line of code on windows (10): `new EventSimulator().SimulateKeyPress(SharpHook.Native.KeyCode.VcMediaNext);` also seems to simulate the P key. I used this website to test simulated keys: https://en.key-test.ru/, I also experienced this in other applications that react to the P key. I don't experience the same behaviour when I simply press the MediaNext key on my keyboard. I did also experience this when I tried a different library for simulating keystrokes, so I don't expect this to be an issue with the library, but I was really hoping I could find some help here.
TolikPylypchuk commented 7 months ago

I've tried to reproduce this issue, and everything works fine on my machine. I've tried to search for this issue, and the closest thing that I've found is this Roblox discussion, but it doesn't say anything about how to fix it. I didn't find anything else, unfortunately.

Since this problem is not reproducible on my machine, it would be impossible to know that I've actually fixed it, so any help would be greatly appreciated, but I won't look into it for the time being.

BlackCoyote commented 7 months ago

I've had tests performed for this issue on 5 different machines now, and the issue happened on 4 out of 5 of them. The issue can happen on both windows 10 and windows 11.

On 3 of the devices which have the issue, I've tried using AutoHotKey to simulate the media keys, and on all of them, the issue did not occur. It seems that something about the way AHK performs their keystrokes (or perhaps handles media keys separately) is different enough to get around the issue.

This is not a lot to work with unfortunately, but I figured It was worth sharing either way.

TolikPylypchuk commented 7 months ago

Thanks for sharing the info! I will look into how AutoHotKey manages media keys, maybe there's something special about them.

TolikPylypchuk commented 7 months ago

@BlackCoyote OK, after looking through the (really non-trivial) code of AutoHotKey, I think I've found the reason for this problem - the extended-key flag is not being set when simulating media keys and that's why your system may get confused. It seems that there is no reliable list of keys that should have this flag enabled, but AutoHotKey sets it when the high-order byte of the key's scan code is non-zero. Previously libuiohook has set this flag for keys defined in a static list, so I've updated its code to do the same as AutoHotKey.

Since I cannot test this change (it works on my machine without problems in both cases), could you please download a patched version of libuiohook, substitute it manually in the output folder, and see whether the problem is gone? Here's the file: uiohook.zip. If it's gone then I'll include this change in the next version of SharpHook that I'm going to release soon. Of course, if you don't trust random DLLs from the internet, you can instead build it yourself from source.

TolikPylypchuk commented 6 months ago

Sorry, the previous version of libuiohook that I've sent here didn't map the scan code correctly so that version probably won't work. Here's a new one: uiohook.zip. I've checked that at least the scan codes are now different for the media keys than they used to be on my machine.

BlackCoyote commented 6 months ago

I was just about mentally prepared to deliver you the bad news that you deciphered the AHK source code for nothing, when I got your follow-up with the corrected DLL.

However I can now happily share that it does indeed seem fix the issue. I've tested it on 2 devices that experienced the issue before and they both only send the media key with no additional keys now.

Thanks for your efforts! I'll be sure to give it some more rigorous testing over the next few days and I'll let you know if any other complications come up.

TolikPylypchuk commented 6 months ago

That's great to hear! I hope it works well on other devices as well!

BlackCoyote commented 6 months ago

We've tested it on the same 5 devices as before now and it's working great on all of them. The 4 devices that experienced the issue now all work as expected, and the one that did not experience the issue unsurprisingly still works as it is supposed to.

Nice work!

TolikPylypchuk commented 6 months ago

I've just released version 5.3.0 which fixes this problem so the issue can be closed.