Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock the clipboard?

Tags:

c#

clipboard

I was wondering if there is anyway to lock and unlock the clipboard from C#. Basically, I'd be writing something into it and I do not want anyone else to write to it before I pick up my stuff.

How can I do this?

like image 498
kambamsu Avatar asked Jun 02 '10 10:06

kambamsu


People also ask

What does lock to clipboard mean?

It does protect items in the clipboard area when the lock icon is activated on an item. If an item is locked, it will not be deleted when you select the delete icon.

How do you unlock the clipboard?

Open the messaging app on your Android, and press the + symbol to the left of the text field. Select the keyboard icon. When the keyboard appears, select the > symbol at the top. Here, you can tap the clipboard icon to open the Android clipboard.


3 Answers

As @shambulator says, this isn't what the clipboard is designed to do. Perhaps you should try a different tack. Integration with legacy systems can be tackled inmany ways (most of them bad, alas!).

The system to which you only can send keystrokes: is it a Windows application? Can you dig into its window structure to explicitly set texts inside text boxes by their HWNDs? Does the system have any kind of file I/O you could use? Perhaps you could dig into its database?

like image 43
Pontus Gagge Avatar answered Sep 22 '22 20:09

Pontus Gagge


The OpenClipboard() API function, followed by EmptyClipboard() locks the clipboard until CloseClipboard is called. You should probably pass a window handle of a window in the target process, but zero works.

Here's a C# console app that worked for me when I wanted to test that my application gracefully handled a rogue application locking the clipboard.

class Program
{
    [DllImport("user32.dll", EntryPoint = "OpenClipboard", SetLastError = true)]
    private static extern bool OpenClipboard(IntPtr hWndNewOwner);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool CloseClipboard();

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool EmptyClipboard();

    static void Main(string[] args)
    {
        OpenClipboard(new IntPtr(0));
        EmptyClipboard();
        Console.WriteLine("Clipboard locked. Press enter to unlock and exit.");
        Console.ReadLine();
        CloseClipboard();
    }
}

These declarations came from pinvoke.net.

like image 114
Hans Passant Avatar answered Sep 25 '22 20:09

Hans Passant


Do NOT do this. The clipboard is there for the convenience of the USER, not the PROGRAMMER! You will cause errors and crashes in other programs that are trying to (properly) use the clipboard, including programs that are monitoring for updates.

“Programs should not transfer data into our out of the clipboard without an explicit instruction from the user.”
— Charles Petzold, Programming Windows 3.1, Microsoft Press, 1992

like image 36
Chris Thornton Avatar answered Sep 24 '22 20:09

Chris Thornton