Home > Enterprise >  SendKeys.SendWait() Electron App Interaction: How to set the DOM3 keyboard event "code"?
SendKeys.SendWait() Electron App Interaction: How to set the DOM3 keyboard event "code"?

Time:02-01

I'm attempting to automate an action on an Electron/javascript-based game that requires keyboard input. Example: typing "a" is supposed to activate an ability in the game, and I want to automate this with software.

Problem: I noticed that when using SendKeys.SendWait("a") in a C# desktop app, the corresponding DOM event does fire (I used an event debugger to prove that it got sent), but the "code" property in that DOM event is empty and nothing happens, whereas when I physically type the key with my keyboard, the code property is supplied and the ability activates as expected - so I suspect the game developer implemented their event handler by checking the code prop and branched off that -> when it's empty, they exit early.

To explain the problem a bit further, if I type "a" physically with my keyboard, the corresponding DOM3 browser event has key = 'a' and code = 'KeyA' but if I use SendKeys.SendWait() to send the same character, the key = 'a' again, which is correct, but the code property is an emptystring which I believe is causing the game to ignore the keyboard input. How can I make sure the code property gets set properly to 'KeyA' or whatever the virtual key was that was pressed when using SendKeys?

I looked into SendInput() from the Win32 API as well, but I'm not sure which flag is required to make sure the code property propagates successfully or if there even is one. Does anyone know if there's a workaround for what I'm experiencing?

Thanks!

CodePudding user response:

Figured this out. Needed to use SendInput like this, ensuring to do a bitwise or with the KeyEventF.Scancode flag which ensure the event gets sent as it would with a physical keyboard:

            Input[] inputs = new Input[]
            {
                new Input
                {
                    type = (int)InputType.Keyboard,
                    u = new InputUnion
                    {
                        ki = new KeyboardInput
                        {
                            wVk = 0,
                            wScan = 0x11, // w key
                            dwFlags = (uint)(KeyEventF.KeyDown | KeyEventF.Scancode),
                            dwExtraInfo = GetMessageExtraInfo()
                        }
                    }
                },
                new Input
                {
                    type = (int)InputType.Keyboard,
                    u = new InputUnion
                    {
                        ki = new KeyboardInput
                        {
                            wVk = 0,
                            wScan = 0x11, // w key
                            dwFlags = (uint)(KeyEventF.KeyUp | KeyEventF.Scancode),
                            dwExtraInfo = GetMessageExtraInfo()
                        }
                    }
                }
            };

            SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(Input)));
  • Related