C# InputSimulator 包装器 - 如何使用它?

发布于 2024-11-19 01:48:23 字数 391 浏览 3 评论 0原文

我想模拟外部程序的键盘点击。我尝试过SendMessage、PostMessage、SendKeys,但它们不会将密钥发送到某个特定程序。所以我想尝试 SendInput 并且我已经下载了一个很好的包装器 SendInput - http://inputsimulator.codeplex.com/

我已添加程序集到我的项目中,但我还无法开始使用任何功能...

我必须做什么? 我应该添加什么“使用”?

I want to simulate keyboard click for a external program.I've tried SendMessage, PostMessage, SendKeys but they do not send the key to one specific program. So i wanted to try SendInput and i have downloaded a good wrapper for SendInput - http://inputsimulator.codeplex.com/

i have added the assembly to my project but i cannot yet start using any of the functions...

What i have to do?
What "Using" should i add?

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

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

发布评论

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

评论(4

后来的我们 2024-11-26 01:48:23

我相信您需要一个

Using WindowsInput; 

如果不是这样,您可以在对象浏览器中查看它并查看名称空间。只需右键单击解决方案资源管理器中的引用,然后单击“浏览”。

I believe you need a

Using WindowsInput; 

If that's not it, you can view it in the Object Browser and see the namespace. Just right click the reference in solution explorer and click browse.

⊕婉儿 2024-11-26 01:48:23

您可以像这样模拟程序的键盘输入:

示例代码(测试前启动记事本):

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SendKeyboardInput
{
    public class SendKey
    {
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        public void Send()
        {
            System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("notepad"); //search for process notepad
            if (p.Length > 0) //check if window was found
            {
                SetForegroundWindow(p[0].MainWindowHandle); //bring notepad to foreground
            }

            SendKeys.SendWait("a"); //send key "a" to notepad
        }
    }
}

You can simulate keyboard input to a program like this:

  • bring the program you want to send keys to the foreground using SetForegroundWindow from user32.dll

  • use the SendKeys.SendWait Method to send the actual key to the program window

Example code (launch notepad before testing):

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SendKeyboardInput
{
    public class SendKey
    {
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        public void Send()
        {
            System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("notepad"); //search for process notepad
            if (p.Length > 0) //check if window was found
            {
                SetForegroundWindow(p[0].MainWindowHandle); //bring notepad to foreground
            }

            SendKeys.SendWait("a"); //send key "a" to notepad
        }
    }
}
内心旳酸楚 2024-11-26 01:48:23

我遇到了同样的问题,但我设法通过以下 3 个步骤来做到这一点

  1. 将 InputSimulator 内容提取到像您这样的合理位置
    项目 URL

  2. 在 Visual Studio 中单击“项目”->“添加引用”并浏览到
    InputSimulator DLL 文件

  3. 通过添加“using”将 WindowsInput 命名空间添加到项目中
    Windows输入法;”

I had the same problem but i managed to do it by following these 3 steps

  1. Extract the InputSimulator contents somewhere sensible like your
    project URL

  2. In Visual Studio Click Project->Add Reference and browse to the
    InputSimulator DLL file

  3. Add the WindowsInput Namespace to the project by adding "using
    WindowsInput;"
城歌 2024-11-26 01:48:23

虽然我无法告诉您可能需要什么 using 指令,但我不确定该工具是否允许您将输入发送到特定窗口。
您链接的页面的“历史记录”部分指出:

它最初是为在 WpfKB(WPF 触摸屏键盘)项目中使用而编写的,用于模拟活动窗口的真实键盘输入。

我知道这个问题的唯一解决方案涉及 SendMessage,也许您可​​以进一步解释问题出在哪里?

While I can't tell you what using directives you might need, I'm not sure this tool is going to allow you to send input to a specific window.
The "History" section of the page you linked states:

It was originally written for use in the WpfKB (WPF Touch Screen Keyboard) project to simulate real keyboard entry to the active window.

The only solution to this problem I am aware of involves SendMessage, maybe you could further explain where the problem was with that?

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