Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1056485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:43:17+00:00 2026-05-16T17:43:17+00:00

Does anyone know how to use the RegisterHotKey/UnregisterHotKey API calls in a console application?

  • 0

Does anyone know how to use the RegisterHotKey/UnregisterHotKey API calls in a console application? I assume that setting up/removing the hotkey is the same, but how do I get the call back when the key was pressed?

Every example I see is for Winforms, and uses protected override void WndProc(ref Message m){...}, which isn’t available to me.


update: what I have is below, but the event is never hit. I thought it could be because when you load ConsoleShell it does block further execution, but even if I put SetupHotkey into a different thread nothing happens. Any thoughts?

class Program
{
    static void Main(string[] args)
    {
        new Hud().Init(args);
    }
}

class Hud
{
    int keyHookId;


    public void Init(string[] args)
    {
        SetupHotkey();
        InitPowershell(args);
        Cleanup();
    }

    private void Cleanup()
    {
        HotKeyManager.UnregisterHotKey(keyHookId);
    }

    private void SetupHotkey()
    {
        keyHookId = HotKeyManager.RegisterHotKey(Keys.Oemtilde, KeyModifiers.Control);
        HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed);
    }

    void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e)
    {
        //never executed
        System.IO.File.WriteAllText("c:\\keyPressed.txt", "Hotkey pressed");
    }

    private static void InitPowershell(string[] args)
    {
        var config = RunspaceConfiguration.Create();
        ConsoleShell.Start(config, "", "", args);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:43:18+00:00Added an answer on May 16, 2026 at 5:43 pm

    What you can do is Create a hidden window in your Console application which is used to handle the hotkey notification and raise an event.

    The code HERE demonstrates the principal. HERE is an article on handling messages in a Console application, using this you should be able to enhance HotKeyManager to run in a Console Application.

    The following update to the HotKeyManager creates a background thread which runs the message loop and handles the windows messages.

    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Threading;
    
    namespace ConsoleHotKey
    {
      public static class HotKeyManager
      {
        public static event EventHandler<HotKeyEventArgs> HotKeyPressed;
    
        public static int RegisterHotKey(Keys key, KeyModifiers modifiers)
        {
          _windowReadyEvent.WaitOne();
          int id = System.Threading.Interlocked.Increment(ref _id);
          _wnd.Invoke(new RegisterHotKeyDelegate(RegisterHotKeyInternal), _hwnd, id, (uint)modifiers, (uint)key);
          return id;
        }
    
        public static void UnregisterHotKey(int id)
        {
          _wnd.Invoke(new UnRegisterHotKeyDelegate(UnRegisterHotKeyInternal), _hwnd, id);
        }
    
        delegate void RegisterHotKeyDelegate(IntPtr hwnd, int id, uint modifiers, uint key);
        delegate void UnRegisterHotKeyDelegate(IntPtr hwnd, int id);
    
        private static void RegisterHotKeyInternal(IntPtr hwnd, int id, uint modifiers, uint key)
        {      
          RegisterHotKey(hwnd, id, modifiers, key);      
        }
    
        private static void UnRegisterHotKeyInternal(IntPtr hwnd, int id)
        {
          UnregisterHotKey(_hwnd, id);
        }    
    
        private static void OnHotKeyPressed(HotKeyEventArgs e)
        {
          if (HotKeyManager.HotKeyPressed != null)
          {
            HotKeyManager.HotKeyPressed(null, e);
          }
        }
    
        private static volatile MessageWindow _wnd;
        private static volatile IntPtr _hwnd;
        private static ManualResetEvent _windowReadyEvent = new ManualResetEvent(false);
        static HotKeyManager()
        {
          Thread messageLoop = new Thread(delegate()
            {
              Application.Run(new MessageWindow());
            });
          messageLoop.Name = "MessageLoopThread";
          messageLoop.IsBackground = true;
          messageLoop.Start();      
        }
    
        private class MessageWindow : Form
        {
          public MessageWindow()
          {
            _wnd = this;
            _hwnd = this.Handle;
            _windowReadyEvent.Set();
          }
    
          protected override void WndProc(ref Message m)
          {
            if (m.Msg == WM_HOTKEY)
            {
              HotKeyEventArgs e = new HotKeyEventArgs(m.LParam);
              HotKeyManager.OnHotKeyPressed(e);
            }
    
            base.WndProc(ref m);
          }
    
          protected override void SetVisibleCore(bool value)
          {
            // Ensure the window never becomes visible
            base.SetVisibleCore(false);
          }
    
          private const int WM_HOTKEY = 0x312;
        }
    
        [DllImport("user32", SetLastError=true)]
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
    
        [DllImport("user32", SetLastError = true)]
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
    
        private static int _id = 0;
      }
    
    
      public class HotKeyEventArgs : EventArgs
      {
        public readonly Keys Key;
        public readonly KeyModifiers Modifiers;
    
        public HotKeyEventArgs(Keys key, KeyModifiers modifiers)
        {
          this.Key = key;
          this.Modifiers = modifiers;
        }
    
        public HotKeyEventArgs(IntPtr hotKeyParam)
        {
          uint param = (uint)hotKeyParam.ToInt64();
          Key = (Keys)((param & 0xffff0000) >> 16);
          Modifiers = (KeyModifiers)(param & 0x0000ffff);
        }
      }
    
      [Flags]
      public enum KeyModifiers
      {
        Alt = 1,
        Control = 2,
        Shift = 4,
        Windows = 8,
        NoRepeat = 0x4000
      }
    }
    

    Here is an example of using HotKeyManager from a Console application

    using System;
    using System.Windows.Forms;
    
    namespace ConsoleHotKey
    {
      class Program
      {
        static void Main(string[] args)
        {
          HotKeyManager.RegisterHotKey(Keys.A, KeyModifiers.Alt);
          HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed);
          Console.ReadLine();      
        }
    
        static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e)
        {
          Console.WriteLine("Hit me!");
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of an application (hosted or otherwise) that I could use to
Does anyone know any good tool that I can use to perform stress tests
Does anyone know of an algorithm that I could use to find an interesting
Does anyone know of some good tutorials that explain how to use the JQuery
Does anyone know how to use the Google safe browsing API. I downloaded the
Does anyone know of any samples that use the DataTables jquery plugin with a
Does anyone know how to use the credential cache or network credential to get
Does anyone know how to use CruiseControl.Net to publish to an FTP server?
Does anyone know how to use the Raw Input facility on Windows from a
Does anyone know if it's possible to use regex capture within Apache's DirectoryMatch directive?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.