Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Wait for keyboard input text sequence via C#

Wait for keyboard input text sequence via C#

Wait for keyboard input text sequence via C#

Hi there,

I want to know is it possible via a keyboard hook in C# to monitor all input for a certain text sequence, e.g. WN0P, and then do what I need once the text has been input via the keyboard?

If so, how do I do that, I only have found articles on checking for a specific key.


RE: Wait for keyboard input text sequence via C#

Not a very elegant solution - and it needs some serious thought and refactoring, but this should get you started. Note: I just used a WPF handler in this example.

CODE --> (WPF)

readonly string magicCombination = "WNOP";
        string keystrokes = string.Empty;

        private void OnPreviewKeyDown(object sender, KeyEventArgs e)
            //refactor this (regex for character?)
            if (e.Key == Key.Space  || e.Key == Key.Tab || e.Key == Key.Enter) //etc. etc.
                //reset keystroke tracking
            keystrokes += e.Key.ToString();

            if (keystrokes.EndsWith(magicCombination))
                //call method to do whatever the magic combo is supposed to trigger here
                //I am just updating a textblock
                UpdateUI("Magic Combination!");

        private void UpdateUI(string incomingText)
            txtbxKeyStrokes.Text = incomingText;

        private void ResetKeyStrokes()
            keystrokes = string.Empty;

good luck!


RE: Wait for keyboard input text sequence via C#

Thanks for that Mark. Unfortunately I seem to be missing something as when I put that into a Windows Form and run it and start typing it never hits the OnPreviewKeyDown function? I need to be able to monitor all keyboard entries, not just in the C# application, I'm trying to implement what I need via C#, if you follow what I mean?

RE: Wait for keyboard input text sequence via C#

I attached the OnPreviewDown method on the eventhandler "PreviewKeyDown" of the WPF window. In the case of Windows Forms, I would attach it to the form.

If you want something outside of a running application, it looks like you may want something else (Windows Service?). Unfortunately, I'm not sure what, exactly. Sorry I couldn't be more help.

RE: Wait for keyboard input text sequence via C#

Thanks for your efforts.

RE: Wait for keyboard input text sequence via C#

It will probably have to be a service. You're looking to build a keylogger-style app. Here's something to get you started:


With business clients like mine, you'd be better off herding cats.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close