Trying to send enter key to a field

Having problems with building or using the CefSharp .NET binding? Ask your CEF-related questions here. Please ask general usage questions on StackOverflow.

Moderator: amaitland

Trying to send enter key to a field

Postby andla71 » Thu Nov 12, 2020 11:12 pm

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);

andla71
Techie
 
Posts: 28
Joined: Sat Oct 31, 2020 12:19 pm

Re: Trying to send enter key to a field

Postby andla71 » Fri Nov 13, 2020 11:19 am

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);
andla71
Techie
 
Posts: 28
Joined: Sat Oct 31, 2020 12:19 pm

Re: Trying to send enter key to a field

Postby amaitland » Mon Nov 16, 2020 9:32 pm

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.
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1290
Joined: Wed Jan 14, 2015 2:35 am


Return to CefSharp Forum

Who is online

Users browsing this forum: No registered users and 13 guests