简体   繁体   English

PostMessage到当前活动进程不适用于所有进程?

[英]PostMessage to Current Active Process doesn't work for all process?

I have this code: 我有这个代码:

    const UInt32 WM_KEYDOWN = 0x0100;

    const int VK_DOWN = 0x28;
    const int VK_UP = 0x26;

    [DllImport("user32.dll")]
    static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);

    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    private static extern Int32 GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

    private static Process GetProcessByHandle(IntPtr hwnd)
    {
        try
        {
            uint processID;
            GetWindowThreadProcessId(hwnd, out processID);
            return Process.GetProcessById((int)processID);
        }
        catch { return null; }
    }

    public static Process GetActiveProcess()
    {
        IntPtr hwnd = GetForegroundWindow();
        return hwnd != null ? GetProcessByHandle(hwnd) : null;
    }

    public static void KeyUp()
    {
        PostMessage(GetActiveProcess().MainWindowHandle, WM_KEYDOWN, VK_UP, 0);
    }

    public static void KeyDown()
    {
        PostMessage(GetActiveProcess().MainWindowHandle, WM_KEYDOWN, VK_DOWN, 0);
    }

When i call the KeyUp/KeyDown it works only on some process, proccess like text editos, visual studio. 当我调用KeyUp / KeyDown时,它仅适用于某些进程,如text editos,visual studio。

But doesn't work on Explorer.exe, iTunes.exe, Chrome.exe, Outlook.exe, FileZilla.exe and more.. 但不适用于Explorer.exe,iTunes.exe,Chrome.exe,Outlook.exe,FileZilla.exe等。

I ran debug on console: 我在控制台上运行调试:

    while (true)
    {
        Process currentProcess = GetActiveProcess();
        if (currentProcess != null)
            Console.WriteLine(currentProcess.MainWindowTitle);
        Thread.Sleep(1000);
    }

Here what I get from some applications: 这是我从一些应用程序得到的:

ProjName - Microsoft Visual Studio (Administrator)
?PostMessage to Current Active Proccess doesn't work for all proccess ? - StackOverFlow - Google Chrome?
FileZilla
Inbox - Outlook Data File - Microsoft Outlook

And on Explorer.exe i get empty line.. 在Explorer.exe上我得到空行..

There are multiple ways to read keyboard state in a Windows application, and responding to WM_KEYDOWN and WM_KEYUP is just one of them. 在Windows应用程序中有多种方法可以读取键盘状态,并且响应WM_KEYDOWNWM_KEYUP只是其中之一。 Applications can also use GetKeyState or GetAsyncKeyState . 应用程序也可以使用GetKeyStateGetAsyncKeyState Applications can also respond to the WM_CHAR , WM_SYSKEYDOWN and WM_SYSKEYUP messages. 应用程序还可以响应WM_CHARWM_SYSKEYDOWNWM_SYSKEYUP消息。 Different applications will use different methods and if you are not simulating the one the application is looking for, it won't respond. 不同的应用程序将使用不同的方法,如果您没有模拟应用程序正在寻找的方法,它将不会响应。

The problem you are having is slightly different than the one posed in the question that Raymond linked to, but the solution may be the same. 您遇到的问题与Raymond关联的问题中提出的问题略有不同,但解决方案可能相同。 Namely using SendInput instead of PostMessage 即使用SendInput而不是PostMessage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM