Search code examples
c#hotkeyssendkeyssendinput

Sendkey or SendInput doesn't copy from input field


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.


Solution

  • 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.