Click here to Skip to main content
14,932,440 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to detect the user input any where in windows and want to replace it with my custom value. For now I am using hooks to detect the key press but I am not able to change the key value: as like if user typed "Hello " in notepad the it want to replace it with "Hey! there..."

What I have tried:

I am using hooks to detect the key press but I am not able to change the key value: as like if user typed "Hello " in notepad the it want to replace it with "Hey! there..."
Updated 23-Jul-18 2:09am
ZurdoDev 23-Jul-18 7:58am
I'm not sure you'll find any one here that will help you write malware.
Abhijeet P Singh 23-Jul-18 8:12am
this will work only on application level i want to d it globally . like if someone typed 2 in notepad then it should become 3 automaticlly

It can't be done because when your hook receives the first character 'H' it does not know if the following key presses are 'ello'. So it has to let it pass anyway. Once it knows that 'Hello' has been entered when handling the 'o', the preceeding keys has been already passed.

There is also no reasonable purpose to perform such replacements globally because your hooking application does not know about the application that actually receives the keyboard input. It might be even doing something unwanted when it results in hotkeys being executed.
Abhijeet P Singh 23-Jul-18 8:23am
It's not something unwanted. It's very simple like text-editor as like when in Sublime we type "div#row" and enter tab then it is being changed to a proper div tag with id=row . I want to achieve same functionality at global level in windows
Jochen Arndt 23-Jul-18 8:28am
As I told you: You can't predict what the user enters.

You always have to know what has been entered and might then change it according to sume rules. But that requires to do it on application level because only the application has for example access to an edit control, knows what is contained in there, and can change it's content.
Abhijeet P Singh 23-Jul-18 8:56am
Are you sure that it can not be done?
Dave Kreskowiak 23-Jul-18 9:07am
The short answer is no, it can't.

The long answer is yes, it can, but with great difficulty. There's a LOT more you have to track besides just looking at the keystrokes flying by.

No, I'm not going to describe it because I'm not going to aid in malware. Maybe not written by you, but someone else who comes by this post looking for something to use.
Jochen Arndt 23-Jul-18 9:09am
Not globally when not knowing how data are handled by the active application.

After having passed keys, you would have to undo and insert new text. So you have to know what has been inserted where and how to change it. That requires tracking the active window which receives the keys and accessing that window to modifiy the content. While theoratically possible if not prevented by security settings, it requires a lot of work and has a great potential of abuse and is prone to unwanted side effects.

Your example for Sublime does not count here: It does it on application level.
Here is the one way:

namespace WindowsFormsApplication1
    public partial class Form1 : Form
        public Form1()

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            if (e.KeyChar >= 48 && e.KeyChar <= 57)
                MessageBox.Show("Form.KeyPress: '" +
                    e.KeyChar.ToString() + "' pressed.");

                switch (e.KeyChar)
                    case (char)50:
                        e.KeyChar = (char)51;
                    case (char)49:
                        MessageBox.Show("Form.KeyPress: '" +
                            e.KeyChar.ToString() + "' consumed.");
                        e.Handled = true;

On pressing number 2, I am changing it to 3 and e.Handled remain false.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900