1

I wrote a program which uses global hotkeys to store a dragged text by user and read the text when she/he presses crtl+shift+w. But the program doesn't work when the dragged text be inside of other applications such as firefox ,adobe reader and etc. here my code in the method of hooking the key(s):

void hook_KeyPressed(object sender, KeyPressedEventArgs e)
        {
            //System.Threading.Thread th;

            string backup = Clipboard.GetText();
            SpeechSynthesizer speaker = new SpeechSynthesizer();
            speaker.SetOutputToDefaultAudioDevice();

            string text = null;
            SendKeys.SendWait("^{c}");
            text = Clipboard.GetText();

            //IDataObject format= Clipboard.GetDataObject();
           // string[] str = format.GetFormats();
          //  MessageBox.Show(str[0]);

            MessageBox.Show(text);
            Clipboard.SetText(backup);
            speaker.Speak(text);
        }

what it does is that storing the clipboard's text to a string and sending crtl+c keys (to store (or grab) the dragged text) and storing the text in another string and then restoring the backup(ed) text to the clipboard.

Any ideas ? thanks.

Abhishek
  • 6,912
  • 14
  • 59
  • 85
Mohammad yummy
  • 111
  • 1
  • 8
  • 1
    SendKeys is a vb6 blight that Microsoft should have (but couldn't) leave out of .NET. Many possible failure modes, one that everybody overlooks is that it cannot work when you use the debugger and started VS elevated (aka "Run as Administrator"). Use an UI Automation library instead. – Hans Passant Mar 10 '15 at 10:33
  • @HansPassant Excuse me but I didn't understand what you said, English isn't my native language.Could you explain that more easily ? thanks. – Mohammad yummy Mar 10 '15 at 11:03
  • 1
    [UI Automation](https://msdn.microsoft.com/en-us/library/ms747327%28v=vs.110%29.aspx) - the same thing that e.g. screenreaders use. Where you can deal with things like "the active text selection" in an application rather than having to rely on the clipboard. But it's a broad subject and not easy to cover within an answer on a Q&A website. – Damien_The_Unbeliever Mar 10 '15 at 11:14
  • You're also broken by assuming that the contents of the clipboard is plain text and can be "restored" by copying the content, misusing the clipboard as you currently are, and the putting the text back in the clipboard - what if the current content of the clipboard (before your code runs) is an image? – Damien_The_Unbeliever Mar 10 '15 at 11:17
  • @Damien_The_Unbeliever that's not the major question, what you've mentioned can be fixed then ,but the major is how to grab the dragged text in such the applications ? Thanks again. – Mohammad yummy Mar 10 '15 at 12:59
  • @Mohammadyummy - and I'm the second person to tell you how - use the automation system rather than trying to bodge a solution together using `SendKeys`. – Damien_The_Unbeliever Mar 10 '15 at 13:02
  • @Damien_The_Unbeliever So what would you suggest to do ? – Mohammad yummy Mar 10 '15 at 13:06
  • A very short search in Stack Exchange ("UI automation text") finds this question: http://stackoverflow.com/questions/517694/ui-automation-selected-text. Take a look and see if that helps. – AAT Mar 10 '15 at 13:46

0 Answers0