inbit
inbit

Reputation: 21

How to copy and paste text from any windows application (second clipboard)

I have a windows desktop application in c# (WPF), which is basically an issue managing application for a call center. It manages (adds, edits, deletes,...) customer issues (records in database), where each issue has numerous fields (textboxes, textareas, checkboxes,...) While the issue is generated, a user often copies/pastes data from other resources to this and other applications (excel, notepad, browser...), and has a windows clipboard full of valuable data.

The problem i don't know how to solve is: A user selects a random text in any textfield (textbox for example) in this program, and copies the data. but this data can't be copied to the clasic windows clipboard

Clipboard.SetText(Textbox1.Text);

because clipboard already has a valuable data, that can't be lost. So i'd like to create a second clipboard which listens to all keyboard input. And whenever a certain combination of keys is pressed it reads the selected text (only text is needed), from any application (notepad, excel, my program,...). Similarly with another combination of keys it pastes that copied text to any application (notepad, excel,..)

Windows already has that functionaliy with the shortcuts CTRL+C and CTRL+V, which work in any application that doesn't override those shortcuts. I'd like to the same for my custom clipboard application/service, with different shortcuts. But i don't know how to get and paste the data to any textfield in any windows application. In a nutshell, i'd need something like:

string clipboardData;

public void OnSpecialCopyShortcutPressed() {
  clipboardData = SelectedTextAnywhereInWindowsOrAnyWindowsApp.Text;
}

public void OnSpecialPasteShortcutPressed() {
  focusedTextFieldAnywhereInWindowsOrAnyWindowsApp.Text = clipboardData;
}

Basicaly i'd like to have two windows clipboards.

Any pointers would be great. Thank you.

--- edit ---

found the solution for hooking up to the keys pressed here global keyboard hook Still looking for a way to "hook up" to active apps and selected text for copying and focused controls for pasting copied text.

Upvotes: 2

Views: 6706

Answers (3)

Chris Thornton
Chris Thornton

Reputation: 15817

You can't have two windows clipboards. You can put something together that will act like a clipboard in certain situations, but it's not going to work for every app (or most apps). The best you could hope for is to capture text with hotkeys in the app that you have control of, and use sendkeys to type the data into other apps that are cooperative. Forget fancy formats like HTML, RTF, Bitmap.
I wrote the original windows clipboard extender (ClipMate) in 1991, so I've seen all sorts of ideas on how to improve the clipboard. This is one of the worst. I include this commentary not to poke fun at you or your client, but in hopes that you can use it to persuade your client from wasting lots of time and money on this boondoggle.

Upvotes: 1

Aamir
Aamir

Reputation: 15546

What I would do in a situation like this is:

  1. Create a service named 'MyClipBoard' or whatever.
  2. Create global keyboard hooks to look for whatever key combinations you are trying to look. i.e., Alternatives of Ctrl + C and Ctrl + V
  3. Whenever you encounter a key combination meant for copying, look for the active window and then get the selection from that window.
  4. Copy the selection to a memory area in your 'MyClipBoard'.
  5. When you encounter keyboard pasting combination, paste the data in your clipboard to the activewindow's focussed window.

This all requries a good knowledge of Keyboard hooks and Windows APIs. And this may not be the perfect solution either.

The most important question is: Why do you want to do this? This doesn't seem to be a correct way of tackling the problem.

Upvotes: 0

EKS
EKS

Reputation: 5623

There are several optiosn to share data between applications: IPC
Memory mapped file
File on filesystem
Using the network via localhost.

Have you explorered these options?

Perhaps read up this article?

Upvotes: 0

Related Questions