在C#中使用SendInput()发送长长的击键。问题是,当使用它时,当它被调用时,它可以在所有程序上工作。有没有可能让它只在一个特定的程序上工作?例如在使用FindWindow的PostMessage()中。
My SendInput():
[DllImport("user32.dll")]
internal static extern uint SendInput(
uint nInputs,
[MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs,
int cbSize);
public void KeySend(VirtualKeyShort key)
{
INPUT[] Inputs = new INPUT[1];
INPUT Input = new INPUT();
Input.type = 1;
Input.U.ki.wVk = key;
Inputs[0] = Input;
SendInput(1, Inputs, INPUT.Size);
}
public void KeyUp(VirtualKeyShort key)
{
INPUT[] Inputs = new INPUT[1];
INPUT Input = new INPUT();
Input.type = 1;
Input.U.ki.wVk = key;
Input.U.ki.dwFlags = KEYEVENTF.KEYUP;
Inputs[0] = Input;
SendInput(1, Inputs, INPUT.Size);
}谢谢。
发布于 2018-12-13 00:24:29
在启动sendinput之前,您可以这样做(例如),如果您想要将键发送到记事本:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
class Program {
static void Main(string[] args) {
var proc = Process.GetProcessesByName("notepad");
if (proc.Length > 0) {
SetForegroundWindow(proc[0].MainWindowHandle);
}
}
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);}
您的window应用程序将获得焦点,并且可以接收密钥
如果您想枚举所有应用程序,则可以使用EnumWindows
https://stackoverflow.com/questions/53745357
复制相似问题