1

Is there an equivalent to SendInput for WPF? I've looked into AutomationPeer classes but was not successfull.

I simply want to send a Keydown (the Enter Key). Simply raising the event (RaiseEvent) does not work in my scenario.

Here is what I have, which is working. I'd prefer to have a managed code alternative.

    private void comboSelectionChanged(object sender, SelectionChangedEventArgs args)
    {
        ((ComboBox)sender).Focus();
        // send keydown
        INPUT input = new INPUT();
        input.type = INPUT_KEYBOARD;
        input.union.keyboardInput.wVk = 0x0D;
        input.union.keyboardInput.time = 0;
        SendInput(1, ref input, Marshal.SizeOf(input));
    }

    [DllImport("user32.dll", SetLastError = true)]
    private static extern int SendInput(int nInputs, ref INPUT mi, int cbSize);

    [StructLayout(LayoutKind.Sequential)]
    private struct INPUT
    {
        public int type;
        public INPUTUNION union;
    };

    [StructLayout(LayoutKind.Explicit)]
    private struct INPUTUNION
    {
        [FieldOffset(0)]
        public MOUSEINPUT mouseInput;
        [FieldOffset(0)]
        public KEYBDINPUT keyboardInput;
    };

    [StructLayout(LayoutKind.Sequential)]
    private struct MOUSEINPUT
    {
        public int dx;
        public int dy;
        public int mouseData;
        public int dwFlags;
        public int time;
        public IntPtr dwExtraInfo;
    };

    [StructLayout(LayoutKind.Sequential)]
    private struct KEYBDINPUT
    {
        public short wVk;
        public short wScan;
        public int dwFlags;
        public int time;
        public IntPtr dwExtraInfo;
    };

    private const int INPUT_MOUSE = 0;
    private const int INPUT_KEYBOARD = 1;
Jf Beaulac
  • 5,206
  • 1
  • 25
  • 46
  • @HighCore Im using a ComboBox that is editable, when you select a value from the dropdown the text is selected, I wanna clear it. – Jf Beaulac Nov 23 '12 at 20:44
  • OMG.. and you want to do that by using SendKeys?? thats a terribly bad approach.. why not just clear the combobox text property? – Federico Berasategui Nov 23 '12 at 21:31
  • 1
    If you are working with WPF you'd better get familiar with the recommended techniques, such as [MVVM](http://en.wikipedia.org/wiki/Model_View_ViewModel) and [DataBinding](http://wpftutorial.net/DataBindingOverview.html). Sending keyboard signals to manipulate UI is a terribly bad approach. – Federico Berasategui Nov 23 '12 at 21:33
  • I dont want to clear the text, just the textselection, cant do this via MVVM/Databinding. The combobox doesnt even expose the text selection – Jf Beaulac Nov 23 '12 at 21:43

1 Answers1

7

You can emulate a keystroke like this:

public void SendKey(UIElement sourceElement, Key keyToSend)
    {

        KeyEventArgs args = new KeyEventArgs(InputManager.Current.PrimaryKeyboardDevice, PresentationSource.FromVisual(sourceElement), 0, keyToSend);

        args.RoutedEvent = Keyboard.KeyDownEvent;
        InputManager.Current.ProcessInput(args);

    }

You could then call it like this:

SendKey(myComboBox, Key.Enter);

I suppose you can put this in a static class somewhere, or even make an extension method out of it. However, I would argue that in most cases there is a more elegant way to accomplish this.

I hope this helps.

XamlZealot
  • 693
  • 5
  • 12
  • thanks, I finally ended up completely changing my approach (wrote yet another custom control...) before you posted, but I tried this and it work too. – Jf Beaulac Nov 23 '12 at 21:46
  • How do I send a combined keys like Ctrl+F? – newman Jan 22 '13 at 17:00
  • 2
    Unfortunately this doesn't cause the TextBox to change its Text-property. Is there a possibility to emulate a KeyDownEvent, that causes the TextBox to update its text? – Dennis Kassel Feb 12 '16 at 15:37