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

  • SEARCH
  • Home
  • 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 9018747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:36:30+00:00 2026-06-16T04:36:30+00:00

Ive seen many solutions online but none does exactly what I want. What is

  • 0

Ive seen many solutions online but none does exactly what I want. What is the best/simplest way to get any keys pressed in a given process (not my console applicaton) while my application is running in background. I dont need the modifiers or anything.

  • 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-06-16T04:36:31+00:00Added an answer on June 16, 2026 at 4:36 am

    If you don’t particularly care which process the keys are being pressed in the easiest method would be to call GetAsyncKeyState. It’s rather limited though as it does not hook the keyboard and requires you to call it continuously. The best approach in my opinion is to hook the keyboard.

    Using SetWindowsHookEx you can actually explicitly specify the identifier of the thread with which the hook procedure is to be associated so you can hook keys for a specific process (see dwThreadId).

    Here’s a class that you can use (originally found on a Micrsoft blog but I cannot seem to find the authors name at the moment!)

    public delegate IntPtr KeyboardProcess(int nCode, IntPtr wParam, IntPtr lParam);
    
    public sealed class KeyboardHook
    {
        public static event EventHandler<KeyPressedEventArgs> KeyPressed;
        private const int WH_KEYBOARD = 13;
        private const int WM_KEYDOWN = 0x0100;
        private static KeyboardProcess keyboardProc = HookCallback;
        private static IntPtr hookID = IntPtr.Zero;
    
        public static void CreateHook()
        {
            hookID = SetHook(keyboardProc);
        }
    
        public static void DisposeHook()
        {
            UnhookWindowsHookEx(hookID);
        }
    
        private static IntPtr SetHook(KeyboardProcess keyboardProc)
        {
            using (Process currentProcess = Process.GetCurrentProcess())
            using (ProcessModule currentProcessModule = currentProcess.MainModule)
            {
                return SetWindowsHookEx(WH_KEYBOARD, keyboardProc, GetModuleHandle(currentProcessModule.ModuleName), 0);
            }
        }
    
        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
            {
                int vkCode = Marshal.ReadInt32(lParam);
    
                if (KeyPressed != null)
                    KeyPressed(null, new KeyPressedEventArgs((Keys)vkCode));
            }
            return CallNextHookEx(hookID, nCode, wParam, lParam);
        }
    
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, KeyboardProcess lpfn, IntPtr hMod, uint dwThreadId);
    
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
    
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);
    
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);
    
    }
    
    public class KeyPressedEventArgs : EventArgs
    {
        public Keys KeyCode { get; set; }
        public KeyPressedEventArgs(Keys Key)
        {
            KeyCode = Key;
        }
    }
    

    Implementation via Console Application:

    class Program
    {
        static void Main(string[] args)
        {
            KeyboardHook.CreateHook();
            KeyboardHook.KeyPressed += KeyboardHook_KeyPressed;
            Application.Run();
            KeyboardHook.DisposeHook();
        }
    
        static void KeyboardHook_KeyPressed(object sender, KeyPressedEventArgs e)
        {
            Console.WriteLine(e.KeyCode.ToString());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen so many solutions for this but haven't been able to implement any
I've seen many Tutorials about the MVVM-Patern but I still don't get why I
i've seen many questions on NSDateformatter and NSDate, but none could help me out.
I've seen many articles about dragging files INTO a browser, but none about dragging
I've seen many similar errors, but I can't see a solution that applies to
i have a general question about properties and ivars. ive seen many different examples
I've seen many examples of pagination like what's shown below. However, I want to
I've seen many questions like this, but i can't seem to find the answer:
I've seen many questions on SO about .NET 3.5 advantages, but these are more
I've seen many many articles on how to read XML into a JTree but

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.