1

I have a project that needs to capture highlighted text from a web browser(chrome, firefox etc.). In my project, the user highlights a text, for example a word from a web browser. And the program translates the word to the language that selected before using google translate. I have managed to capture highlighted text from some applications like notepad, but I especially need to do that with web browsers.

Could anybody help me with that. I searched all of the documents and tried all of the approaches but I couldn't achieve my goal. The project is for my college to graduate.

I would greatly appreciate some help here. Thank you!

Ecehan Ece
  • 98
  • 9

1 Answers1

1

Put this on top of your class:

    [DllImport("user32.dll")]
    static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);        

Then a method to recieve text.

    static uint KEYEVENTF_KEYUP = 2;
    static byte VK_CONTROL = 0x11;
   public static string gettext()
    {
        string message;
        try
        {
            Thread.Sleep(100);
            message=Clipboard.GetText();
            return message;
        }
        catch(Exception)
        {
            gettext();
        }
    }
   public static string highlightedtext()
    {
       string output;
       keybd_event(VK_CONTROL,0,0,0);
       keybd_event (0x43, 0, 0, 0 ); 
       keybd_event (0x43, 0, KEYEVENTF_KEYUP, 0);
       keybd_event (VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
       output=gettext();
       return output;
    }

NOTE: Sometimes the clipboard is in use and will give exception so you need try and catch , If it catch exception , do it again until it's not.

Advantage of using these code.:

1.If you just want something in the clipboard , call gettext()

2For highlighted text , call highlightedtext()

Anyone see errors please edit my post.

Thank you

EDIT!!: Adding static is use for main(). If you aren't use with main, Remove static

Edit2!!:Adding static for all outside variables!! Please tell me any more error.

Poomrokc The 3years
  • 1,099
  • 2
  • 10
  • 23
  • Thank you so much for your help. I put that code to my project but can not call higlightedtext() or gettext() from main(). Where do I do wrong? – Ecehan Ece Apr 22 '14 at 11:28
  • did you put it into the same class? Also, what error does it give? Or visual studio did not regcognize it? – Poomrokc The 3years Apr 22 '14 at 11:34
  • Yes I put it into the same class. Error is "An object reference is required for the non-static field, method, or property". – Ecehan Ece Apr 22 '14 at 11:41
  • @EcehanEce Ohhh , main is static. You must add static to both method. See the edit. – Poomrokc The 3years Apr 22 '14 at 12:16
  • I added static gettext(), but contents of the highlightedtext() like VK_CONTROL made the same error. – Ecehan Ece Apr 22 '14 at 12:26
  • static all!! I'm sorry , I did not tested this yet. see second edit. With the same error on any variable , please add static. – Poomrokc The 3years Apr 22 '14 at 12:32
  • Thank you man :) The program is working, but isn't getting me the highlighted text from a web browser. Am I doing something wrong? I appreciate your help. – Ecehan Ece Apr 22 '14 at 12:43
  • @EcehanEce I thought you would call this inside a global hotkey. See [this](http://stackoverflow.com/questions/3654787/global-hotkey-in-console-application) Try to understand global hotkeys,my first time with it wasn't so nice. And when you got where to put the highlighttext(); **GAME OVER**. Usage: highlight the text then press the hotkey. This is all I can help. By the way , why don't you use form application. That's easier to set up hotkeys and these kind of stuff. Wish you luck :). – Poomrokc The 3years Apr 22 '14 at 13:13