触发操作系统以编程方式复制(ctrl+c 或 Ctrl-x)
我正在开发一个程序来触发剪切和粘贴
粘贴我没有问题(我只是将字符串转储到剪贴板中)
剪切和/或复制被证明有点困难
我的程序失去焦点并且有操作系统注册的几个热键 CTRL+ALT+2 CTRL+ALT +3 等)
我想用它来触发 Windows 复制焦点窗口中突出显示的任何内容
我尝试执行 sendkeys
SendKeys.Send("^c");
但似乎工作一次或两次(如果有的话)然后停止工作。
有没有更好的方法来尝试触发窗口来处理不同窗口上突出显示的内容
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+2 CTRL+ALT+3 etc)
That I want to use to trigger Windows to copy anything that is highlighted in the window that is focused
I tried doing a 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法是使用 Win32
SendInput
函数。使用SendInput
,您必须模拟按键按下和按键按下事件,以便注册完整的按键操作。要模拟 CTRL+C,您必须执行以下操作:pinvoke.net 有一些
SendInput
使用示例。需要注意的一个问题是该键是否已被按下。您可以使用GetAsyncKeyState
仅在按键尚未按下时模拟按键按下事件。下面是一些如何模拟 CTRL+C 的示例代码。使用下面的代码,您可以简单地调用
Keyboard.SimulateKeyStroke('c', ctrl: true);
请注意,这就像用户确实按下了 CTRL+C,因此当此类事件发生时,活动应用程序将像往常一样运行(即,如果正常情况下没有复制任何内容,则使用此方法也不会复制任何内容)。编辑:请参阅下面 David 关于批量发送的输入的评论。下面的代码应该通过一次调用
SendInput
发送整个输入事件序列,以避免与真实的用户输入事件交错(和误解)。One way to do this is by using the Win32
SendInput
function. WithSendInput
, 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:pinvoke.net has some examples of
SendInput
usage. One issue to be mindful of is if the key is already pressed. You can useGetAsyncKeyState
to only simulate a key down event if the key is not already down.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).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
SendInput
to avoid being interleaved (and misinterpreted) with real user input events.如果您可以从聚焦窗口中获取选定的文本(也许是一个更容易解决的问题),那么您最好使用
System.Windows.Forms.Clipboard
的SetText
方法代码>类。If you can get the selected text from the focused window (maybe an easier problem to solve) then you're better off using the
SetText
method of theSystem.Windows.Forms.Clipboard
class.