我想做的是创建一个简单的Windows应用程序,它将钩取Notepad并模拟按键。我已经打开了Notepad,将其置于前景,然后模拟按下数字1的过程。但是,如果我从notepad点击到其他地方,正在活动的就成为了正在输入的内容。
我该如何将此应用程序绑定到Notepad,以便我可以点击并在任何地方键入内容,而此应用程序仍会将命令推送到Notepad?
这是我使用的模拟按键的DLL:
我该如何将此应用程序绑定到Notepad,以便我可以点击并在任何地方键入内容,而此应用程序仍会将命令推送到Notepad?
这是我使用的模拟按键的DLL:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using WindowsInput;
namespace NotePadTesting
{
class Program
{
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
static void Main(string[] args)
{
Process[] processes = Process.GetProcessesByName("notepad");
if (processes.Length == 0)
{
Process.Start("notepad.exe");
processes = Process.GetProcessesByName("notepad");
}
if (processes.Length == 0)
{
throw new Exception("Could not find notepad huh....");
}
IntPtr WindowHandle = processes[0].MainWindowHandle;
SetForegroundWindow(WindowHandle);
for (int i = 0; i < 500; i++)
{
System.Threading.Thread.Sleep(100);
InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_1);
}
}
}
}
WindowsInput
项目使用 SendInput 原生的 Windows 调用,它看起来只是模拟了一个硬件消息,这个消息会被 Windows 处理并路由到活动应用程序。你可能需要自己使用本地方法调用,并使用SendMessage
WinAPI 函数将按键发送到Process
的MainWindowHandle
。 - Anthony