0

I have written a small Console application which Copies the highlightes text from a web application. I am trying to do it two ways

1) via simple Sendkeys

SendKeys.SendWait("^C");
 Application.DoEvents();

2) via SendInput

Keyboard.SimulateKeyStroke('c', ctrl: true);

    public static void SimulateKeyStroke(char key, bool ctrl = false, bool alt = false, bool shift = false)
                    {
                        List<ushort> keys = new List<ushort>();

                        if (ctrl)
                            keys.Add(VK_CONTROL);

                        if (alt)
                            keys.Add(VK_MENU);

                        if (shift)
                            keys.Add(VK_SHIFT);

                        keys.Add(char.ToUpper(key));

                        INPUT input = new INPUT();
                        input.type = INPUT_KEYBOARD;
                        int inputSize = Marshal.SizeOf(input);

                        for (int i = 0; i < keys.Count; ++i)
                        {
                            input.mkhi.ki.wVk = keys[i];

                            bool isKeyDown = (GetAsyncKeyState(keys[i]) & 0x10000) != 0;

                            if (!isKeyDown)
                                SendInput(1, ref input, inputSize);
                        }

                        input.mkhi.ki.dwFlags = KEYEVENTF_KEYUP;
                        for (int i = keys.Count - 1; i >= 0; --i)
                        {
                            input.mkhi.ki.wVk = keys[i];
                            SendInput(1, ref input, inputSize);
                        }
                    }

I m highlighting the text on web application in Chrome and running my console application thru a ShortKey.

It works (from both ways), when I highlight a label field.

but it doesn't work (neither from method 1 or method2 ) when I highlight any input field.

enter image description here

For ex. My web application has label Contact 2 (above pic) and next it it input field. When I highlight Lable i.e Contact2 (by double clicking) and run my console application (by pressing shorkey), i get highlighted text but when i do that same for input field, i get nothing.

Why is it so that Chrome is accepting Copy commnad for label but not for input.

Any help is appreciated.

Rupesh
  • 545
  • 2
  • 8
  • 19
  • How do you trigger that your code is executed? Can you debug your application and use a break point to check that your code is actually activated? – NineBerry Mar 15 '16 at 14:25
  • @Nineberry: I try to print on console (Console.writeLine). It is hard to debug, because, if i debug then my current window is VisualStudio instead of Chrome. I have add print at many location in my code. And code is executed every time. The only problem is Chrome is accepting Copy commnad for label but not for input – Rupesh Mar 15 '16 at 15:06
  • You haven't answered the question: How do you trigger that your code is executed? – NineBerry Mar 15 '16 at 15:14
  • @NineBerry: I trigger my code using ShortCutKey.. – Rupesh Mar 15 '16 at 15:23
  • What is the ShortCutKey that you have registered? Show us some code where you deal with invoking your code. – NineBerry Mar 15 '16 at 15:40
  • @NineBerry: Thanks for your help. When I add a time to my form, it works, but without timer it doesn't work – Rupesh Mar 16 '16 at 15:13
  • So I support that your shortcut does not work. What have you registered as a shortcut? – NineBerry Mar 16 '16 at 15:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106489/discussion-between-rupesh-and-nineberry). – Rupesh Mar 16 '16 at 15:48

1 Answers1

1

I have a sample application with a timer enabled. In the timer event, there is the following code:

  private void timer1_Tick_1(object sender, EventArgs e)
  {
      SendKeys.SendWait("^c");
      Application.DoEvents();

      if (Clipboard.ContainsText())
      {
          var lText = Clipboard.GetText();
          textBox1.AppendText(lText);
      }
  }

Notice the use of a small "c" instead of a capital "C", since Control+Shift+C will trigger the Developer tools in Chrome.

This does successfully print the selected text in input fields in websites in Chrome.

If this does not work for you, I see two possibilities:

  1. You try to get the text from a password field. This will not work.
  2. You put the focus to a different application (your own application) for triggering your code, so that the input field is not the control that has the focus at the moment your code is executed.

If this does work, but your approach doesn't, then your mechanism to trigger the code (using some form of global keyboard shortcut) does not work. Maybe because the keyboard shortcut you use is already interpreted by the input field.

NineBerry
  • 26,306
  • 3
  • 62
  • 93
  • @Niberry: I am not taking text password field. It is just a normal input field. In fact the form has around 50 labels and 50 fields. for all labels I am getting the highlighted text but not for fields. I dont think focus is changing becuase when i highlight the LABEL (by double clicking) it works but when i do the same for input field, it doesn't work – Rupesh Mar 15 '16 at 15:33
  • Have you tested this with other web sites? For example, my sample application works with the search box on youtube and other sites. Maybe there is something special about the web page you target – NineBerry Mar 15 '16 at 15:36
  • is it?. i just tested on you tube and stackoverflow. I am facing the same problem on these websites also. – Rupesh Mar 15 '16 at 15:38