tags:

views:

715

answers:

2

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();
  }
 }
A: 

Can't test it right now as I don't have Chrome installed at this box but, have you tried checking Chrome's code as to confirm that they handle CTRL + C the way you expect it?

arul
+3  A: 

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

casperOne
My question is: using this microsoft ui automation, i can capture selected text from other applications?
Amr ElGarhy
Yes, you can. I've edited my answer to reflect where you need to look.
casperOne
Thanks man, seams that these stuff will help me too much, but i will need to read about in details and search for some examples and write some examples my self to fully understand it.By the way its something new i didn't think about before,Thank you very much
Amr ElGarhy
Just one more thing if you please, if you know a good example using UI automation to get the selected text from other applications, please tell me about it.Thanks
Amr ElGarhy
Ric Tokyo