为什么后母亲不适合所有VK值?

发布于 2025-02-03 00:25:26 字数 1292 浏览 3 评论 0 原文

我试图通过编程键入键盘输入到窗口。我意识到 sendinput> sendinput 是最佳的这里的方法,但是我需要能够将此击键发送到当前不重点的窗口。

因此,我正在尝试使用

以下作品可用于模拟记事本中F5密钥的输入,而不是为空格键。

using System.Diagnostics;
using System.Runtime.InteropServices;

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern bool PostMessage(IntPtr hWnd, UInt32 msg, uint wParam, int lParam);

const uint WM_KEYDOWN = 0x0100;
const uint WM_KEYUP = 0x0101;
const int VK_SPACE = 0x20;
const int VK_F5 = 0x74;

while(true)
{
    Process[] processes = Process.GetProcessesByName("notepad");
    foreach (Process process in processes)
    {
        PostMessage(process.MainWindowHandle, WM_KEYDOWN, VK_F5, 0);
        PostMessage(process.MainWindowHandle, WM_KEYUP, VK_F5, 0);
        Thread.Sleep(5000);
        PostMessage(process.MainWindowHandle, WM_KEYDOWN, VK_SPACE, 0);
        PostMessage(process.MainWindowHandle, WM_KEYUP, VK_SPACE, 0);
    }
}

知道为什么吗?有更好的选择吗?

I'm attempting to simulate keyboard input to an window programmatically. I realize SendInput is the optimal method here, but I need to be able to send this keystroke to a window that is not currently in focus.

As such, I'm trying to use PostMessage instead.

The following works for simulating the input of the F5 key in notepad, but not for the spacebar.

using System.Diagnostics;
using System.Runtime.InteropServices;

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern bool PostMessage(IntPtr hWnd, UInt32 msg, uint wParam, int lParam);

const uint WM_KEYDOWN = 0x0100;
const uint WM_KEYUP = 0x0101;
const int VK_SPACE = 0x20;
const int VK_F5 = 0x74;

while(true)
{
    Process[] processes = Process.GetProcessesByName("notepad");
    foreach (Process process in processes)
    {
        PostMessage(process.MainWindowHandle, WM_KEYDOWN, VK_F5, 0);
        PostMessage(process.MainWindowHandle, WM_KEYUP, VK_F5, 0);
        Thread.Sleep(5000);
        PostMessage(process.MainWindowHandle, WM_KEYDOWN, VK_SPACE, 0);
        PostMessage(process.MainWindowHandle, WM_KEYUP, VK_SPACE, 0);
    }
}

Any idea why? Is there a better why I should go about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文