title: Unity: Send Key to Background Window
body: <p>I am making a customizable on-screen keyboard with Unity, but am having trouble figuring out how to get the keyboard to send the keys to whatever window is behind it.</p>
  
  <p>I have tried using a slightly edited version of <a href="https://stackoverflow.com/questions/12805345/send-combination-of-keystrokes-to-background-window">Louisbob's</a> code:</p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using System;
  using UnityEngine.UI;
  using System.Windows.Forms;
  using System.Runtime.InteropServices;
  using System.Diagnostics;
  
  [SerializeField]
  class SendMessages
  {
      [DllImport("user32.dll")]
      public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  
      public static void sendKeystroke()
      {
          const uint WM_KEYDOWN = 0x0100;
          const uint WM_KEYUP = 0x0101;
  
          IntPtr hWnd;
          string processName = "Notepad";
          Process[] processList = Process.GetProcesses();
  
          foreach (Process P in processList)
          {
              if (P.ProcessName.Equals(processName))
              {
                  IntPtr edit = P.MainWindowHandle;
                  PostMessage(edit, WM_KEYDOWN, (IntPtr)(Keys.B), IntPtr.Zero);
              }
          }
      }
  
  }
  
  public class B_Button : MonoBehaviour
  {
      // Start is called before the first frame update
      void Start()
      {
  
      }
  
      // Update is called once per frame
      void Update()
      {
  
          if(Input.GetMouseButtonDown(0))
          {
              SendMessages.sendKeystroke();
          }
      }
  
  }
  
  </code></pre>
  
  <p>This code does not return any errors but when I run/build the project and click, no 'B' appears in Notepad.  Am I doing something wrong or does the PostMessage function not work with Unity?  If it does not work, what else can I do to get this program to work?</p>
  
  <p><b> Update </b></p>
  
  <p>I figured out that getting the handle on the window is one or the only problem.  I did </p>
  
  <pre><code>Process[] n = Process.GetProcessesByName("Notepad");
          foreach (Process p in n)
          {
              hWnd = p.MainWindowHandle;
              UnityEngine.Debug.Log(hWnd);
          }
  </code></pre>
  
  <p>on multiple programs, that were running, and it returned 0 for both mainwindowhandles.  I am not sure what is causing this since the applications are running and not hidden, so any advice would be appreciated.</p>