【问题标题】:C# InputSimulator wrapper - how to use it?C# InputSimulator 包装器 - 如何使用它?
【发布时间】:2011-09-30 11:39:22
【问题描述】:

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

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

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

【问题讨论】:

  • “我应该添加什么“使用””。如果您使用 VS,只需在代码中编写 InputSimulator,智能就会建议您命名空间。不是吗?

标签: c# wrapper simulator sendinput


【解决方案1】:

我相信你需要一个

Using WindowsInput; 

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

【讨论】:

  • 是的,就是这样。我非常讨厌你:)
【解决方案2】:

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

  • 使用 user32.dll

  • 中的 SetForegroundWindow 将要发送密钥的程序带到前台
  • 使用SendKeys.SendWait Method 将实际密钥发送到程序窗口

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

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
        }
    }
}

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,但我设法按照这 3 个步骤解决了

    1. 将 InputSimulator 的内容提取到某个合理的位置,例如您的 项目网址

    2. 在 Visual Studio 中单击项目->添加引用并浏览到 输入模拟器DLL文件

    3. 通过添加“使用”将 WindowsInput 命名空间添加到项目中 窗口输入;"

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多