Page 1 of 1

Trying to send enter key to a field

PostPosted: Thu Nov 12, 2020 11:12 pm
by andla71
I have tried for hours. I could verify that simple characters like a to z etc work but not Enter.
Running out of ideas.
Please help.
I would really appreciate it.

My test code. Nothing works for me:
Code: Select all
                KeyEvent k = new KeyEvent
                            {
                                WindowsKeyCode = (int)Keys.Enter,
                                FocusOnEditableField = true,

                                IsSystemKey = false,
                                Type = KeyEventType.Char
                            };
                            chrome.GetBrowser().GetHost().SendKeyEvent(k);

                            //KeyEvent k = new KeyEvent();

                            //k.FocusOnEditableField = true;
                            //k.WindowsKeyCode = (int)Keys.Enter;
                            //k.Modifiers = CefEventFlags.None;
                            //k.Type = KeyEventType.KeyDown;
                            //k.IsSystemKey = false;
                            //chrome.Focus();
                            //chrome.GetBrowser().GetHost().SendKeyEvent(k);

                            //Thread.Sleep(100);

                            //k = new KeyEvent();

                            //k.FocusOnEditableField = true;
                            //k.WindowsKeyCode = (int)Keys.Enter;
                            //k.Modifiers = CefEventFlags.None;
                            //k.Type = KeyEventType.KeyUp;
                            //k.IsSystemKey = false;
                            //chrome.Focus();
                            //chrome.GetBrowser().GetHost().SendKeyEvent(k);


Re: Trying to send enter key to a field

PostPosted: Fri Nov 13, 2020 11:19 am
by andla71
I got things working now.
I was checking if I could use SendMessage lparam wparam and I saw different examples.
Then I noticed that the enter key needed 3 events.
One key down
One key char
One key up

Here is my contribution
Code: Select all
                          KeyEvent k = new KeyEvent();

                            k.FocusOnEditableField = true;
                            k.WindowsKeyCode = (int)Keys.Enter;
                            k.Modifiers = CefEventFlags.None;
                            k.Type = KeyEventType.KeyDown;
                            k.IsSystemKey = false;
                            chrome.Focus();
                            chrome.GetBrowser().GetHost().SendKeyEvent(k);

                            Thread.Sleep(100);

                            k = new KeyEvent
                            {
                                WindowsKeyCode = (int)Keys.Enter,
                                FocusOnEditableField = false,

                                IsSystemKey = false,
                                Type = KeyEventType.Char
                            };
                            chrome.GetBrowser().GetHost().SendKeyEvent(k);


                            Thread.Sleep(100);

                            k = new KeyEvent();

                            k.FocusOnEditableField = true;
                            k.WindowsKeyCode = (int)Keys.Enter;
                            k.Modifiers = CefEventFlags.None;
                            k.Type = KeyEventType.KeyUp;
                            k.IsSystemKey = false;
                            chrome.GetBrowser().GetHost().SendKeyEvent(k);

Re: Trying to send enter key to a field

PostPosted: Mon Nov 16, 2020 9:32 pm
by amaitland
If the page has JavaScript that checks the key states(up/down) then char 13 wouldn't have been sufficient. Historically sending char 13 was sufficient to trigger a basic form submit. It's possible this is no longer sufficient.

https://docs.microsoft.com/en-us/windows/win32/learnwin32/keyboard-input is worth a read if you haven't already.

If you are having trouble working out the correct values to send in future then using Spy++ and notepad can be quite useful. There's a bit of noise you'll have to filter out, you can see the low level messages generated by the OS for the key press.