I wrote a small sample of code in C# to capture selected text from other applications:
SendKeys.SendWait('^c'); string searchedText = Convert.ToString(Clipboard.GetText());
my problem is its not working if i captured text from the browser Chrome anyone know why this happen or if there is another way to do the same task
UPDATE
I am doing this application to capture selected text from any other application and save it in a file when the user press a hot key.
protected override void WndProc(ref System.Windows.Forms.Message m) { // let the base class process the message base.WndProc(ref m); // if this is a WM_HOTKEY message, notify the parent object const int WM_HOTKEY = 0x312; if (m.Msg == WM_HOTKEY) { SendKeys.SendWait('^c'); string searchedText = Convert.ToString(Clipboard.GetText()); Save(searchedText); Clipboard.Clear(); } }
Well, you are assuming here that Ctrl-C is always going to copy text to the clipboard in every application. That’s a big no-no, you don’t know what will copy text to the clipboard.
Also, it should be said that using the clipboard for this is a very bad idea if you are not specifically trying to change the contents of the clipboard. It seems you are just using it to copy contents from another application.
That being said, I recommend that you use the Microsoft UI Automation Library for this. The reference for it is here:
http://msdn.microsoft.com/en-us/library/ms747327.aspx
There is also a good article on the subject in MSDN magazine:
http://msdn.microsoft.com/en-us/magazine/cc163288.aspx
Specifcally, if you are looking to get selected text, then you want to look at the UI Automation TextPattern Overview located at:
http://msdn.microsoft.com/en-us/library/ms745158.aspx