{"id":481491,"date":"2026-05-29T05:48:59","date_gmt":"2026-05-29T05:48:59","guid":{"rendered":"https:\/\/savepearlharbor.com\/?p=481491"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=481491","title":{"rendered":"KeyboardHook: \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u044b\u0439 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442 \u043a\u043b\u0430\u0432\u0438\u0430\u0442\u0443\u0440\u044b \u0438 \u043c\u044b\u0448\u0438 \u0434\u043b\u044f .NET"},"content":{"rendered":"<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430: \u043d\u0438 \u043e\u0434\u0438\u043d \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043f\u0430\u043a\u0435\u0442 \u043d\u0435 \u043f\u043e\u0434\u043e\u0448\u0451\u043b<\/h3>\n<p>\u041f\u0435\u0440\u0432\u044b\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0430\u043a\u0435\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b, \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0430 Windows \u0438 \u043c\u043e\u043b\u0447\u0430\u043b\u0438 \u043d\u0430 macOS. \u0414\u0440\u0443\u0433\u0438\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u0435\u0448\u0430\u043b\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u043d\u0430\u0436\u0430\u0442\u0438\u0438 \u043a\u043b\u0430\u0432\u0438\u0448\u0438. \u042f \u0438\u0441\u043a\u0430\u043b \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u00ab\u0442\u043e\u043b\u044c\u043a\u043e Windows\u00bb \u043c\u0435\u043d\u044f \u043d\u0435 \u0443\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u043b\u043e.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0447\u0435\u0433\u043e-\u0442\u043e \u043d\u0435\u0442, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c? \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u043c\u0443. \u041f\u043e \u043d\u0430\u0447\u0430\u043b\u0443 \u0432\u0441\u0451 \u0431\u044b\u043b\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u0434\u0430\u0436\u0435 \u0431\u0435\u0437 \u0432\u044b\u043d\u0435\u0441\u0435\u043d\u0438\u044f \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443, \u043d\u043e \u0447\u0443\u0442\u044c \u043f\u043e\u0437\u0436\u0435, \u043a\u043e\u0433\u0434\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u0441\u044f \u0438 \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u043c\u043e\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445, \u043f\u043e\u0434\u0443\u043c\u0430\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0441 \u043d\u0443\u043b\u044f. \u0421\u0430\u043c\u043e\u0435 \u0433\u043b\u0430\u0432\u043d\u043e\u0435 \u0447\u0435\u0433\u043e \u044f \u0445\u043e\u0442\u0435\u043b \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f: \u0431\u0435\u0437 \u043b\u0438\u0448\u043d\u0438\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439, \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u043e\u0441\u0442\u044c, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u0447\u0435\u0432\u0430\u0442\u044c \u043c\u0435\u0436\u0434\u0443 pet-\u043f\u0440\u043e\u0435\u043a\u0442\u0430\u043c\u0438. \u041f\u043e \u0445\u043e\u0434\u0443 \u0440\u0435\u0448\u0438\u043b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0435 open source, \u0447\u0442\u043e\u0431\u044b \u043d\u0435\u043c\u0438\u043d\u0443\u0435\u043c\u043e \u0443\u043b\u0443\u0447\u0448\u0430\u0442\u044c \u0438 \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435.<\/p>\n<p><a href=\"https:\/\/github.com\/c3n9\/KeyboardHook\" rel=\"noopener noreferrer nofollow\"><strong>KeyboardHook<\/strong><\/a> &#8212; \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u044b\u0439 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u0445\u0443\u043a \u043a\u043b\u0430\u0432\u0438\u0430\u0442\u0443\u0440\u044b \u0438 \u043c\u044b\u0448\u0438 \u0434\u043b\u044f .NET Standard 2.0.<\/p>\n<hr\/>\n<h3>\u0427\u0442\u043e \u0443\u043c\u0435\u0435\u0442 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430<\/h3>\n<ul>\n<li>\n<p>\u0413\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442 \u043d\u0430\u0436\u0430\u0442\u0438\u0439 \u0438 \u043e\u0442\u043f\u0443\u0441\u043a\u0430\u043d\u0438\u0439 \u043a\u043b\u0430\u0432\u0438\u0448 \u2014 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u0430\u0436\u0435 \u043a\u043e\u0433\u0434\u0430 \u0432\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0435 \u0432 \u0444\u043e\u043a\u0443\u0441\u0435<\/p>\n<\/li>\n<li>\n<p>\u0413\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u043c\u044b\u0448\u0438 (Left, Right, Middle)<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u0430\u044f \u044d\u043c\u0443\u043b\u044f\u0446\u0438\u044f \u043d\u0430\u0436\u0430\u0442\u0438\u0439 \u043a\u043b\u0430\u0432\u0438\u0448 \u0438 \u043a\u043d\u043e\u043f\u043e\u043a \u043c\u044b\u0448\u0438<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439 \u043a\u043b\u0430\u0432\u0438\u0448 (Ctrl+C, Alt+F4 \u0438 \u043b\u044e\u0431\u044b\u0445 \u0434\u0440\u0443\u0433\u0438\u0445)<\/p>\n<\/li>\n<li>\n<p>\u0422\u0440\u0438 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u044b: <strong>Windows x86\/x64<\/strong>, <strong>macOS Arm64<\/strong>, <strong>Linux x64 (X11)<\/strong><\/p>\n<\/li>\n<li>\n<p>\u041d\u0435\u0442 \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u2014 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 .NET \u0438 P\/Invoke \u043a \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u043c<\/p>\n<\/li>\n<\/ul>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u043e\u0439:<\/p>\n<pre><code>dotnet add package KeyboardHook<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:87px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<hr\/>\n<h3>\u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430: \u043e\u0434\u0438\u043d \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u0442\u0440\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/h3>\n<p>\u041a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u2014 \u043f\u043e\u043b\u043d\u043e\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u043e\u0433\u043e API \u0438 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430. \u0421\u043d\u0430\u0440\u0443\u0436\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432\u0438\u0434\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u0432\u0430 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430:<\/p>\n<pre><code>public interface IKeyboardHook{    event Action&lt;KeyboardKey&gt; KeyDown;    event Action&lt;KeyboardKey&gt; KeyUp;    void SendKey(KeyboardKey key);    void SendKeyCombo(params KeyboardKey[] keys);}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<pre><code>public interface IMouseHook : IDisposable{    event Action&lt;MouseButton&gt; ButtonDown;    event Action&lt;MouseButton&gt; ButtonUp;    void SendButton(MouseButton button);    void SendButtonCombo(params MouseButton[] buttons);}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0443\u0436\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u2014 \u043e\u0434\u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0430, \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435:<\/p>\n<pre><code>IKeyboardHook keyboard = KeyboardHookFactory.Create();IMouseHook mouse = MouseHookFactory.Create();<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0424\u0430\u0431\u0440\u0438\u043a\u0430 \u0432\u043d\u0443\u0442\u0440\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code>public static class KeyboardHookFactory{    public static IKeyboardHook Create()    {        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))            return new WindowsKeyboardHook();        if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))            return new LinuxKeyboardHook();        if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))            return new MacKeyboardHook();        throw new PlatformNotSupportedException(\"Unsupported platform\");    }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<hr\/>\n<h3>\u041c\u0430\u043f\u043f\u0438\u043d\u0433 \u043a\u043b\u0430\u0432\u0438\u0448: \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b \u0432\u043c\u0435\u0441\u0442\u043e switch-\u043f\u043e\u0440\u0442\u044f\u043d\u043e\u043a<\/h3>\n<p>\u041a\u0430\u0436\u0434\u0430\u044f \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0441\u0432\u043e\u0438 \u043a\u043e\u0434\u044b \u043a\u043b\u0430\u0432\u0438\u0448. \u041d\u0430 Windows \u044d\u0442\u043e Virtual Key Codes, \u043d\u0430 Linux \u2014 X11 keycodes, \u043d\u0430 macOS \u2014 Virtual Key \u043a\u043e\u0434\u044b CoreGraphics. \u0412\u043c\u0435\u0441\u0442\u043e \u0442\u0440\u0451\u0445 \u0433\u0438\u0433\u0430\u043d\u0442\u0441\u043a\u0438\u0445 <code>switch<\/code>-\u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u044f \u0440\u0435\u0448\u0438\u043b \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043a\u043e\u0434\u044b \u043f\u0440\u044f\u043c\u043e \u0432 enum \u0447\u0435\u0440\u0435\u0437 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b:<\/p>\n<pre><code>public enum KeyboardKey{    [WindowsCode(0x41)]    [LinuxCode(38)]    [MacosCode(0)]    A,    [WindowsCode(0x1B)]    [LinuxCode(9)]    [MacosCode(53)]    Escape,    [WindowsCode(0x70)]    [LinuxCode(67)]    [MacosCode(122)]    F1,    \/\/ ... 150+ \u043a\u043b\u0430\u0432\u0438\u0448}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0410\u0442\u0440\u0438\u0431\u0443\u0442\u044b \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u044b:<\/p>\n<pre><code>[AttributeUsage(AttributeTargets.Field)]public class WindowsCodeAttribute : Attribute{    public int Code { get; }    public WindowsCodeAttribute(int code) =&gt; Code = code;}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u044f \u0432 \u043e\u0431\u0435 \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0447\u0435\u0440\u0435\u0437 \u0440\u0435\u0444\u043b\u0435\u043a\u0441\u0438\u044e:<\/p>\n<pre><code>internal static int ToPlatformCode(this KeyboardKey key){    var field = typeof(KeyboardKey).GetField(key.ToString());    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))        return field?.GetCustomAttribute&lt;WindowsCodeAttribute&gt;()?.Code ?? 0;    if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))        return field?.GetCustomAttribute&lt;LinuxCodeAttribute&gt;()?.Code ?? 0;    if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))        return field?.GetCustomAttribute&lt;MacosCodeAttribute&gt;()?.Code ?? 0;    return 0;}internal static KeyboardKey FromPlatformCode(int platformCode){    foreach (var field in typeof(KeyboardKey).GetFields())    {        if (field.FieldType != typeof(KeyboardKey)) continue;        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))        {            var attr = field.GetCustomAttribute&lt;WindowsCodeAttribute&gt;();            if (attr?.Code == platformCode)                return (KeyboardKey)field.GetValue(null);        }        \/\/ ... \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0434\u043b\u044f Linux \u0438 macOS    }    return KeyboardKey.None;}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u0443\u044e \u043a\u043b\u0430\u0432\u0438\u0448\u0443 \u0442\u0435\u043f\u0435\u0440\u044c \u0437\u043d\u0430\u0447\u0438\u0442 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043e\u0434\u043d\u0443 \u0437\u0430\u043f\u0438\u0441\u044c \u0432 enum \u0441 \u0442\u0440\u0435\u043c\u044f \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u043c\u0438 \u2014 \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u0442\u0440\u043e\u0433\u0430\u0442\u044c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e.<\/p>\n<hr\/>\n<h3>Windows: WH_KEYBOARD_LL<\/h3>\n<p>\u041d\u0430 Windows \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u043f\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043d\u0438\u0437\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u044b\u0439 \u0445\u0443\u043a <code>WH_KEYBOARD_LL<\/code> \u2014 \u044d\u0442\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c Win32 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430 \u043a\u043b\u0430\u0432\u0438\u0448 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u044b:<\/p>\n<pre><code>internal class WindowsKeyboardHook : IKeyboardHook, IDisposable{    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);    private const int WH_KEYBOARD_LL = 13;    private const int WM_KEYDOWN    = 0x0100;    private const int WM_KEYUP      = 0x0101;    private const int WM_SYSKEYDOWN = 0x0104;    private const int WM_SYSKEYUP   = 0x0105;    private readonly HashSet&lt;KeyboardKey&gt; _pressedKeys = new HashSet&lt;KeyboardKey&gt;();    public event Action&lt;KeyboardKey&gt; KeyDown;    public event Action&lt;KeyboardKey&gt; KeyUp;    public WindowsKeyboardHook()    {        _proc = HookCallback;        _hookId = SetHook(_proc);    }    private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)    {        if (nCode &gt;= 0)        {            var kb = Marshal.PtrToStructure&lt;KBDLLHOOKSTRUCT&gt;(lParam);            var key = KeyboardKeyExtensions.FromPlatformCode(kb.vkCode);            if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)            {                _pressedKeys.Add(key);                KeyDown?.Invoke(key);            }            else if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)            {                _pressedKeys.Remove(key);                KeyUp?.Invoke(key);            }        }        return CallNextHookEx(_hookId, nCode, wParam, lParam);    }    public void SendKey(KeyboardKey key)    {        var code = (byte)KeyboardKeyExtensions.ToPlatformCode(key);        keybd_event(code, 0, 0, UIntPtr.Zero);        keybd_event(code, 0, KEYEVENTF_KEYUP, UIntPtr.Zero);    }    public void SendKeyCombo(params KeyboardKey[] keys)    {        foreach (var k in keys)            keybd_event((byte)KeyboardKeyExtensions.ToPlatformCode(k), 0, 0, UIntPtr.Zero);        for (int i = keys.Length - 1; i &gt;= 0; i--)            keybd_event((byte)KeyboardKeyExtensions.ToPlatformCode(keys[i]), 0, KEYEVENTF_KEYUP, UIntPtr.Zero);    }    [DllImport(\"user32.dll\")] private static extern IntPtr SetWindowsHookEx(...);    [DllImport(\"user32.dll\")] private static extern IntPtr CallNextHookEx(...);    [DllImport(\"user32.dll\")] private static extern void keybd_event(...);    \/\/ ...}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0412\u0430\u0436\u043d\u0430\u044f \u0434\u0435\u0442\u0430\u043b\u044c: <code>SYSKEYDOWN<\/code>\/<code>SYSKEYUP<\/code> \u2014 \u044d\u0442\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043a\u043b\u0430\u0432\u0438\u0448, \u043d\u0430\u0436\u0430\u0442\u044b\u0445 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 Alt (\u0438\u043b\u0438 \u0435\u0441\u043b\u0438 \u043e\u043a\u043d\u043e \u043d\u0435 \u0432 \u0444\u043e\u043a\u0443\u0441\u0435). \u0411\u0435\u0437 \u043d\u0438\u0445 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u0435 \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u0443 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0439.<\/p>\n<hr\/>\n<h3>macOS: CGEventTap + CoreFoundation RunLoop<\/h3>\n<p>\u041d\u0430 macOS \u0432\u0441\u0451 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0435\u0435. \u0421\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u2014 <code>CGEventTap<\/code> \u0438\u0437 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 CoreGraphics. \u041e\u043d \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u00ab\u0442\u043e\u0447\u043a\u0443 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430\u00bb \u0432 \u043f\u043e\u0442\u043e\u043a\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0441\u0435\u0441\u0441\u0438\u0438 \u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0439 Accessibility (\u0438\u043d\u0430\u0447\u0435 <code>CGEventTapCreate<\/code> \u0432\u0435\u0440\u043d\u0451\u0442 <code>NULL<\/code>):<\/p>\n<pre><code>internal class MacKeyboardHook : IKeyboardHook, IDisposable{    private const int kCGEventKeyDown        = 10;    private const int kCGEventKeyUp          = 11;    private const int kCGKeyboardEventKeycode = 9;    private const int kCGSessionEventTap     = 1;    private CGEventTapCallBack _callbackKeepAlive; \/\/ \u0443\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c \u0434\u0435\u043b\u0435\u0433\u0430\u0442 \u043e\u0442 GC!    private IntPtr _eventTap;    private IntPtr _runLoop;    private void InitializeEventTap()    {        _callbackKeepAlive = EventCallback;        var eventMask = (1UL &lt;&lt; kCGEventKeyDown) | (1UL &lt;&lt; kCGEventKeyUp);        _eventTap = CGEventTapCreate(            kCGSessionEventTap,            place:   1,   \/\/ HeadInsert            options: 1,   \/\/ Default            eventsOfInterest: eventMask,            callback: _callbackKeepAlive,            userInfo: IntPtr.Zero        );        if (_eventTap == IntPtr.Zero)            throw new UnauthorizedAccessException(                \"Accessibility permissions are required to intercept keys.\");        _runLoopSource = CFMachPortCreateRunLoopSource(IntPtr.Zero, _eventTap, 0);        CGEventTapEnable(_eventTap, true);        StartRunLoop();    }    private void StartRunLoop()    {        var thread = new Thread(() =&gt;        {            _runLoop = CFRunLoopGetCurrent();            IntPtr modes = GetCFRunLoopCommonModes(); \/\/ \u0447\u0435\u0440\u0435\u0437 dlopen\/dlsym            CFRunLoopAddSource(_runLoop, _runLoopSource, modes);            CFRunLoopRun(); \/\/ \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u0442\u043e\u043a, \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u044f        })        {            IsBackground = true,            Name = \"MacKeyboardHook Loop\"        };        thread.Start();    }    private IntPtr EventCallback(IntPtr proxy, int type, IntPtr eventRef, IntPtr userInfo)    {        if (type == kCGEventKeyDown || type == kCGEventKeyUp)        {            long macKeyCode = CGEventGetIntegerValueField(eventRef, kCGKeyboardEventKeycode);            var key = KeyboardKeyExtensions.FromPlatformCode((int)macKeyCode);            if (type == kCGEventKeyDown) KeyDown?.Invoke(key);            else                         KeyUp?.Invoke(key);        }        return eventRef;    }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041e\u0434\u043d\u0430 \u043d\u0435\u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u0435\u0449\u044c: <code>kCFRunLoopCommonModes<\/code> \u2014 \u044d\u0442\u043e \u043d\u0435 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430, \u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0432\u043e \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0435. \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0435\u0440\u0435\u0437 <code>dlopen<\/code>\/<code>dlsym<\/code>:<\/p>\n<pre><code>private static IntPtr GetCFRunLoopCommonModes(){    IntPtr handle = dlopen(        \"\/System\/Library\/Frameworks\/CoreFoundation.framework\/CoreFoundation\", 2);    IntPtr symbol = dlsym(handle, \"kCFRunLoopCommonModes\");    return Marshal.ReadIntPtr(symbol);}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0411\u0435\u0437 \u044d\u0442\u043e\u0433\u043e \u0445\u0443\u043a \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0440\u0435\u0436\u0438\u043c\u0430\u0445 runloop.<\/p>\n<hr\/>\n<h3>Linux: \u043e\u043f\u0440\u043e\u0441 XQueryKeymap \u043d\u0430 60 FPS<\/h3>\n<p>\u041d\u0430 Linux \u0441 X11 \u043d\u0435\u0442 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0445\u0443\u043a\u0430 \u0432 \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0438. \u0417\u0430\u0442\u043e \u0435\u0441\u0442\u044c <code>XQueryKeymap<\/code> \u2014 \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0430\u044f \u0431\u0438\u0442\u043e\u0432\u0443\u044e \u043a\u0430\u0440\u0442\u0443 \u0438\u0437 256 \u0431\u0438\u0442 (32 \u0431\u0430\u0439\u0442\u0430), \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0431\u0438\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e \u043e\u0434\u043d\u043e\u0439 \u043a\u043b\u0430\u0432\u0438\u0448\u0438. \u041e\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c \u0435\u0451 \u0432 \u0444\u043e\u043d\u043e\u0432\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 ~60 \u0440\u0430\u0437 \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443 \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0441 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c:<\/p>\n<pre><code>internal class LinuxKeyboardHook : IKeyboardHook, IDisposable{    private byte[] _previousKeys = new byte[32];    [DllImport(\"libX11.so.6\")]    private static extern bool XQueryKeymap(IntPtr display, byte[] keys);    [DllImport(\"libXtst.so.6\")]    private static extern int XTestFakeKeyEvent(IntPtr display, uint keycode, bool press, uint delay);    private void KeymapPollingLoop()    {        while (_running)        {            byte[] currentKeys = new byte[32];            if (XQueryKeymap(_display, currentKeys))                ProcessKeymapChanges(currentKeys);            Thread.Sleep(16); \/\/ ~60 FPS        }    }    private void ProcessKeymapChanges(byte[] currentKeys)    {        for (int i = 0; i &lt; 32; i++)        {            byte current  = currentKeys[i];            byte previous = _previousKeys[i];            if (current == previous) continue;            for (int bit = 0; bit &lt; 8; bit++)            {                bool wasPressed = (previous &amp; (1 &lt;&lt; bit)) != 0;                bool isPressed  = (current  &amp; (1 &lt;&lt; bit)) != 0;                int keyCode = i * 8 + bit;                if (isPressed &amp;&amp; !wasPressed)                    KeyDown?.Invoke(KeyboardKeyExtensions.FromPlatformCode(keyCode));                else if (!isPressed &amp;&amp; wasPressed)                    KeyUp?.Invoke(KeyboardKeyExtensions.FromPlatformCode(keyCode));            }        }        Array.Copy(currentKeys, _previousKeys, 32);    }    public void SendKey(KeyboardKey key)    {        XTestFakeKeyEvent(_display, (uint)KeyboardKeyExtensions.ToPlatformCode(key), true, 0);        XTestFakeKeyEvent(_display, (uint)KeyboardKeyExtensions.ToPlatformCode(key), false, 0);        XFlush(_display);    }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041f\u043e\u0434\u0445\u043e\u0434 \u0441 \u043e\u043f\u0440\u043e\u0441\u043e\u043c \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0443 \u0434\u043e 16 \u043c\u0441, \u0437\u0430\u0442\u043e \u043e\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0431\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u0430\u0432 \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432\u043e \u0432\u0441\u0435\u0445 \u0434\u0438\u0441\u0442\u0440\u0438\u0431\u0443\u0442\u0438\u0432\u0430\u0445 \u0441 X11.<\/p>\n<blockquote>\n<p><strong>\u0412\u0430\u0436\u043d\u043e:<\/strong> Wayland \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u2014 \u0442\u0430\u043c \u043d\u0435\u0442 \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u0430 <code>XQueryKeymap<\/code>, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0433\u043e \u0438\u0437 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0431\u0435\u0437 \u043f\u0440\u0438\u0432\u0438\u043b\u0435\u0433\u0438\u0439.<\/p>\n<\/blockquote>\n<hr\/>\n<h3>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435: \u0432\u0441\u0451 \u0443\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a<\/h3>\n<p>\u041f\u043e\u0434\u043f\u0438\u0441\u043a\u0430 \u043d\u0430 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f:<\/p>\n<pre><code>var hook = KeyboardHookFactory.Create();hook.KeyDown += key =&gt; Console.WriteLine($\"\u041d\u0430\u0436\u0430\u0442\u0430: {key}\");hook.KeyUp   += key =&gt; Console.WriteLine($\"\u041e\u0442\u043f\u0443\u0449\u0435\u043d\u0430: {key}\");<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u042d\u043c\u0443\u043b\u044f\u0446\u0438\u044f \u043e\u0434\u0438\u043d\u043e\u0447\u043d\u043e\u0433\u043e \u043d\u0430\u0436\u0430\u0442\u0438\u044f:<\/p>\n<pre><code>hook.SendKey(KeyboardKey.A);<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041e\u0442\u043f\u0440\u0430\u0432\u043a\u0430 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438 (\u043a\u043b\u0430\u0432\u0438\u0448\u0438 \u043d\u0430\u0436\u0438\u043c\u0430\u044e\u0442\u0441\u044f \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443, \u043e\u0442\u043f\u0443\u0441\u043a\u0430\u044e\u0442\u0441\u044f \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c):<\/p>\n<pre><code>hook.SendKeyCombo(KeyboardKey.LControl, KeyboardKey.C); \/\/ Ctrl+Chook.SendKeyCombo(KeyboardKey.LControl, KeyboardKey.LShift, KeyboardKey.Escape); \/\/ \u0414\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440 \u0437\u0430\u0434\u0430\u0447<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041c\u044b\u0448\u044c:<\/p>\n<pre><code>var mouse = MouseHookFactory.Create();mouse.ButtonDown += btn =&gt; Console.WriteLine($\"\u041a\u043d\u043e\u043f\u043a\u0430 \u043c\u044b\u0448\u0438: {btn}\");mouse.SendButton(MouseButton.Left); \/\/ \u043a\u043b\u0438\u043a \u043b\u0435\u0432\u043e\u0439<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432:<\/p>\n<pre><code>if (hook is IDisposable d) d.Dispose();<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<hr\/>\n<h3>\u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0441 Avalonia<\/h3>\n<pre><code>public class MainWindowViewModel : ViewModelBase{    private readonly IKeyboardHook _hook;    public MainWindowViewModel()    {        _hook = KeyboardHookFactory.Create();        _hook.KeyDown += OnGlobalKeyDown;    }    private void OnGlobalKeyDown(KeyboardKey key)    {        \/\/ \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0432 UI-\u043f\u043e\u0442\u043e\u043a\u0435, \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0431\u0438\u043d\u0434\u0438\u043d\u0433\u0438        Dispatcher.UIThread.Post(() =&gt;        {            LastKey = key.ToString();        });    }    public string LastKey { get; private set; }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0425\u0443\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u0430\u0436\u0435 \u043a\u043e\u0433\u0434\u0430 \u043e\u043a\u043d\u043e \u0441\u0432\u0451\u0440\u043d\u0443\u0442\u043e.<\/p>\n<hr\/>\n<h3>\u0427\u0442\u043e \u0434\u0430\u043b\u044c\u0448\u0435<\/h3>\n<p>\u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0436\u0438\u0432\u0451\u0442 \u043d\u0430 <a href=\"https:\/\/github.com\/c3n9\/KeyboardHook\" rel=\"noopener noreferrer nofollow\">GitHub<\/a> \u043f\u043e\u0434 \u043b\u0438\u0446\u0435\u043d\u0437\u0438\u0435\u0439 MIT. \u041f\u0430\u043a\u0435\u0442 \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043d \u043d\u0430 <a href=\"https:\/\/www.nuget.org\/packages\/KeyboardHook\/\" rel=\"noopener noreferrer nofollow\">NuGet<\/a>.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u043b\u0451\u0433\u043a\u0438\u0439, \u043f\u0440\u043e\u0437\u0440\u0430\u0447\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a \u0432\u0432\u043e\u0434\u0430 \u0431\u0435\u0437 \u0442\u044f\u0436\u0451\u043b\u044b\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u2014 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435, \u0438 \u043d\u0435 \u0441\u0442\u0435\u0441\u043d\u044f\u0439\u0442\u0435\u0441\u044c \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0442\u044c issues \u0438 \u0434\u0435\u043b\u0430\u0442\u044c \u0444\u043e\u0440\u043a\u0438.<\/p>\n<hr\/>\n<p><em>\u0411\u0443\u0434\u0443 \u0440\u0430\u0434 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u043c \u2014 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043e\u0442 \u0442\u0435\u0445, \u043a\u0442\u043e \u0440\u0435\u0448\u0430\u043b \u043f\u043e\u0445\u043e\u0436\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u043d\u0430 Wayland.<\/em><\/p>\n<\/div>\n<p>\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/1040932\/\">https:\/\/habr.com\/ru\/articles\/1040932\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430: \u043d\u0438 \u043e\u0434\u0438\u043d \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043f\u0430\u043a\u0435\u0442 \u043d\u0435 \u043f\u043e\u0434\u043e\u0448\u0451\u043b\u041f\u0435\u0440\u0432\u044b\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0430\u043a\u0435\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b, \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0430 Windows \u0438 \u043c\u043e\u043b\u0447\u0430\u043b\u0438 \u043d\u0430 macOS. \u0414\u0440\u0443\u0433\u0438\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u0435\u0448\u0430\u043b\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u043d\u0430\u0436\u0430\u0442\u0438\u0438 \u043a\u043b\u0430\u0432\u0438\u0448\u0438. \u042f \u0438\u0441\u043a\u0430\u043b \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u00ab\u0442\u043e\u043b\u044c\u043a\u043e Windows\u00bb \u043c\u0435\u043d\u044f \u043d\u0435 \u0443\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u043b\u043e.\u0415\u0441\u043b\u0438 \u0447\u0435\u0433\u043e-\u0442\u043e \u043d\u0435\u0442, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c? \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u043c\u0443. \u041f\u043e \u043d\u0430\u0447\u0430\u043b\u0443 \u0432\u0441\u0451 \u0431\u044b\u043b\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u0434\u0430\u0436\u0435 \u0431\u0435\u0437 \u0432\u044b\u043d\u0435\u0441\u0435\u043d\u0438\u044f \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443, \u043d\u043e \u0447\u0443\u0442\u044c \u043f\u043e\u0437\u0436\u0435, \u043a\u043e\u0433\u0434\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u0441\u044f \u0438 \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u043c\u043e\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445, \u043f\u043e\u0434\u0443\u043c\u0430\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0441 \u043d\u0443\u043b\u044f. \u0421\u0430\u043c\u043e\u0435 \u0433\u043b\u0430\u0432\u043d\u043e\u0435 \u0447\u0435\u0433\u043e \u044f \u0445\u043e\u0442\u0435\u043b \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f: \u0431\u0435\u0437 \u043b\u0438\u0448\u043d\u0438\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439, \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u043e\u0441\u0442\u044c, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u0447\u0435\u0432\u0430\u0442\u044c \u043c\u0435\u0436\u0434\u0443 pet-\u043f\u0440\u043e\u0435\u043a\u0442\u0430\u043c\u0438. \u041f\u043e \u0445\u043e\u0434\u0443 \u0440\u0435\u0448\u0438\u043b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0435 open source, \u0447\u0442\u043e\u0431\u044b \u043d\u0435\u043c\u0438\u043d\u0443\u0435\u043c\u043e \u0443\u043b\u0443\u0447\u0448\u0430\u0442\u044c \u0438 \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435.KeyboardHook &#8212; \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u044b\u0439 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u0445\u0443\u043a \u043a\u043b\u0430\u0432\u0438\u0430\u0442\u0443\u0440\u044b \u0438 \u043c\u044b\u0448\u0438 \u0434\u043b\u044f .NET Standard 2.0.\u0427\u0442\u043e \u0443\u043c\u0435\u0435\u0442 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u0413\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442 \u043d\u0430\u0436\u0430\u0442\u0438\u0439 \u0438 \u043e\u0442\u043f\u0443\u0441\u043a\u0430\u043d\u0438\u0439 \u043a\u043b\u0430\u0432\u0438\u0448 \u2014 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u0430\u0436\u0435 \u043a\u043e\u0433\u0434\u0430 \u0432\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0435 \u0432 \u0444\u043e\u043a\u0443\u0441\u0435\u0413\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u043c\u044b\u0448\u0438 (Left, Right, Middle)\u041f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u0430\u044f \u044d\u043c\u0443\u043b\u044f\u0446\u0438\u044f \u043d\u0430\u0436\u0430\u0442\u0438\u0439 \u043a\u043b\u0430\u0432\u0438\u0448 \u0438 \u043a\u043d\u043e\u043f\u043e\u043a \u043c\u044b\u0448\u0438\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439 \u043a\u043b\u0430\u0432\u0438\u0448 (Ctrl+C, Alt+F4 \u0438 \u043b\u044e\u0431\u044b\u0445 \u0434\u0440\u0443\u0433\u0438\u0445)\u0422\u0440\u0438 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u044b: Windows x86\/x64, macOS Arm64, Linux x64 (X11)\u041d\u0435\u0442 \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u2014 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 .NET \u0438 P\/Invoke \u043a \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u043c\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u043e\u0439:dotnet add package KeyboardHook\u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430: \u043e\u0434\u0438\u043d \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u0442\u0440\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438\u041a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u2014 \u043f\u043e\u043b\u043d\u043e\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u043e\u0433\u043e API \u0438 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430. \u0421\u043d\u0430\u0440\u0443\u0436\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432\u0438\u0434\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u0432\u0430 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430:public interface IKeyboardHook{    event Action&lt;KeyboardKey&gt; KeyDown;    event Action&lt;KeyboardKey&gt; KeyUp;    void SendKey(KeyboardKey key);    void SendKeyCombo(params KeyboardKey[] keys);}public interface IMouseHook : IDisposable{    event Action&lt;MouseButton&gt; ButtonDown;    event Action&lt;MouseButton&gt; ButtonUp;    void SendButton(MouseButton button);    void SendButtonCombo(params MouseButton[] buttons);}\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0443\u0436\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u2014 \u043e\u0434\u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0430, \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435:IKeyboardHook keyboard = KeyboardHookFactory.Create();IMouseHook mouse = MouseHookFactory.Create();\u0424\u0430\u0431\u0440\u0438\u043a\u0430 \u0432\u043d\u0443\u0442\u0440\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:public static class KeyboardHookFactory{    public static IKeyboardHook Create()    {        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))            return new WindowsKeyboardHook();        if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))            return new LinuxKeyboardHook();        if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))            return new MacKeyboardHook();        throw new PlatformNotSupportedException(&#171;Unsupported platform&#187;);    }}\u041c\u0430\u043f\u043f\u0438\u043d\u0433 \u043a\u043b\u0430\u0432\u0438\u0448: \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b \u0432\u043c\u0435\u0441\u0442\u043e switch-\u043f\u043e\u0440\u0442\u044f\u043d\u043e\u043a\u041a\u0430\u0436\u0434\u0430\u044f \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0441\u0432\u043e\u0438 \u043a\u043e\u0434\u044b \u043a\u043b\u0430\u0432\u0438\u0448. \u041d\u0430 Windows \u044d\u0442\u043e Virtual Key Codes, \u043d\u0430 Linux \u2014 X11 keycodes, \u043d\u0430 macOS \u2014 Virtual Key \u043a\u043e\u0434\u044b CoreGraphics. \u0412\u043c\u0435\u0441\u0442\u043e \u0442\u0440\u0451\u0445 \u0433\u0438\u0433\u0430\u043d\u0442\u0441\u043a\u0438\u0445 switch-\u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u044f \u0440\u0435\u0448\u0438\u043b \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043a\u043e\u0434\u044b \u043f\u0440\u044f\u043c\u043e \u0432 enum \u0447\u0435\u0440\u0435\u0437 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b:public enum KeyboardKey{    [WindowsCode(0x41)]    [LinuxCode(38)]    [MacosCode(0)]    A,    [WindowsCode(0x1B)]    [LinuxCode(9)]    [MacosCode(53)]    Escape,    [WindowsCode(0x70)]    [LinuxCode(67)]    [MacosCode(122)]    F1,    \/\/ &#8230; 150+ \u043a\u043b\u0430\u0432\u0438\u0448}\u0410\u0442\u0440\u0438\u0431\u0443\u0442\u044b \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u044b:[AttributeUsage(AttributeTargets.Field)]public class WindowsCodeAttribute : Attribute{    public int Code { get; }    public WindowsCodeAttribute(int code) =&gt; Code = code;}\u041a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u044f \u0432 \u043e\u0431\u0435 \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0447\u0435\u0440\u0435\u0437 \u0440\u0435\u0444\u043b\u0435\u043a\u0441\u0438\u044e:internal static int ToPlatformCode(this KeyboardKey key){    var field = typeof(KeyboardKey).GetField(key.ToString());    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))        return field?.GetCustomAttribute&lt;WindowsCodeAttribute&gt;()?.Code ?? 0;    if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))        return field?.GetCustomAttribute&lt;LinuxCodeAttribute&gt;()?.Code ?? 0;    if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))        return field?.GetCustomAttribute&lt;MacosCodeAttribute&gt;()?.Code ?? 0;    return 0;}internal static KeyboardKey FromPlatformCode(int platformCode){    foreach (var field in typeof(KeyboardKey).GetFields())    {        if (field.FieldType != typeof(KeyboardKey)) continue;        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))        {            var attr = field.GetCustomAttribute&lt;WindowsCodeAttribute&gt;();            if (attr?.Code == platformCode)                return (KeyboardKey)field.GetValue(null);        }        \/\/ &#8230; \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0434\u043b\u044f Linux \u0438 macOS    }    return KeyboardKey.None;}\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u0443\u044e \u043a\u043b\u0430\u0432\u0438\u0448\u0443 \u0442\u0435\u043f\u0435\u0440\u044c \u0437\u043d\u0430\u0447\u0438\u0442 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043e\u0434\u043d\u0443 \u0437\u0430\u043f\u0438\u0441\u044c \u0432 enum \u0441 \u0442\u0440\u0435\u043c\u044f \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u043c\u0438 \u2014 \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u0442\u0440\u043e\u0433\u0430\u0442\u044c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e.Windows: WH_KEYBOARD_LL\u041d\u0430 Windows \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u043f\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043d\u0438\u0437\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u044b\u0439 \u0445\u0443\u043a WH_KEYBOARD_LL \u2014 \u044d\u0442\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c Win32 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430 \u043a\u043b\u0430\u0432\u0438\u0448 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u044b:internal class WindowsKeyboardHook : IKeyboardHook, IDisposable{    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);    private const int WH_KEYBOARD_LL = 13;    private const int WM_KEYDOWN    = 0x0100;    private const int WM_KEYUP      = 0x0101;    private const int WM_SYSKEYDOWN = 0x0104;    private const int WM_SYSKEYUP   = 0x0105;    private readonly HashSet&lt;KeyboardKey&gt; _pressedKeys = new HashSet&lt;KeyboardKey&gt;();    public event Action&lt;KeyboardKey&gt; KeyDown;    public event Action&lt;KeyboardKey&gt; KeyUp;    public WindowsKeyboardHook()    {        _proc = HookCallback;        _hookId = SetHook(_proc);    }    private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)    {        if (nCode &gt;= 0)        {            var kb = Marshal.PtrToStructure&lt;KBDLLHOOKSTRUCT&gt;(lParam);            var key = KeyboardKeyExtensions.FromPlatformCode(kb.vkCode);            if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)            {                _pressedKeys.Add(key);                KeyDown?.Invoke(key);            }            else if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)            {                _pressedKeys.Remove(key);                KeyUp?.Invoke(key);            }        }        return CallNextHookEx(_hookId, nCode, wParam, lParam);    }    public void SendKey(KeyboardKey key)    {        var code = (byte)KeyboardKeyExtensions.ToPlatformCode(key);        keybd_event(code, 0, 0, UIntPtr.Zero);        keybd_event(code, 0, KEYEVENTF_KEYUP, UIntPtr.Zero);    }    public void SendKeyCombo(params KeyboardKey[] keys)    {        foreach (var k in keys)            keybd_event((byte)KeyboardKeyExtensions.ToPlatformCode(k), 0, 0, UIntPtr.Zero);        for (int i = keys.Length &#8212; 1; i &gt;= 0; i&#8212;)            keybd_event((byte)KeyboardKeyExtensions.ToPlatformCode(keys[i]), 0, KEYEVENTF_KEYUP, UIntPtr.Zero);    }    [DllImport(&#171;user32.dll&#187;)] private static extern IntPtr SetWindowsHookEx(&#8230;);    [DllImport(&#171;user32.dll&#187;)] private static extern IntPtr CallNextHookEx(&#8230;);    [DllImport(&#171;user32.dll&#187;)] private static extern void keybd_event(&#8230;);    \/\/ &#8230;}\u0412\u0430\u0436\u043d\u0430\u044f \u0434\u0435\u0442\u0430\u043b\u044c: SYSKEYDOWN\/SYSKEYUP \u2014 \u044d\u0442\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043a\u043b\u0430\u0432\u0438\u0448, \u043d\u0430\u0436\u0430\u0442\u044b\u0445 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 Alt (\u0438\u043b\u0438 \u0435\u0441\u043b\u0438 \u043e\u043a\u043d\u043e \u043d\u0435 \u0432 \u0444\u043e\u043a\u0443\u0441\u0435). \u0411\u0435\u0437 \u043d\u0438\u0445 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u0435 \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u0443 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0439.macOS: CGEventTap + CoreFoundation RunLoop\u041d\u0430 macOS \u0432\u0441\u0451 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0435\u0435. \u0421\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u2014 CGEventTap \u0438\u0437 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 CoreGraphics. \u041e\u043d \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u00ab\u0442\u043e\u0447\u043a\u0443 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430\u00bb \u0432 \u043f\u043e\u0442\u043e\u043a\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0441\u0435\u0441\u0441\u0438\u0438 \u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0439 Accessibility (\u0438\u043d\u0430\u0447\u0435 CGEventTapCreate \u0432\u0435\u0440\u043d\u0451\u0442 NULL):internal class MacKeyboardHook : IKeyboardHook, IDisposable{    private const int kCGEventKeyDown        = 10;    private const int kCGEventKeyUp          = 11;    private const int kCGKeyboardEventKeycode = 9;    private const int kCGSessionEventTap     = 1;    private CGEventTapCallBack _callbackKeepAlive; \/\/ \u0443\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c \u0434\u0435\u043b\u0435\u0433\u0430\u0442 \u043e\u0442 GC!    private IntPtr _eventTap;    private IntPtr _runLoop;    private void InitializeEventTap()    {        _callbackKeepAlive = EventCallback;        var eventMask = (1UL &lt;&lt; kCGEventKeyDown) | (1UL &lt;&lt; kCGEventKeyUp);        _eventTap = CGEventTapCreate(            kCGSessionEventTap,            place:   1,   \/\/ HeadInsert            options: 1,   \/\/ Default            eventsOfInterest: eventMask,            callback: _callbackKeepAlive,            userInfo: IntPtr.Zero        );        if (_eventTap == IntPtr.Zero)            throw new UnauthorizedAccessException(                &#171;Accessibility permissions are required to intercept keys.&#187;);        _runLoopSource = CFMachPortCreateRunLoopSource(IntPtr.Zero, _eventTap, 0);        CGEventTapEnable(_eventTap, true);        StartRunLoop();    }    private void StartRunLoop()    {        var thread = new Thread(() =&gt;        {            _runLoop = CFRunLoopGetCurrent();            IntPtr modes = GetCFRunLoopCommonModes(); \/\/ \u0447\u0435\u0440\u0435\u0437 dlopen\/dlsym            CFRunLoopAddSource(_runLoop, _runLoopSource, modes);            CFRunLoopRun(); \/\/ \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u0442\u043e\u043a, \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u044f        })        {            IsBackground = true,            Name = &#171;MacKeyboardHook Loop&#187;        };        thread.Start();    }    private IntPtr EventCallback(IntPtr proxy, int type, IntPtr eventRef, IntPtr userInfo)    {        if (type == kCGEventKeyDown || type == kCGEventKeyUp)        {            long macKeyCode = CGEventGetIntegerValueField(eventRef, kCGKeyboardEventKeycode);            var key = KeyboardKeyExtensions.FromPlatformCode((int)macKeyCode);            if (type == kCGEventKeyDown) KeyDown?.Invoke(key);            else                         KeyUp?.Invoke(key);        }        return eventRef;    }}\u041e\u0434\u043d\u0430 \u043d\u0435\u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u0435\u0449\u044c: kCFRunLoopCommonModes \u2014 \u044d\u0442\u043e \u043d\u0435 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430, \u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0432\u043e \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0435. \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0435\u0440\u0435\u0437 dlopen\/dlsym:private static IntPtr GetCFRunLoopCommonModes(){    IntPtr handle = dlopen(        &#171;\/System\/Library\/Frameworks\/CoreFoundation.framework\/CoreFoundation&#187;, 2);    IntPtr symbol = dlsym(handle, &#171;kCFRunLoopCommonModes&#187;);    return Marshal.ReadIntPtr(symbol);}\u0411\u0435\u0437 \u044d\u0442\u043e\u0433\u043e \u0445\u0443\u043a \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0440\u0435\u0436\u0438\u043c\u0430\u0445 runloop.Linux: \u043e\u043f\u0440\u043e\u0441 XQueryKeymap \u043d\u0430 60 FPS\u041d\u0430 Linux \u0441 X11 \u043d\u0435\u0442&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-481491","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/481491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=481491"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/481491\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=481491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=481491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=481491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}