ChaosBlade

I've made a little application that uses PInvoke to set up a global key hook. Based on the keys pressed, I want to put certain data into the clipboard, And paste it into the currently active window. I tried using SendKeys ("+(v){Enter}" or something similar), But it seems to have no effect - I believe it is related to the fact I'm running on Vista, And hooking is basically cross-application injection.

Is there another way to do this

Thanks in advance, Ran.

EDIT; Thanks to the moderator, Wasn't sure if this fits in Windows.Forms, As i haven't mentioned any specific Visual elements.


Re: Windows Forms General Simulating a paste from the clipboard via code?

Rong-Chun Zhang - MSFT

Hi ChaosBlade

You can try to use Clipboard class. The Clipboard class provides methods that you can use to interact with the Windows operating system Clipboard feature. You code may be something like that.

Code Snippet

IDataObject iData = Clipboard.GetDataObject();

if (iData.GetDataPresent(DataFormats.Text))

{

this.richTextBox1.Text = (string)iData.GetData(DataFormats.Text);

}

Hope it helps.

Regards






Re: Windows Forms General Simulating a paste from the clipboard via code?

ChaosBlade

Rong-Chun, Thanks for your reply, But you perhaps misunderstood me.

I am using the Clipboard.SetText method in my application, But the pasting needs to occur at whatever active window\application at the time of pressing the hooked keyboard hotkey. I.e. I could be in a chat program, in a certain textbox and click the hotkey. My application would catch the keypress, do its logic, and set some text into the clipboard. The next stage which i can't seem to acheive, would be simulating a ctrl+v keypress at wherever the user was - Resulting in the info i posted in the clipboard pasted.

I've seen the SendInput API method, But i'm not sure how to use it correctly under .NET\Pinvoke - It uses some structs which im unsure as how to represent in .NET.





Re: Windows Forms General Simulating a paste from the clipboard via code?

ChaosBlade

Just an update to this - I've tried using the SendInput API - To no avail. I can't reliably trigger a clipboard paste even using that approach.

Any suggestions will be greatly appriciated, As i am running out of ideas.




Re: Windows Forms General Simulating a paste from the clipboard via code?

nobugz

I tried SendKeys.Send() like this:

private void timer1_Tick(object sender, EventArgs e) {
if (!this.Focused) {
Clipboard.SetText("nobugz waz here");
SendKeys.Send("^V");
}
}

It worked fine when I tested it with Notepad. Not sure why you would have a problem making this work. Be sure that the window in which you want to paste has the focus.





Re: Windows Forms General Simulating a paste from the clipboard via code?

ChaosBlade

What OS are you using I'm having issues with SendKeys on Vista, Even when specifically setting the method used in the app.config file (either SendInput or JournalHook).
It seems to just ignore the presses.

EDIT: It is notable, however, That i am using Visual Studio 2005 to develop. Im assuming the target framework for it is v2, Even though Vista runs with v3 by default. Looking at the note in the SendKeys.SendWait() function in MSDN, Would building against v3 work




Re: Windows Forms General Simulating a paste from the clipboard via code?

nobugz

I'm using XP SP2 and VS2005. The .NET 3.0 framework uses the .NET 2.0 CLR and the .NET 2.0 version of Windows Forms. Under the hood, SendKeys uses a WH_JOURNALPLAYBACK hook set with SetWindowsHookEx(). I have seen no other reports about SendKeys not working on Vista. Nothing in Product Feedback either.





Re: Windows Forms General Simulating a paste from the clipboard via code?

ChaosBlade

Well, The MSDN info page for SendKeys says it was re-written for Vista in .NET 3.0 to use SendInput. I am just wondering if migrating to the newest framework might help.




Re: Windows Forms General Simulating a paste from the clipboard via code?

nobugz

Hot dog, you're right! Hmm, this is mighty odd. I have .NET 3.0 installed but I don't have a 3.0 version of System.Windows.Forms.dll in the c:\windows\ microsoft.net\ Framework folders. I'm not aware of a special build of the .NET 3.0 framework for Vista. I just checked a non-development PC on which I installed Vista a month ago, nothing there either. Maybe this is pre-release info on an upcoming service pack. I've been hearing rumors of an upcoming SP for .NET 2.0

Meanwhile, the note in the online version of the MSDN article clearly points out that SendKeys() won't work on Vista with UAC enabled. I can't tell you how to use the .NET 3.0 specific version of SendKeys when I can't find it.





Re: Windows Forms General Simulating a paste from the clipboard via code?

ChaosBlade

Hopefully, Perhaps someone else who notices this thread will know more about that note.
Can you suggest a different approach that might work in Vista with UAC
My entire goal is just simulating a paste hotkey. I am aware such methods are usually considered as major security holes.




Re: Windows Forms General Simulating a paste from the clipboard via code?

nobugz

I'll try to get in touch with the development group at Microsoft about this. It might take a while, my primary contact is in Orlando right now. Hopefully somebody else knows this.





Re: Windows Forms General Simulating a paste from the clipboard via code?

ChaosBlade

Thank you for all your help so far Smile




Re: Windows Forms General Simulating a paste from the clipboard via code?

ChaosBlade

I forgot to ask, Would you want some sort of a repro sample I can whip one up rather quickly. Basically declaring a low level keyboard hook, and simulating a paste when a certain key combo was pressed.



Re: Windows Forms General Simulating a paste from the clipboard via code?

nobugz

I'm not running Vista on my dev PCs to try your sample. I haven't heard back from my MSFT contact yet.





Re: Windows Forms General Simulating a paste from the clipboard via code?

nobugz

Still haven't heard back. MVP doesn't mean much anymore. Shall I keep trying