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 8070837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:31:12+00:00 2026-06-05T13:31:12+00:00

I know there is a question for Windows Forms but it doesn’t work in

  • 0

I know there is a question for Windows Forms but it doesn’t work in the console, or at least I couldn’t get it to work. I need to capture key presses even though the console doesn’t have focus.

  • 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-05T13:31:14+00:00Added an answer on June 5, 2026 at 1:31 pm

    You can create a global keyboard hook in a console application, too.

    Here’s complete, working code:
    https://learn.microsoft.com/en-us/archive/blogs/toub/low-level-keyboard-hook-in-c

    You create a console application, but must add a reference to System.Windows.Forms for this to work. There’s no reason a console app can’t reference that dll.

    I just created a console app using this code and verified that it gets each key pressed, whether or not the console app has the focus.

    EDIT

    The main thread will run Application.Run() until the application exits, e.g. via a call to Application.Exit(). The simplest way to do other work is to start a new Task to perform that work. Here’s a modified version of Main() from the linked code that does this

    public static void Main()
    {
        var doWork = Task.Run(() =>
            {
                for (int i = 0; i < 20; i++)
                {
                    Console.WriteLine(i);
                    Thread.Sleep(1000);
                }
                Application.Exit(); // Quick exit for demonstration only.  
            });
    
        _hookID = SetHook(_proc);
    
        Application.Run();
    
        UnhookWindowsHookEx(_hookID);
    }
    

    NOTE

    Possibly provide a means to exit the Console app, e.g. when a special key combo is pressed depending on your specific needs. In the

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there's no one answer to this question, but I'd like to get
I know there is a question about that, but there isn't any good answer(for
I know there is such a question on SO, but I could not find
I know there is a very similar question here but I was hoping to
I know there is no simple answer to my question but I would appreciate
I know there must be a really simple answer to this question, but I
This is a very narrow and specific question, but I know there are someone
I know there is a very similiar question ( JavaScript: How to select "Cancel"
Firstly I know that there are many question and solutions to correct thread marshalling
Before jumping on me, please read my question carefully :D. I know there are

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.