1 year ago

#332250

test-img

Bob the builder

Simulate the UP arrow key press on a keyboard for a test executed by TestHost

I am trying to programmatically simulate the pressing of the UP arrow key on the keyboard for an end-to-end test. The test runs under TestHost in Visual Studio.

My work is divided in 2 parts:

1- The function in the below code is inserted in the application under test. TestHost is not involved here. It works. The UP arrow is received by the control and it moves the selected image. There is an image in the control. The control inherits from UserControl (WinForms).

2- TestHost simulates the key pressed/up and is also supposed to receive it from the system. When TestHost runs the code, the control does not receive the UP arrow (or does not seem because its coordinates (x,y) remain constant).

The control inherits from the UserControl class (WinForms). Also there is no cross-thread exception because I used Control.InvokeRequired().

I suspect two causes of the issue:

1- TestHost does not have the capacity to pump a message queue.

2- The application under test does not receive the message because it runs at an higher security level than TestHost. see Can an application block calls to SendInput?

From my understanding, I dont think the second cause is valid because the application under test does not run. Instead, its code is run by TestHost.

This is a follow-up question on Simulate the UP arrow key press (or other extended keys) on a keyboard

Do you know why \ can you suggest another solution?

    public void SimulateKeystrokeEventMethod()
    {
        Thread.Sleep(10000);
        IntPtr handle = DesignPlanHelper.Handle;
        ushort key = (ushort)KB_UP;
        int repetitions = 30;

        Control control = Control.FromHandle(handle);

        if (control.InvokeRequired)
        {
            System.Action safeStimulation = delegate
            {
                SetFocus(handle);

                // The position of the antenna in the Design plan
                Clicks(handle, 330, 401);

                Input keyDown = new Input
                {
                        type = (int) InputType.Keyboard,
                        u = new InputUnion
                        {
                            ki = new KeyboardInput
                            {
                                wVk = 0, wScan = key, dwFlags = (uint) (KeyEventF.KeyDown | KeyEventF.Scancode), dwExtraInfo = GetMessageExtraInfo()
                            }
                        }
                };

                Input keyUp = new Input
                {
                    type = (int) InputType.Keyboard,
                    u = new InputUnion
                    {
                        ki = new KeyboardInput
                        {
                            wVk = 0, wScan = key, dwFlags = (uint) (KeyEventF.KeyUp | KeyEventF.Scancode), dwExtraInfo = GetMessageExtraInfo()
                        }
                    }
                };

                Input[] inputs = new Input[repetitions * 2];
                for (int input = 0; input < repetitions*2;)
                {
                    inputs[input] = keyDown;
                    input++;
                    inputs[input] = keyUp;
                    input++;
                }

                // You must pass all Key presses in a single call to this function.
                SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(Input)));
            };
            control.Invoke(safeStimulation);
        }
        else
        {
            //copy some code from above.
        }
    }

Your help is much appreciated!

c#

winforms

winapi

win32gui

vstesthost

0 Answers

Your Answer

Accepted video resources