Serge
Serge

Reputation: 6692

From CTRL+"something" to a string with a code sample

    public virtual void Send(string keysToType, ActionListener actionListener)
    {
        if (heldKeys.Count > 0) keysToType = keysToType.ToLower();

        CapsLockOn = false;
        foreach (char c in keysToType)
        {
            short key = VkKeyScan(c);
            if (c.Equals('\r')) continue;

            if (ShiftKeyIsNeeded(key)) SendKeyDown((short) KeyboardInput.SpecialKeys.SHIFT, false);
            if (CtrlKeyIsNeeded(key)) SendKeyDown((short) KeyboardInput.SpecialKeys.CONTROL, false);
            if (AltKeyIsNeeded(key)) SendKeyDown((short) KeyboardInput.SpecialKeys.ALT, false);
            Press(key, false);
            if (ShiftKeyIsNeeded(key)) SendKeyUp((short) KeyboardInput.SpecialKeys.SHIFT, false);
            if (CtrlKeyIsNeeded(key)) SendKeyUp((short) KeyboardInput.SpecialKeys.CONTROL, false);
            if (AltKeyIsNeeded(key)) SendKeyUp((short) KeyboardInput.SpecialKeys.ALT, false);
        }

        actionListener.ActionPerformed(Action.WindowMessage);
    }

I need to send a list of keyboard shortcuts to that method

CTRL + A, CTRL + End, etc.

But I don't know how to build such a string.

This far I wrote this:

 string shortcuts;
 // shortcuts = "\CTRL + A" + "\CTRL + End";
 Send(shortcuts, myactionlistener)

Upvotes: 1

Views: 2748

Answers (4)

sarh
sarh

Reputation: 6627

In my experience of UI automation there were some cases where solution of @m3tikn0b didn't work for me, particularly ALT+DOWN and ALT+UP, not sure why. To make them work I had to use WinForm's SendKeys, for example for ALT+DOWN it is:

SendKeys.SendWait("%({DOWN})");

It has pretty rich syntax described in docs for Send method. However it was not suitable in all my cases as well, so I had to use TestStack.White for the most cases and SendKeys for some rare exclusions.

Upvotes: 0

m3tikn0b
m3tikn0b

Reputation: 1318

Too late for Serge but for all the googlers out there...

Serge is referring to the Keyboard class in TestStack.White. The Send function can only be used to send strings literally to another window. You cannot specify control keys here. They are just needed internally sometimes. E.g. Send("{") on my keyboard layout would internally translate to "AltKeyIsNeeded" and "7".

You can send keyboard shortcuts CTRL+A, CTRL+End like this:

myWindow.Keyboard.HoldKey(KeyboardInput.SpecialKeys.CONTROL);
myWindow.Keyboard.Enter("A");
myWindow.Keyboard.LeaveKey(KeyboardInput.SpecialKeys.CONTROL);

myWindow.Keyboard.HoldKey(KeyboardInput.SpecialKeys.CONTROL);
myWindow.Keyboard.HoldKey(KeyboardInput.SpecialKeys.END);
myWindow.Keyboard.LeaveKey(KeyboardInput.SpecialKeys.END);
myWindow.Keyboard.LeaveKey(KeyboardInput.SpecialKeys.CONTROL);

Upvotes: 1

Laoujin
Laoujin

Reputation: 10229

Perhaps a 'normal' string is not the best format to do this?

If the string contains 'characters that need to be sent', perhaps a list of KeyEventArgs or some custom built data class could be sent to your Send method. Then you can loop over the list and execute one by one. If you need combinations (like CTRL+K + CTRL+K (=toggle bookmark in Visual Studio)) you might need a composite.

Another option is to create your own DSL.

Upvotes: 1

TheMightyX2Y
TheMightyX2Y

Reputation: 1503

I've done something similar in an application, with the Windows key' shortcuts. I'm used a WH_KEYBOARD_LL hook and when I've got a specific shortcut I call the method. Maybe this could help you.

Upvotes: 0

Related Questions