0

The Problem:

I have an active WPF Window. On Creation a process is set as the owner of the Window. Now when the Key-Down event is fired on the WPF window the Key should be passed to the main Process.

At first i set the Focus to main process using SetForegroundWindow(). The the key is send using InputSimulator.

So far this works as expected. However, after sending the Key, the Wpf Window should get back to focus, so it can catch the next key presses.

I'm trying to call Activate() on the WPF Window. The problem that now occurs is that Key send by the InputSimulator is no longer passed to the main process, but instead is caught by the WPF Window.

Code

Heres the code:

This is the creation of the Window

public void createWindow() 
{
    CommandLineWindow win = new CommandLineWindow();
    helper = new WindowInteropHelper(win);
    Process oCurrent = Process.GetCurrentProcess();
    var ww = new WindowWrapper(oCurrent.MainWindowHandle); 
    helper.Owner = oCurrent.MainWindowHandle;
    win.ShowDialog();
}
 

The implementation of the WindowWrapper:

public class WindowWrapper : System.Windows.Forms.IWin32Window
{
    public WindowWrapper(IntPtr handle)
    {
        _hwnd = handle;
    }
    public IntPtr Handle
    {
        get { return _hwnd; }
    }
    private IntPtr _hwnd;
}           

And my Window.KeyDown Handler:

private void Window_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.X)
    {
         //Do something                 
         SetForegroundWindow(helper.Owner);
         new InputSimulator().Keyboard.KeyDown(WindowsInput.Native.VirtualKeyCode.VK_Y);
         this.Activate();
    }
}
Community
  • 1
  • 1
  • If I understand it correctly, you try to forward the keyboard input from one window to another in the same WPF process. Why so complicated (using WinAPI, `InputSimulator` etc)? Could you maybe elaborate more what your use case is? – dymanoid Feb 04 '19 at 15:19
  • there is an application running (no wpf) that i dont have an influence to manipulate except specific API calls. When i create my WPF application i set this external programm as the owner of the wpf window. The WPF is activated and so gets the keydown events. I want to pass this key presses to the external application and turn the focus back to the wpf apllication – Mayday Malone Feb 04 '19 at 15:38
  • So your code doesn't represent your use case. In your code, you only handle your application. That's a big difference actually. E.g. `SetForegroundWindow` most probably won't work at all (read remarks [here](https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setforegroundwindow)). Please provide a [mcve]. Your code sample must represent your use cases. – dymanoid Feb 04 '19 at 15:42
  • SetForegroundWindow works well as long as i dont call this.Activate(). What i dont understand is why it stops working when i call this.Active() afterwards – Mayday Malone Feb 04 '19 at 15:45
  • Can't you use `SendKeys.SendWait` Method? as it described in here: https://stackoverflow.com/a/6612270/4731319 – gpro Feb 08 '19 at 11:01

0 Answers0