如何在C#中向Windows发送CTRL + C键? [英] How to send CTRL+C keys to windows in C#?

查看:417
本文介绍了如何在C#中向Windows发送CTRL + C键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想复制在Windows中任何地方突出显示的文本,然后将其发布到我的文本框中进行翻译。



Windows命令复制突出显示的内容也很好。



我尝试了什么:



SendKeys。发送(^(C));







SendKeys.SendWait(^( C));

I want to copy the text that is highlighted anywhere in Windows and post it in my textbox to translate it.

An order for Windows to copy whatever is highlighted is fine too.

What I have tried:

SendKeys.Send("^(C)");

and

SendKeys.SendWait("^(C)");

推荐答案

这不起作用,因为 SendKeys()将键击发送到当前应用程序(您的),而不是任何可能包含文本的应用程序的窗口。要复制其他应用程序中的文本,必须在包含所选文本的窗口为活动窗口时按Ctrl-C(并支持复制到剪贴板)。



全部您可以在应用程序中查看剪贴板中的新数据并自动将其插入到文本框中。但在此之前考虑可能的副作用(即使您只是想将某些内容复制到不同的应用程序,所有内容都会被粘贴)。
This won't work because SendKeys() sends the keystrokes to the current application (yours) and not to any window of other applications that might contain text. To copy text from other applications you have to press Ctrl-C when the window containing the selected text is the active one (and supports copying to the clipboard).

All you can do from within your application is watching the clipboard for new data and inserting that into your text box automatically. But before doing so think about the possible side effects (everything is pasted even if you just want to copy something to a different application).


并不像您想象的那样简单......

但是这应该有效:

Not as simple as you may think...
But this should work:
Process[] local = Process.GetProcessesByName("Name of process to copy from");
if (local.Length > 0)
    {
    Process p = local[0];
    IntPtr h = p.MainWindowHandle;
    SetForegroundWindow(h);
    p.WaitForInputIdle();
    SendKeys.SendWait("^(c)");
    p.WaitForInputIdle();
    if (Clipboard.ContainsText())
        {
        Console.WriteLine(Clipboard.GetText());
        }
    }


Quote:

我想复制在Windows中任何地方突出显示的文本,并将其发布到我的文本框中进行翻译。

I want to copy the text that is highlighted anywhere in Windows and post it in my textbox to translate it.



这是一个错误的好主意。您正在用另一个更复杂的问题替换问题,在Windows上打开的每个应用程序都可以有一些选定的文本。你怎么知道选择哪一个?



我的建议:

使用经典的Windows复制/粘贴。


This is a wrong good idea. You are replacing a problem with another more complicated, every app opened on windows can have some selected text. How can you know which one to choose ?

My advice:
Use the classical windows Copy/Paste.


这篇关于如何在C#中向Windows发送CTRL + C键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆