C # // SendKeys.SendWait only works when the process window is minimal

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.ComponentModel; using System.Data; using System.Runtime.InteropServices; using System.Diagnostics; using System.Threading; using System.Windows.Forms; using System.Drawing; namespace TextSendKeys { class Program { [DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); static void Main(string[] args) { Process[] processes = Process.GetProcessesByName("game"); Process game1 = processes[0]; IntPtr p = game1.MainWindowHandle; ShowWindow(p,1); SendKeys.SendWait("{DOWN}"); Thread.Sleep(1000); SendKeys.SendWait("{DOWN}"); } } } 

This program is designed to send twice the DOWN button in the game window. It only works when my window is minimized (it activates the window and does its work). If my window is activated (not minimized), nothing happens. How to solve this?

Thanks!:)

+4
source share
1 answer

Try using the SetForegroundWindow call to the Win32 API instead of ShowWindow to activate the game window. (Signed from pinvoke.net .)

 [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool SetForegroundWindow(IntPtr hWnd); static void Main(string[] args) { Process[] processes = Process.GetProcessesByName("game"); Process game1 = processes[0]; IntPtr p = game1.MainWindowHandle; SetForegroundWindow(p); SendKeys.SendWait("{DOWN}"); Thread.Sleep(1000); SendKeys.SendWait("{DOWN}"); } 
+8
source

Source: https://habr.com/ru/post/1392201/


All Articles