I'm building an On-Screen keyboard using WPF and C# and I would like to know how to tell my program to enter text in an active window when I click a button. I want it to behave like other on-screen keyboards.
-
3Does this answer your question? [c# Sending keyboard commands to another window / process](https://stackoverflow.com/questions/8605147/c-sharp-sending-keyboard-commands-to-another-window-process) – Lucca Ferri Jan 23 '20 at 13:50
2 Answers
One way could be the use of a Messenger, like it's a common use in MVVM-Patterns.
You can register a Method to the messenger:
messengerInstance.Register<objecttype>(object recipient, string identCode, Action<objecttype> method);
And you can send a message like this:
messengerInstance.Send<objecttype>(object obj, string identCode);
So for example you have a Method MyMethod(string str)
. Then you can register it with
messengerInstance.Register<string>(this /*you are referring to this instance*/, "MyCode", MyMethod);
Now you send a message in another class:
messengerInstance.Send<string>("A", "MyCode");
What happens now, is that every Method, that is registered to the Code "MyCode" gets "A" as parameter and gets executed.
Bigger example:
public class A
{
private Messenger messenger = Messenger.Instance; //Messenger is singleton, means it is build to only have one instance
public A()
{
messenger.Register<string>(this, "MyCode", MyMethod);
}
private void MyMethod(string str)
{
Console.WriteLine(str);
}
}
public class B
{
private Messenger messenger = Messenger.Instance;
public B()
{
messenger.Send<string>("Hello, World!", "MyCode");
}
}
When class B gets initialized, the constructor sends the message "Hello, World!" with the IdentCode "MyCode". Now class A gets the notification "I have the message Hello, World!
for everyone who is registered to MyCode
". Class A sees that MyMethod is registered to this code and passes the message "Hello, World!" to the Method. In the end you get the output "Hello, World!"
Of course this is simplified, there are plenty of possibilities to create such a messenger, this is just my version that I use, but it's quite without stress, you can basically send the letters to the textbox. Of course you can send any object you like. For example
//an integer
messenger.Send<int>(10, "MyCode");
//a boolean
messenger.Send<bool>(true, "MyCode");
//or even other classes
messenger.Send<MyClass>(new MyClass(), "MyCode");
This is my Messenger-class I use at work: https://pastebin.com/k6q9Y6rR

- 751
- 2
- 10
- 29
-
-
Oh, then I don't know the answer. I don't even think that many will, cause windows already has an on-screen-keyboard, so there's no need to create another one – DudeWhoWantsToLearn Jan 23 '20 at 14:40
-
Is your button subscribed to some sort of click event? If it is, you can just add the value the button has to the active window you're talking about by concatenating the button string value to the current value in your active window. If there's no click event, you can add one and then do as i mentionned above
how to subscribe to a button click event:
btn.Click += btn1_Click;
private void btn1_Click(object sender, RoutedEventArgs e)
{
// concatenate to the active window result view the button value here
}

- 306
- 1
- 5
-
1-1: the way I understood it, OP wants to write to another process' input, after you click on a button, like a normal On-Screen keyboard. – Lucca Ferri Jan 23 '20 at 13:51
-
What he wants is not really clear, I thought of it as he had multiple buttons representing the keyboard, and when one is pressed, he wants the value being sent to some sort of textbox in an active window – Jonathan Mathieu Jan 23 '20 at 13:54
-
"I would like to know how to tell my program to enter text in an active window when I click a button.", and yes, the question does lack some explanation of what he's after and what he's tried, but that's definitely what he has asked. – Lucca Ferri Jan 23 '20 at 14:00
-
But he said he's building an on-screen keyboard, so he probably has to go through a button click event somewhere that should update text in his active window, the button being a representation of a key in his keyboard – Jonathan Mathieu Jan 23 '20 at 14:06
-
You are correct, Jonathan. I want to choose an active window and click a button to input text in that active window. – Jonathan George VanSteenburg Jan 23 '20 at 15:02