How to access the variable to which the OS is copied

An interesting feature that I saw in jDownloader is any links that I copy in the browser window (i.e. Ctrl + c), the copied links to the content are displayed automatically (i.e. pasting it) into my interface and it starts loading content from links, if valid.

I would like to program the same, but I am puzzled by how to access the variable that the OS is being copied to. Share your ideas.

Thanks.

+1
source share
3 answers

Adam Robinson is on the right track, but not quite right. I will try to present a โ€œlongโ€ version (as opposed to its โ€œshortโ€ version) and explain how and why I think that the solution that he offers does not reach your final goal.

As documentation, he refers to explanations; there are three different ways to monitor changes in the Windows clipboard, each of which has its own reservations:

  • Creating a clipboard viewer window that is hooked into the clipboard viewer chain and receives notifications when the contents of the clipboard have been modified by the user. (Available on all versions of Windows, but it is usually more difficult to code and thereby discourage new applications that do not have a specific need for its features.)

  • A request for the clipboard sequence number, which is a 32-bit value that changes every time the contents of the clipboard change. Your program calls the GetClipboardSequenceNumber Windows API GetClipboardSequenceNumber once and caches its value, and then each time you want to check if the contents of the clipboard have changed, you call the same function again and compare its return value with the value you cached. There are two important caveats here:

    • This feature is available only in Windows 2000 and later. This is unlikely to be a problem if you are writing .NET applications, since Framework versions have already lost W2K support in 3.0.

    • This is not a notification method, and you should not call this function repeatedly in the polling cycle. This means that you need to manually call the corresponding function and compare the serial number of the clipboard. You cannot use this method if you want to โ€œlistenโ€ and be notified immediately when the contents of the clipboard change, as you describe in your question. The documentation here is very clear:

      This method is more suitable for programs whose caching results are based on the current contents of the clipboard, and you need to know if all of them are still valid before using the results from this cache. Please note that this is not a notification method and should not be used in the polling cycle. To be notified when clipboard contents change, use the clipboard format listener or the clipboard viewer.

  • Create a clipboard format listener that registers for notification when the contents of the clipboard change. This is the ideal solution in your case, since it avoids the difficulties of creating a clipboard viewer window (option 1), but also allows you to listen and receive notifications every time the contents of the clipboard change (unlike option 2).

    The problem is that this is only available on Windows Vista and later. If you still need to focus on Windows XP (like most of us), this is really not an option for you.


Therefore, from the example that you provide in the question, it sounds to me as the only option available to you is option 1, which creates a clipboard viewer window. The documentation contains detailed information on how you configured this using the SetClipboardViewer function and listening to the WM_DRAWCLIPBOARD and WM_CHANGECBCHAIN messages. Performing this task correctly may be a difficult task for you, but, fortunately for .NET developers, others have already done the hard work for us. ("Others," I say, despite the fact that he was one of those who himself was.)

This CodeProject article is a good example. It implements three different types of hooks: a mouse hook, a keyboard hook, and a clipboard hook. The only thing that interests you is the capture of the clipboard, but you can simply add a link to the DLL in your project to immediately start using its functions.

If you are interested in the internal principles of how this works, and you want to try to code it yourself, this article seems like a fantastic description of the specific steps.

+3
source

Use the function My.Computer.Clipboard.GetText()

Also see msdn page

+1
source

Check this link on the MSDN for the clipboard. In particular, this binding binding will lead you to the section on the monitoring of clipboard contents.

The short option is that you can either track by polling for the sequence number, or by checking to see if it has changed, or you can register to listen to the changes for certain clipboard content formats. Please note that the latter is only available for Vista and higher, so you may stick to the survey, depending on your target platform.

To use these functions, you will have to declare P / Invoke an unmanaged function. Here's the PInvoke.net page in the GetClipboardSequenceNumber function, although the declaration here is C #, not VB.NET. VB.NET syntax should be (I don't have VS in front of me to check):

 <DllImport("user32.dll")> Public Shared Function GetClipboardSequenceNumber() as UInt32 End Function 
0
source

Source: https://habr.com/ru/post/1387721/


All Articles