在另一个应用程序中模拟按键和按键释放?

发布于 2024-08-12 05:31:17 字数 170 浏览 2 评论 0原文

我需要与正在运行的外部应用程序交互,并发送特定的按键和消息。发布。我尝试使用 SendKeys 类,但它只完成了一半的工作,因为按键是通过立即按键释放发送到外部应用程序的。

我需要能够模拟外部应用程序的“按键按下”。我现在尝试使用 SendMessage 东西,但现在它根本不起作用:(而且我什至没有收到错误。

I need to interact with an external application running, and send specific keypresses & releases. I've tried to use the SendKeys class, but it does only half of the job, as the keypress is being sent with an immediate keyrelease to the external applications.

I need to be able to simulate a "key hold down" for the external app. I'm now trying to use the SendMessage thing, but for now it won't work at all :( and I don't even get errors.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜清冷一曲。 2024-08-19 05:31:17

好的,案子解决了。我实际上安装了 VC++ 来尝试核心 keybd_event() 函数,在它工作后我能够在 C# 中明智使用它。

这是代码,令人惊讶的是它非常简单。您需要将此 using 添加到您的代码中才能导入 dll: using System.Runtime.InteropServices;

此代码将按住“1”按钮 3 秒,然后将松开 1 秒并重复该过程。

(代码高亮显示混乱了:/,从“命名空间...”复制到最后一个括号“}”)

public class Program 
{ 
    [DllImport("user32.dll")] 
    private static extern void keybd_event(byte bVk, byte bScan, 
        uint dwFlags, UIntPtr dwExtraInfo);

    private static void Main(string[] args)
    {            
        while (true)
        {
            keybd_event((byte)0x31, (byte)0x02, 0, UIntPtr.Zero);
            Thread.Sleep(3000);

            keybd_event((byte)0x31, (byte)0x82, (uint)0x2, UIntPtr.Zero);
            Thread.Sleep(1000);
        }
    }
}

Ok, case solved. I actually installed VC++ to try the core keybd_event() function, and after it worked I was able to use it wisely in C#.

Here's the code, and surprisingly it's very simple. You'll need to add this using to your code to be able to import dll's: using System.Runtime.InteropServices;

This code will press and hold the '1' button for 3 secs, and then will release for 1 second and repeat the process.

(the code highlight got messed up :/, copy from 'namespace ...' to the last bracket '}')

public class Program 
{ 
    [DllImport("user32.dll")] 
    private static extern void keybd_event(byte bVk, byte bScan, 
        uint dwFlags, UIntPtr dwExtraInfo);

    private static void Main(string[] args)
    {            
        while (true)
        {
            keybd_event((byte)0x31, (byte)0x02, 0, UIntPtr.Zero);
            Thread.Sleep(3000);

            keybd_event((byte)0x31, (byte)0x82, (uint)0x2, UIntPtr.Zero);
            Thread.Sleep(1000);
        }
    }
}
牵你手 2024-08-19 05:31:17

您是否尝试过使用 PostMessage 发送WM_KEYDOWNWM_KEYUP

编辑

你会这样使用它(我用 C++ 编写,但你可以轻松使用 PInvoke 和 ..NET)

HWND hwnd = FindWindow(NULL,_T("Mywindow"));
PostMessage(hwnd,WM_KEYDOWN,VK_A,0);

have you tried using PostMessage to send WM_KEYDOWN and WM_KEYUP ?

Edit

You would use it this way (I am writing in C++, but you can easily use PInvoke and ..NET)

HWND hwnd = FindWindow(NULL,_T("Mywindow"));
PostMessage(hwnd,WM_KEYDOWN,VK_A,0);
倾城泪 2024-08-19 05:31:17

您可以使用 WSH Scripting Shell 来执行此操作:

var shell   = new WshShellClass();
var missing = System.Reflection.Missing.Value;

shell.SendKeys("MOO!!!", ref missing);

您所需要做的就是添加对“Windows Scripting Host Object”版本 1.0 的 COM 引用。一切都在命名空间 IWshRuntimeLibrary 中。

You can use the WSH Scripting Shell to do this:

var shell   = new WshShellClass();
var missing = System.Reflection.Missing.Value;

shell.SendKeys("MOO!!!", ref missing);

All you need to do is add a COM reference to "Windows Scripting Host Object", version 1.0. Everything is in the namespace IWshRuntimeLibrary.

悲念泪 2024-08-19 05:31:17

官方API是SendInput< /a>.

The official API is SendInput.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文