Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

When using parentHWND option, keyboard input doesn't work

Discussion in 'Editor & General Support' started by Virtualware, Oct 29, 2014.

  1. Virtualware

    Virtualware

    Joined:
    Sep 17, 2012
    Posts:
    75
    I've been testing the new parentHWND command line option that appeared in 4.5.5p1 using the reference example provided in the documentation page, but when I tried to use Input.GetKey it never triggered. Input.GetMouseButton works correctly, but I couldn't get keyboard input to work. I've tested it in Windows 8.1 x64 and Windows 7 x64.
    I know is a very recent feature, but it looked great and I'd love to use it.
    Any advice?
     
  2. jariRG

    jariRG

    Joined:
    May 9, 2013
    Posts:
    1
    I have the same problem, mouse input works fine, but no keyboard input at all.
    Did you find a solution or workaround for this?
    Any official statement about this? Is it a bug, or missing something from the initialization on opener side?
     
  3. Dave-Hampson

    Dave-Hampson

    Unity Technologies

    Joined:
    Jan 2, 2014
    Posts:
    150
    I think you need to send WM_ACTIVATE
     
    GilCat likes this.
  4. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,875
    Adding to Dave answer, here's the updated Form1.cs

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Runtime.InteropServices;
    8. using System.Text;
    9. using System.Threading;
    10. using System.Windows.Forms;
    11. using System.Diagnostics;
    12. using System.Windows.Forms.VisualStyles;
    13.  
    14. namespace Container
    15. {
    16.     public partial class Form1 : Form
    17.     {
    18.         [DllImport("User32.dll")]
    19.         static extern bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
    20.  
    21.         internal delegate int WindowEnumProc(IntPtr hwnd, IntPtr lparam);
    22.         [DllImport("user32.dll")]
    23.         internal static extern bool EnumChildWindows(IntPtr hwnd, WindowEnumProc func, IntPtr lParam);
    24.  
    25.         [DllImport("user32.dll")]
    26.         static extern int SendMessage(IntPtr hWnd, int msg, int wParam, uint lParam);
    27.  
    28.         private Process process;
    29.         private IntPtr unityHWND = IntPtr.Zero;
    30.  
    31.         private const int WM_ACTIVATE = 0x0006;
    32.         private const int WA_ACTIVE = 1;
    33.  
    34.         public Form1()
    35.         {
    36.             InitializeComponent();
    37.  
    38.             try
    39.             {
    40.                 process = new Process();
    41.                 process.StartInfo.FileName = "Child.exe";
    42.                 process.StartInfo.Arguments = "-parentHWND " + panel1.Handle.ToInt32() + " " + Environment.CommandLine;
    43.                 process.StartInfo.UseShellExecute = true;
    44.                 process.StartInfo.CreateNoWindow = true;
    45.  
    46.                 process.Start();
    47.  
    48.                 process.WaitForInputIdle();
    49.                 // Doesn't work for some reason ?!
    50.                 //unityHWND = process.MainWindowHandle;
    51.                 EnumChildWindows(panel1.Handle, WindowEnum, IntPtr.Zero);
    52.  
    53.                 unityHWNDLabel.Text = "Unity HWND: 0x" + unityHWND.ToString("X8");
    54.             }
    55.             catch (Exception ex)
    56.             {
    57.                 MessageBox.Show(ex.Message + ".\nCheck if Container.exe is placed next to Child.exe.");
    58.             }
    59.  
    60.         }
    61.         private int WindowEnum(IntPtr hwnd, IntPtr lparam)
    62.         {
    63.             unityHWND = hwnd;
    64.             SendMessage(unityHWND, WM_ACTIVATE, WA_ACTIVE, 0);
    65.             return 0;
    66.         }
    67.  
    68.         private void panel1_Resize(object sender, EventArgs e)
    69.         {
    70.             MoveWindow(unityHWND, 0, 0, panel1.Width, panel1.Height, true);
    71.             SendMessage(unityHWND, WM_ACTIVATE, WA_ACTIVE, 0);
    72.         }
    73.  
    74.         // Close Unity application
    75.         private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    76.         {
    77.             try
    78.             {
    79.                 process.CloseMainWindow();
    80.  
    81.                 Thread.Sleep(1000);
    82.                 while (process.HasExited == false)
    83.                     process.Kill();
    84.             }
    85.             catch (Exception)
    86.             {
    87.                
    88.             }
    89.         }
    90.     }
    91. }
    92.  
     
  5. Razia Mahmood

    Razia Mahmood

    Joined:
    Feb 2, 2015
    Posts:
    2
    Can you plz guide me how to pass data to the embedded Unity Exe from the Host WPF Application ?
     
  6. wisockijunior

    wisockijunior

    Joined:
    Jul 14, 2015
    Posts:
    1
  7. unity_FkbuTsvygonPag

    unity_FkbuTsvygonPag

    Joined:
    Jul 16, 2019
    Posts:
    1
    I had similar issue, this was caused by the line
    Code (CSharp):
    1. unityHWND = process.MainWindowHandle;
    I dont know why I uncommented it. Removed the line, works well.
     
  8. leduc232

    leduc232

    Joined:
    Dec 14, 2022
    Posts:
    3
    Still looking for a solution for this with the new input system. If anyone have one.
     
  9. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,645