Question

I had problems with copy and paste on MonoMac and found Clipboard.GetText() always returns empty string in Mono on Mac which suggested using NSPasteboard but I can't find any good examples or explanation on how to do so in Mono/C#. I think I understand the Objective-C API but the mapping the C# I find confusing so any help would be great.

Was it helpful?

Solution

Here is a simple example of copying and pasting a string:

private static string[] pboardTypes = new string[] { "NSStringPboardType" };

public static void SetText(string text)
{
    NSPasteboard.GeneralPasteboard.DeclareTypes(pboardTypes, null);
    NSPasteboard.GeneralPasteboard.SetStringForType(text, pboardTypes[0]);
}

public static string GetText()
{
    return NSPasteboard.GeneralPasteboard.GetStringForType(pboardTypes[0]);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top