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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:40:24+00:00 2026-05-19T12:40:24+00:00

Greetings, I want to write code that executes within an event handler, inside a

  • 0

Greetings,

I want to write code that executes within an event handler, inside a WPF Windows application, that can detect a keypress, specifically an “Escape” character keypress, within a processing loop. This will allow the user to escape from processing. I realize this may be accomplished with some kind of multi-threaded approach, but the problem seems so simple I wondered if it might be accomplished as follows:

// Attempt 1: See if Keyboard static IsKeyDown method detects key presses while executing.
// Note that this was not successful. The Keyboard states do not appear to be updated during processing.

        bool iskeypressed = false;
        while (!iskeypressed)
        {
            System.Threading.Thread.Sleep(1000);
            if (Keyboard.IsKeyDown(Key.Enter))
                iskeypressed = true;  
        }

So, on to attempt #2. I saw some articles and samples using the Pinvoke “GetKeyboardState” method. I’m not sure I used the method correctly, but here is my attempt. It is a bit clumsy to refer to a Windows.Forms enumeration in a WPF application, but it seems like it could work.

// Attempt 2: Use Pinvoke GetKeyboardState method.
// So far, I’ve been unsuccessful with this as well, but I’m not sure my usage is correct.

        bool iskeypressed = false;
        while (!iskeypressed)
        {
            System.Threading.Thread.Sleep(1000);
            if (isEscapePressed()) 
                iskeypressed = true;  
        }
    }


    [DllImport("user32.dll")] public static extern int GetKeyboardState(byte[] lpKeyState);
    private bool isEscapePressed()
    {
        byte[] keyboardState = new byte[255];
        int keystate = GetKeyboardState(keyboardState);

        if (keyboardState[(int)System.Windows.Forms.Keys.Escape] == 128)
            return true;
        else
            return false; 
    }    

But unfortunately, I’m not seeing any change in the keyboard states as this executes. I also played around a little with calls to the Dispatcher to see if I could get the keyboard information to refresh during processing, but I have not been successful with any technique.

I’m out of ideas. Can someone propose something? Thank you in advance for your assistance.

  • David
  • 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-19T12:40:24+00:00Added an answer on May 19, 2026 at 12:40 pm

    Something like this:

    private bool IsCancelled { get; set; }
    
    private void OnButtonClick(object sender, EventArgs e)
    {
       Action doWorkDelegate = DoWork;
    
       doWorkDelegate.BeginInvoke(null, null);
    }
    
    protected override void OnKeyDown(KeyEventArgs e) {
        if (e.Key == Key.Escape) {
            IsCancelled = true;
            e.Handled = true;
        } else {
            base.OnKeyDown(e);
        }
    }
    
    private void DoWork()
    {
       IsCancelled  = false;
       while (!IsCancelled)
       {
           System.Threading.Thread.Sleep(1000);
       }
    }
    

    The important point is that the method that does the work is executed in a separate thread so the main thread can process user input (key strokes).

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

Sidebar

Related Questions

Greetings! I am trying to check directory write-permissions from within a Windows MFC/ATL program
Greetings! Anyway, My question, If you want to experiment developing for a Windows Mobile
Greetings, The VBA code below will create an Excel QueryTable object and display it
Greetings, I'm trying to find a way to 'unbind' a socket from a particular
Greetings. I'm looking for a way to parse a number of XML files in
Greetings! I'm working on wrapping my head around LINQ. If I had some XML
Greetings, I'm trying to find either a free .NET library or a command-line executable
Greetings, I need a way (either via C# or in a .bat file) to
Greetings all, I'm trying to localize a .NET/C# project. I'm using string resource files
Greetings, currently I am refactoring one of my programs, and I found an interesting

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.