windows 触发操作系统以编程方式复制(ctrl+c 或 Ctrl-x)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2886756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:30:07  来源:igfitidea点击:

Trigger OS to copy (ctrl+c or Ctrl-x) programmatically

c#windowsapisendkeys

提问by Crash893

I'm working on a program to trigger cut and pastes

我正在开发一个程序来触发剪切和粘贴

Pastes I have no problem with (I just dump a string into the clipboard)

我没有问题的粘贴(我只是将一个字符串转储到剪贴板中)

Cut and or Copys are proving to be a little more difficult

剪切和/或复制被证明有点困难

The program I have is out of focus and has several hot keys registered with the os CTRL+ALT+2CTRL+ALT+3etc)

该方案我已经是焦点的出来,并具有与操作系统注册的几个快捷键CTRL+ ALT+ 2CTRL+ ALT+3等)

That I want to use to trigger Windows to copy anything that is highlighted in the window that is focused

我想用它来触发 Windows 复制焦点窗口中突出显示的任何内容

I tried doing a sendkeys

我试着做一个sendkeys

SendKeys.Send("^c");

but that seems to work once or twice if at all then stop working.

但这似乎可以工作一两次,如果有的话就停止工作。

is there a better way to try to trigger windows into coping highlighted content on a different window

有没有更好的方法来尝试触发窗口处理不同窗口上的突出显示内容

回答by Chris Schmich

One way to do this is by using the Win32 SendInputfunction. With SendInput, you have to simulate both the key down and key up events in order for the full key press to register. To simulate CTRL+C, you'd have to do:

一种方法是使用 Win32SendInput函数。使用SendInput,您必须同时模拟按键按下和按键按下事件才能注册完整的按键。要模拟CTRL+ C,您必须执行以下操作:

  • CTRLkey down
  • Ckey down
  • Ckey up
  • CTRLkey up
  • CTRL按下键
  • C按下键
  • C调高
  • CTRL调高

pinvoke.nethas some examples of SendInputusage. One issue to be mindful of is if the key is already pressed. You can use GetAsyncKeyStateto only simulate a key down event if the key is not already down.

pinvoke.net有一些SendInput使用示例。需要注意的一个问题是按键是否已按下。GetAsyncKeyState如果键尚未按下,您可以使用仅模拟键按下事件。

Below is some example code of how you could simulate CTRL+C. With the code below, you can simply call Keyboard.SimulateKeyStroke('c', ctrl: true);Note that this works as if the user literally pressed CTRL+C, so the active application will behave as it always does when such an event happens (i.e. if nothing is normally copied, then nothing will be copied with this method, either).

下面是一些关于如何模拟CTRL+ 的示例代码C。使用下面的代码,您可以简单地调用Keyboard.SimulateKeyStroke('c', ctrl: true);注意,这就像用户按字面上按下CTRL+ 一样工作C,因此活动应用程序将在发生此类事件时像往常一样运行(即,如果通常不复制任何内容,则不会复制任何内容)这种方法,要么)。

Edit:See David's comment below about batching the sent input. The code below should be sending the entire sequence of input events through a single call to SendInputto avoid being interleaved (and misinterpreted) with real user input events.

编辑:请参阅下面大卫关于批处理发送的输入的评论。下面的代码应该通过单个调用发送整个输入事件序列,SendInput以避免与真实用户输入事件交错(和误解)。

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;

namespace SimulateKeys
{
    static class Keyboard
    {
        public static void SimulateKeyStroke(char key, bool ctrl = false, bool alt = false, bool shift = false)
        {
            List<ushort> keys = new List<ushort>();

            if (ctrl)
                keys.Add(VK_CONTROL);

            if (alt)
                keys.Add(VK_MENU);

            if (shift)
                keys.Add(VK_SHIFT);

            keys.Add(char.ToUpper(key));

            INPUT input = new INPUT();
            input.type = INPUT_KEYBOARD;
            int inputSize = Marshal.SizeOf(input);

            for (int i = 0; i < keys.Count; ++i)
            {
                input.mkhi.ki.wVk = keys[i];

                bool isKeyDown = (GetAsyncKeyState(keys[i]) & 0x10000) != 0;

                if (!isKeyDown)
                    SendInput(1, ref input, inputSize);
            }

            input.mkhi.ki.dwFlags = KEYEVENTF_KEYUP;
            for (int i = keys.Count - 1; i >= 0; --i)
            {
                input.mkhi.ki.wVk = keys[i];
                SendInput(1, ref input, inputSize);
            }
        }

        [DllImport("user32.dll")]
        static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);

        [DllImport("user32.dll")]
        static extern short GetAsyncKeyState(ushort vKey);

        struct MOUSEINPUT
        {
            public int dx;
            public int dy;
            public uint mouseData;
            public uint dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }

        struct KEYBDINPUT
        {
            public ushort wVk;
            public ushort wScan;
            public uint dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }

        struct HARDWAREINPUT
        {
            public int uMsg;
            public short wParamL;
            public short wParamH;
        }

        [StructLayout(LayoutKind.Explicit)]
        struct MOUSEKEYBDHARDWAREINPUT
        {
            [FieldOffset(0)]
            public MOUSEINPUT mi;

            [FieldOffset(0)]
            public KEYBDINPUT ki;

            [FieldOffset(0)]
            public HARDWAREINPUT hi;
        }

        struct INPUT
        {
            public int type;
            public MOUSEKEYBDHARDWAREINPUT mkhi;
        }

        const int INPUT_KEYBOARD = 1;
        const uint KEYEVENTF_KEYUP = 0x0002;

        const ushort VK_SHIFT = 0x10;
        const ushort VK_CONTROL = 0x11;
        const ushort VK_MENU = 0x12;
    }

    class Program
    {
        static void Main(string[] args)
        {
            Thread.Sleep(3000);
            Keyboard.SimulateKeyStroke('c', ctrl: true);
        }
    }
}

回答by philiphobgen

If you can get the selected text from the focused window (maybe an easier problem to solve) then you're better off using the SetTextmethod of the System.Windows.Forms.Clipboardclass.

如果您可以从聚焦窗口中获取所选文本(可能是一个更容易解决的问题),那么您最好使用类的SetText方法 System.Windows.Forms.Clipboard