0

Currently I used this snip code as a result from googling.

var eventArgs = new TextCompositionEventArgs(Keyboard.PrimaryDevice,
                new TextComposition(InputManager.Current, Keyboard.FocusedElement, "A"));
eventArgs.RoutedEvent = TextInputEvent;             
var flag = InputManager.Current.ProcessInput(eventArgs);

It was working if I used Keyboard.Focus(TxtBox); and the TxtBox will be filled with the Keystroke.

But what I want really achieved is:

  • 1.Drawing a box (for example, I draw box on one of the excel cell)
  • 2.Click on the box coordinate (to change Keyboard Focus)
  • 3.Send Keystroke to clicked excel cell

I have done step 1 and 2. But I can't find a way to do the third step.

Somehow, the click event (using mouse event) maybe not changing Keyboard Focus automatically.

So, how do I change Keyboard focus, if possible using coordinate ? Or maybe can I get IInputElement from a coordinate ? and then set keyboard focus to it. Of course, all of it outside from the main application window of the WPF.

Lyn
  • 507
  • 3
  • 15
  • add the keystrokes to a different control that has focus at the time and just update the text of the target? – mahlatse Nov 30 '18 at 06:57
  • @mahlatse I dont think so. My target is outside of the wpf app window. In my example it is excel cell. How do I update the excel text then? – Lyn Nov 30 '18 at 07:07
  • If it is an excel cell and you know the cell/grid, you can use a 3rd party library like EPPLUS to update the excel by loading it, just not sure you you would reload the excel file though. – mahlatse Nov 30 '18 at 07:35
  • @mahlatse Thank you for your idea. But excel here is just an example for simple explanation of what I tried to achieved. But it won't be an excel cell. It should be anythng that can be input-ed by text. Like notepad, search bar, etc. – Lyn Nov 30 '18 at 07:41
  • If it is something that you created , then its simpler, if its something that you have no control over, you might need to do some magic dev[ assuming they allow command line parameters, etc] – mahlatse Nov 30 '18 at 09:38

1 Answers1

2

Found it !

At: Installed InputSimulator via NuGet, no members accessible

It is working in most cases.

I said in most cases, because it is able to type in other window like excel application, but on other custom app window. There might be a case it won't work.

Hope it help for other people, looking for the same thing.

Lyn
  • 507
  • 3
  • 15