可能定位到SendInput()?

时间:2018-12-12 14:36:11

标签: c# user32

使用SendInput()在C#中发送长击键。问题在于,使用它时,它在所有程序上都可以运行。是否有可能将其定位为仅在特定程序上工作?就像在使用FindWindow的PostMessage()中一样。

我的SendInput():

    private static FirebaseAuth Auth { get; set; }
    public void SignInWithCredential(Credential credential, Action<FirebaseUser> onSignIn, Action<Exception> onFailure, Action onCanceled = default(Action))
        {
            if (onCanceled == null)
                onCanceled = delegate { };

            Auth.SignInWithCredentialAsync(credential).ContinueWith(task =>
            {
                if (task.IsCanceled)
                    onCanceled.Invoke();

                else if (task.IsFaulted)
                    onFailure.Invoke(task.Exception);

                else if (task.IsCompleted)
                    onSignIn.Invoke(task.Result);
            });            
    }

    public void LinkWithCredential(FirebaseUser user, Credential credential, Action<FirebaseUser> onLinkIn, Action<Exception> onFailure, Action onCanceled = default(Action))
    {
        if (onCanceled == null)
            onCanceled = delegate { };

        user.LinkWithCredentialAsync(credential).ContinueWith(task =>
        {
            if (task.IsCanceled)
                onCanceled.Invoke();

            else if (task.IsFaulted)
                onFailure.Invoke(task.Exception);

            else if (task.IsCompleted)
                onLinkIn.Invoke(task.Result);
        });
    }

谢谢。

1 个答案:

答案 0 :(得分:0)

在启动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);

}

您的窗口应用程序将具有焦点并可以接收键

如果要枚举所有应用程序,则可以使用EnumWindows