{"id":17259588,"url":"https://github.com/jnm2/lowlevelhooking","last_synced_at":"2025-04-14T06:21:30.629Z","repository":{"id":86546342,"uuid":"76748425","full_name":"jnm2/LowLevelHooking","owner":"jnm2","description":"Windows low level keyboard hooking component","archived":false,"fork":false,"pushed_at":"2017-02-05T04:55:35.000Z","size":24,"stargazers_count":15,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-27T20:06:45.454Z","etag":null,"topics":["dotnet","dotnetcore","hook","keyboard","low-level","windows"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jnm2.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-12-17T21:47:41.000Z","updated_at":"2024-10-08T14:06:02.000Z","dependencies_parsed_at":"2023-07-09T06:01:38.344Z","dependency_job_id":null,"html_url":"https://github.com/jnm2/LowLevelHooking","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jnm2%2FLowLevelHooking","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jnm2%2FLowLevelHooking/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jnm2%2FLowLevelHooking/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jnm2%2FLowLevelHooking/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jnm2","download_url":"https://codeload.github.com/jnm2/LowLevelHooking/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248830884,"owners_count":21168361,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dotnet","dotnetcore","hook","keyboard","low-level","windows"],"created_at":"2024-10-15T07:45:33.790Z","updated_at":"2025-04-14T06:21:30.619Z","avatar_url":"https://github.com/jnm2.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Low Level Hooking for .NET\n\n[![NuGet](https://img.shields.io/nuget/v/LowLevelHooking.svg)](https://www.nuget.org/packages/LowLevelHooking/1.0.1)\n[![Gitter](https://img.shields.io/gitter/room/jnm2/LowLevelHooking.svg)](https://gitter.im/jnm2/LowLevelHooking)\n\nThis library is intended to serve as a reference implementation for Windows low\nlevel keyboard hooks. (And mouse hooks, if there is interest.) Very often I see\nimplementations floating around containing noob mistakes, and let's be honest-\nshould you actually *have* to roll your own implementation and spend time in the\nsame pitfalls, just to do something this ordinary? I have, several times over the\nyears, and I want to contribute back what I believe is the most optimal\nimplementation. If you spot something that could be done more optimally, please\ndon't hesitate to [comment](https://github.com/jnm2/LowLevelHooking/issues)!\n\n\n## Get up and running\n\n 1. Add a [LowLevelHooking](https://www.nuget.org/packages/LowLevelHooking/1.0.1)\n    NuGet package reference to your project.\n\n 2. Windows low level hooks communicate with your process by sending a message to\n    your thread message pump. That means you'll only be able to receive low level\n    notifications while running\n    [`Application.Run`](https://msdn.microsoft.com/en-us/library/system.windows.forms.application.run.aspx)\n    (already in place if you're using Windows Forms) or\n    [`Dispatcher.PushFrame`](https://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.pushframe.aspx)\n    (already in place if you're using WPF). SharpDX has\n    [`RenderLoop.Run`](https://github.com/sharpdx/SharpDX/blob/master/Source/SharpDX.Desktop/RenderLoop.cs).\n    Any UI framework will be running something similar.\n\n    \u003e If you're not running UI at all, for example if you're\n      a console app, you'll have to implement your own message loop. This is rare,\n      but I do have a .NET Core console app sample planned, so stay tuned.)\n\n    With this in mind, and because you want to create as _few_ hooks as possible\n    (read: one), it makes the most logical sense to tie the lifetime of the hook to\n    the lifetime of the message loop rather than to a window:\n\n    ```c#    \n    public static class Program\n    {\n        public static GlobalKeyboardHook GlobalKeyboardHook { get; private set; }\n        \n        [STAThread]\n        public static void Main()\n        {\n            Application.EnableVisualStyles();\n            Application.SetCompatibleTextRenderingDefault(false);\n            \n            using (GlobalKeyboardHook = new GlobalKeyboardHook())\n            {\n                Application.Run(new Form1());\n            }\n        }\n    }\n    ```\n    (See sample [Program.cs](./src/Samples/WinForms/Program.cs))\n\n3. Once that's taken care of, you can subscribe and unsubscribe as needed:\n\n    ```c#\n    public partial class Form1 : Form\n    {\n        public Form1()\n        {\n            InitializeComponent();\n            Program.GlobalKeyboardHook.KeyDownOrUp += GlobalKeyboardHook_KeyDownOrUp;\n            Disposed += MainView_Disposed;\n        }\n\n        private void MainView_Disposed(object sender, EventArgs e)\n        {\n            // This isn't strictly necessary since Form1 does not have a shorter lifetime\n            // than the whole application, but just in case something should change...\n            // Unsubscribing allows the garbage collector to free everything associated with Form1\n            // and of course, stops doing unnecessary work on each keypress system-wide.\n            Program.GlobalKeyboardHook.KeyDownOrUp -= GlobalKeyboardHook_KeyDownOrUp;\n        }\n\n        private void GlobalKeyboardHook_KeyDownOrUp(object sender, GlobalKeyboardHookEventArgs e)\n        {\n            Debug.WriteLine($\"{e.KeyCode} {(e.IsUp ? \"up\" : \"down\")}\");\n        }\n    }\n    ```\n    (See sample [MainView.cs](./src/Samples/WinForms/MainView.cs))\n\n    And that's it!\n\n\n## Feedback\n\nIf you have questions, critiques or contributions, I can't wait to hear from you via\n[Gitter](https://gitter.im/jnm2/LowLevelHooking) or \n[GitHub issues](https://github.com/jnm2/LowLevelHooking/issues)!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjnm2%2Flowlevelhooking","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjnm2%2Flowlevelhooking","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjnm2%2Flowlevelhooking/lists"}