将密钥发送到由我的应用程序启动的进程不起作用
Sending Keys to the process started by my application not working
我已经使用 c# 创建了一个 windows 表单应用程序,并且我有一个要启动的应用程序。我以管理员身份成功启动了应用程序,接下来我要做的是向应用程序发送一些键,特别是 Alt 和箭头键并输入。我使用 FindWindow 函数通过其 window 名称查找 window。然后我使用 SetForeground 函数将焦点设置到我的应用程序,最后我将键发送给它。
为了检查我的代码是否正确,我尝试将密钥发送到标题为 "Untitled - Notepad" 的记事本并且它有效。但如果我用应用程序的标题替换它,它就不起作用。我使用了 Visual Studio 提供的 "spy++" 工具。我开始我的申请。复制它的 class 名称和 windows 标题,将其粘贴到 FindWindow 函数中,但仍然不起作用。
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.FileName = finalString;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.Verb = "runas";
myProcess.StartInfo.Arguments = "--AutoInject --EmulateController";
myProcess.Start(); String Pid = "" + myProcess.Id;
/* The variables found using spy++
Window Caption = [DEBUG] PS4 Macro - v0.5.2 (BETA)
Class Name = WindowsForms10.Window.8.app.0.31c915c_r6_ad1
*/
IntPtr AppHandle = FindWindow(null, "[DEBUG] PS4 Macro - v0.5.2 (BETA)");
if (AppHandle == IntPtr.Zero)
{
MessageBox.Show("Something's wrong I can feel it");
return;
}
SetForegroundWindow(AppHandle);
SendKeys.SendWait("^(o)");
在 运行 之后,应用程序运行,但无法按 ctrl+o 键。
它应该收到 ctrl+o 命令。另请注意:当应用程序启动时,我的表单的菜单栏不会变白,即它可能不会失去焦点。我的表单的菜单栏和启动的应用程序都是黑色的。
我认为你需要使用
AllowSetForegroundWindow(AppHandle);
SetForegroundWindow(AppHandle);
使指定进程能够使用 SetForegroundWindow 函数设置前景 window。
我已经使用 c# 创建了一个 windows 表单应用程序,并且我有一个要启动的应用程序。我以管理员身份成功启动了应用程序,接下来我要做的是向应用程序发送一些键,特别是 Alt 和箭头键并输入。我使用 FindWindow 函数通过其 window 名称查找 window。然后我使用 SetForeground 函数将焦点设置到我的应用程序,最后我将键发送给它。
为了检查我的代码是否正确,我尝试将密钥发送到标题为 "Untitled - Notepad" 的记事本并且它有效。但如果我用应用程序的标题替换它,它就不起作用。我使用了 Visual Studio 提供的 "spy++" 工具。我开始我的申请。复制它的 class 名称和 windows 标题,将其粘贴到 FindWindow 函数中,但仍然不起作用。
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.FileName = finalString;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.Verb = "runas";
myProcess.StartInfo.Arguments = "--AutoInject --EmulateController";
myProcess.Start(); String Pid = "" + myProcess.Id;
/* The variables found using spy++
Window Caption = [DEBUG] PS4 Macro - v0.5.2 (BETA)
Class Name = WindowsForms10.Window.8.app.0.31c915c_r6_ad1
*/
IntPtr AppHandle = FindWindow(null, "[DEBUG] PS4 Macro - v0.5.2 (BETA)");
if (AppHandle == IntPtr.Zero)
{
MessageBox.Show("Something's wrong I can feel it");
return;
}
SetForegroundWindow(AppHandle);
SendKeys.SendWait("^(o)");
在 运行 之后,应用程序运行,但无法按 ctrl+o 键。 它应该收到 ctrl+o 命令。另请注意:当应用程序启动时,我的表单的菜单栏不会变白,即它可能不会失去焦点。我的表单的菜单栏和启动的应用程序都是黑色的。
我认为你需要使用
AllowSetForegroundWindow(AppHandle);
SetForegroundWindow(AppHandle);
使指定进程能够使用 SetForegroundWindow 函数设置前景 window。