【问题标题】:How use C# to send Key Down & Key Down to another application如何使用 C# 将 Key Down 和 Key Down 发送到另一个应用程序
【发布时间】:2020-02-16 11:10:22
【问题描述】:

我有一个解决方案,用于将按键输入发送到另一个应用程序,但是当我发送“Key Down”时它实际上并没有按住那个键,我需要在某些情况下基本上发送垃圾邮件“key Down”一种更新循环。

如果有人能告诉我如何(使用 winforms)向另一个应用程序发送假的 Key Down,我将不胜感激,但我不是在寻找击键。

[DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll")]
        public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        public static void SendKeystroke(Keys k)
        {
            const uint WM_KEYDOWN = 0x100;

            IntPtr WindowToFind = FindWindow(null, "application");

            IntPtr result3 = SendMessage(WindowToFind, WM_KEYDOWN, ((IntPtr)(ushort)k), (IntPtr)0);
        }

        public static void EndKeyStroke(Keys k)
        {
            const uint WM_KEYUP = 0x101;

            IntPtr WindowToFind = FindWindow(null, "application");

            IntPtr result3 = SendMessage(WindowToFind, WM_KEYUP, ((IntPtr)(ushort)k), (IntPtr)0);
        }

【问题讨论】:

    标签: c# input keyboard key


    【解决方案1】:

    当按住某个键时,应用程序会被带有WM_KEYDOWN 的消息泵淹没。只有松开按键时才会发送 WM_KEYUP。

    【讨论】:

    • 那么,我应该多久发送一次 Key Down?模拟真实输入。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-27
    相关资源
    最近更新 更多