SharpHook
SharpHook copied to clipboard
Keyboard capture failing on macos apps
I'm trying to capture keyboard data using the SharpHook library in a Mac OS environment, as per the example on the website.
Everything works great, except if the machine is accessed via AnyDesk.
On Intel processors, only some keys are recognized, as below (such as CTRL, ALT or SHIFT) and the rest do not trigger events. The other keys such as 'Q', 'W', '1', etc., all do not work.
On ARM processors, all keys are recognized as 'A' (except special keys such as CTRL, ALT or SHIFT).
Mouse events are working correctly (via AnyDesk or physically on the machine). Keyboard events only work correctly using the machine physically.
Is there any solution?
Here is an example of my PublishProfile:
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net6.0\publish\osx-x64\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
<TargetFramework>net6.0</TargetFramework>
<RuntimeIdentifier>osx-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>true</PublishSingleFile>
<PublishTrimmed>false</PublishTrimmed>
</PropertyGroup>
</Project>
Here is the source code:
using SharpHook;
using SharpHook.Providers;
using SharpHook.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
namespace _005_SharpHook
{
internal class Program
{
static void Main(string[] args)
{
var provider = UioHookProvider.Instance;
Console.WriteLine("System Info:");
Console.WriteLine($"Auto-repeat rate: {provider.GetAutoRepeatRate()}");
Console.WriteLine($"Auto-repeat delay: {provider.GetAutoRepeatDelay()}");
Console.WriteLine($"Pointer acceleration multiplier: {provider.GetPointerAccelerationMultiplier()}");
Console.WriteLine($"Pointer acceleration threshold: {provider.GetPointerAccelerationThreshold()}");
Console.WriteLine($"Pointer sensitivity: {provider.GetPointerSensitivity()}");
Console.WriteLine($"Multi-click time: {provider.GetMultiClickTime()}");
var screens = provider.CreateScreenInfo();
foreach(var screen in screens)
{
Console.WriteLine($"Screen #{screen.Number}: {screen.Width}x{screen.Height}; {screen.X}, {screen.Y}");
}
Console.WriteLine("---------- Press q to quit ----------\n");
var hook = new SimpleReactiveGlobalHook(TaskPoolScheduler.Default);
hook.HookEnabled.Subscribe(OnHookEvent);
hook.HookDisabled.Subscribe(OnHookEvent);
hook.KeyTyped.Subscribe(OnHookEvent);
hook.KeyPressed.Subscribe(OnHookEvent);
hook.KeyReleased.Subscribe(e => OnKeyReleased(e, hook));
hook.MouseClicked.Subscribe(OnHookEvent);
hook.MousePressed.Subscribe(OnHookEvent);
hook.MouseReleased.Subscribe(OnHookEvent);
hook.MouseMoved
//.Throttle(TimeSpan.FromSeconds(1))
.Subscribe(OnHookEvent);
hook.MouseDragged
.Throttle(TimeSpan.FromSeconds(1))
.Subscribe(OnHookEvent);
hook.MouseWheel.Subscribe(OnHookEvent);
hook.Run();
}
static void OnHookEvent(HookEventArgs e)
{
Console.WriteLine($"{e.EventTime.ToLocalTime()}: {e.RawEvent}");
}
static void OnKeyReleased(KeyboardHookEventArgs e, IReactiveGlobalHook hook)
{
if(e.Data.KeyCode == SharpHook.Native.KeyCode.VcQ)
{
hook.Dispose();
}
}
}
}