| View previous topic :: View next topic |
| Author |
Message |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Mon Aug 29, 2011 12:10 pm Post subject: Hooking clipboard in C# |
|
|
Hi,
just wondering how's it possible to hook the clipboard without using any win32 api's?
I mean, i've done it with SetClipboardViewer for example, but it worked only for simple text areas such as notepad, explorer etc . .
for more complicated text boxes such as Microsoft office firefox visual studio and other stuff that rewriting the win32 api it's impossible using that method.
my goal is pretty much hooking Ctrl+Q for example to simulate a copy function ( Ctrl+C sending ),
so i've tried few methods for that:
SendMessage -> WM_COPY
SendKeys.Send("^(C)");
and keybd_event method
each and every one of them is triggered by clicking Ctrl+Q, but again they're not working for complicated applications
is there another way to do it?
thanks :]
_________________
Stylo |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Aug 29, 2011 12:54 pm Post subject: |
|
|
.NET has methods to handle the clipboard already, have you looked into use them?
Something like this should get you started:
| Code: |
private void button1_Click(object sender, EventArgs e)
{
// Ensure clipboard has data..
IDataObject objClipboard = Clipboard.GetDataObject();
if (objClipboard == null) return;
// Obtain data formats for the clipboard data..
string[] strFormats = objClipboard.GetFormats();
// Assume we have a string in the clipboard..
if (strFormats.Contains("System.String"))
{
// Obtain the unicode string..
if (strFormats.Contains("UnicodeText"))
richTextBox1.Text = objClipboard.GetData(DataFormats.UnicodeText).ToString();
// Obtain the Rtf formatted string..
if (strFormats.Contains("Rich Text Format"))
richTextBox1.Rtf = objClipboard.GetData(DataFormats.Rtf).ToString();
}
// Assume we have an image copied in the clipboard..
else if (strFormats.Contains("DeviceIndependentBitmap"))
{
pictureBox1.Image = (objClipboard.GetData(DataFormats.Bitmap) as Bitmap);
}
}
|
New project with a richtextbox, picturebox, and a button added to it.
I wrote this to test what you mentioned with not being able to handle special objects like Visual Studio, Firefox, etc. and they all worked fine for me. Including copying from Visual Studio and obtainin the Rtf data to apply it to the richtextbox to keep the syntax highlighting and such.
I only did two small examples (text / images) and it doesn't include all the mix/matching that can be done with the formats.
_________________
- Retired. |
|
| Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Mon Aug 29, 2011 1:34 pm Post subject: |
|
|
I already know how to handle the clipboard,
the only thing is that i'm trying to copy the action of copy function & paste function to other combination of keys - for example: Ctrl+Q
i'm hooking ctrl+q using RegisterHotKey
i use SetClipboardViewer to view the clipboard changes
waiting for WM_DRAWCLIPBOARD msg to arrive
and then writing the clipboard text into a textbox
the problem is that when i use Ctrl+Q it isn't working but when i use ctrl+c as usual it is working
all i'm trying to do is to get the same result as ctrl+c but from other key ( Q )
_________________
Stylo |
|
| Back to top |
|
 |
|