从剪贴板读取文本 [英] Read Text from Clipboard

查看:85
本文介绍了从剪贴板读取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Unity中的C#中读取剪贴板中的文本,然后将其设置为变量.

I am trying to read the text in the clipboard in C# in Unity and then set it to a variable.

我看过这篇文章,但是它似乎在Unity中不起作用: https://msdn.microsoft.com/zh-我们/library/kz40084e(v=vs.110).aspx

I have seen this article however it doesn't seem to work in Unity: https://msdn.microsoft.com/en-us/library/kz40084e(v=vs.110).aspx

我只希望能够阅读纯文本.没有图像或其他任何东西.我还找到了其他一些文章,但是这些代码在Unity中均不起作用.

I just want to be able to read plain text. No images or anything. I have also found a few other articles on this however none of the code works in Unity.

推荐答案

我举了一个简单的示例来说明如何使用System.Windows.Forms命名空间中的Clipboard类.事实证明,该方法需要[STAThread]方法属性才能起作用.我不知道在Unity3D C#脚本中是否可以使用.

I made a quick example to show how to use the Clipboard class from the System.Windows.Forms namespace. It turns out, that the method needed the [STAThread] method attribute to work. I don't know if that is possible to use in a Unity3D C# script.

[STAThread]
static void Main(string[] args)
{
    if (Clipboard.ContainsText(TextDataFormat.Text))
    {
        string clipboardText = Clipboard.GetText(TextDataFormat.Text);
        // Do whatever you need to do with clipboardText
    }
}

要了解有关该属性的用途的更多信息,请查看以下问题(更重要的是,其答案): [STAThread]会做什么?

To learn more about what the attribute is used for, have a look at this question (and more importantly, its answers): What does [STAThread] do?

我做了一些挖掘工作,看起来Unity3D有一个用于System Clipboard的包装器.我还没有尝试过,但是看起来它应该可以在不同的操作系统上运行,而不仅限于Windows: GUIUtility.systemCopyBuffer

I did a little bit of digging, and it looks like Unity3D has a wrapper for the System Clipboard. I haven't tried it yet, but it looks like it should work across different operating systems and not just for Windows: GUIUtility.systemCopyBuffer

这篇关于从剪贴板读取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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