Skip to content

SendKeyEvent does not work as expected #2591

@Cereal-Killa

Description

@Cereal-Killa

Could you please confirm that SendKeyEvent is supposed to simulate a "real" key press on the page, just like pressing a key on the keyboard?

The reason I'm asking is that it partially works for me, meaning, the keys are send, be it a char or the enter key, or tab key, no problem, however... in a few pages already I noticed that it does not trigger all the field events as pressing the keys on the real keyboard.

It's not just one website that I've noticed that, let's suppose you have to enter the zip code in order to choose the city from a combo box, sendind the zip code via SendKeyEvent fills-in the field no problem but does not act like a real key press, if I happen to simply delete the last char using a real keyboard and re-type it then the field events are triggered as expected.

I noticed the issue with plain javascript websites, jquery forms and angularjs forms, on all cases the form is filled but the keys sent do not trigger the events that are bound to the controls.
What version of the product are you using?

What version are you using?
Nuget 63.0.3

On what operating system?
Windows 8.1 x64

Are you using WinForms, WPF or OffScreen?
Winforms

What steps will reproduce the problem?
See above, will try to get a demo url.

Please provide detailed information here, enough for someone else to reprodce your problem.
Please no binary (zip, etc) links, fork the MinimalExample and push your changes to GitHub. (Alternatively use a code sharing service list Gist or Pastebin).
What is the expected output? What do you see instead?

Thanks

                [DllImport("user32.dll")]
                public static extern int ToUnicode(uint virtualKeyCode, uint scanCode,
                    byte[] keyboardState,
                    [Out, MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
                    StringBuilder receivingBuffer,
                    int bufferSize, uint flags);

        /// <summary>
        /// https://stackoverflow.com/a/6949520/450141
        /// </summary>
        static string GetCharsFromKeys(Keys keys, bool shift, bool altGr)
        {
            var buf = new StringBuilder(256);
            var keyboardState = new byte[256];
            if (shift)
                keyboardState[(int)Keys.ShiftKey] = 0xff;
            if (altGr) {
                keyboardState[(int)Keys.ControlKey] = 0xff;
                keyboardState[(int)Keys.Menu] = 0xff;
            }
            ToUnicode((uint)keys, 0, keyboardState, buf, 256, 0);
            return buf.ToString();
        }

        static void SendKeys(ChromiumWebBrowser browser)
        {
            KeyEvent[] events = new KeyEvent[] {
                new KeyEvent() { FocusOnEditableField = true, WindowsKeyCode = GetCharsFromKeys(Keys.R, false, false)[0], Modifiers = CefEventFlags.None, Type = KeyEventType.Char, IsSystemKey = false }, // Just the letter R, no shift (so no caps...?)
                new KeyEvent() { FocusOnEditableField = true, WindowsKeyCode = GetCharsFromKeys(Keys.R, true, false)[0], Modifiers = CefEventFlags.ShiftDown, Type = KeyEventType.Char, IsSystemKey = false }, // Capital R?
                new KeyEvent() { FocusOnEditableField = true, WindowsKeyCode = GetCharsFromKeys(Keys.D4, false, false)[0], Modifiers = CefEventFlags.None, Type = KeyEventType.Char, IsSystemKey = false }, // Just the number 4
                new KeyEvent() { FocusOnEditableField = true, WindowsKeyCode = GetCharsFromKeys(Keys.D4, true, false)[0], Modifiers = CefEventFlags.ShiftDown, Type = KeyEventType.Char, IsSystemKey = false }, // Shift 4 (should be $)
            };

            foreach (KeyEvent ev in events) {
                Thread.Sleep(100);
                browser.GetBrowser().GetHost().SendKeyEvent(ev);
            }
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions