locked
Simulating a keypress via C# ... RRS feed

  • Question

  • Greetings All,

     

    I'm having difficulty simulating (using C#) the 'Tab' key being pressed - I wish to simulate the key being pressed via a Button's click-event ; I'm using ....

    System.Windows.Forms.SendKeys.Send("{TAB}");

    ...but this simply crashes my WPF (?).  I simply wish to  tab through on-screen elements (simply following the tabindex) and use the control.Focus(); to jump to different points within the tabindex.

     

    Any suggestions would be greatly appreciated.

    Many Thanks,

    -RJ-

    (Expression Blend 4, WPF, C#)

    Sunday, July 25, 2010 6:37 PM

Answers

  • Greetings All,

    Found this link ...

    http://social.msdn.microsoft.com/forums/en-US/wpf/thread/7af74fdc-92f4-49a8-85ac-bd57e5efbf36/

    (... Thanks DR. WPF)

     

    The following bit of code appears to be working as a simulated key press (it ain't pretty though)...

    System.Windows.Input.KeyEventArgs args = new System.Windows.Input.KeyEventArgs(Keyboard.PrimaryDevice,
    Keyboard.PrimaryDevice.ActiveSource, 0, Key.Tab);
    args.RoutedEvent = Keyboard.KeyDownEvent;
    InputManager.Current.ProcessInput(args);

    • Marked as answer by dt01rrj Monday, July 26, 2010 2:16 PM
    Monday, July 26, 2010 2:16 PM

All replies

  • Greetings All,

    Found this link ...

    http://social.msdn.microsoft.com/forums/en-US/wpf/thread/7af74fdc-92f4-49a8-85ac-bd57e5efbf36/

    (... Thanks DR. WPF)

     

    The following bit of code appears to be working as a simulated key press (it ain't pretty though)...

    System.Windows.Input.KeyEventArgs args = new System.Windows.Input.KeyEventArgs(Keyboard.PrimaryDevice,
    Keyboard.PrimaryDevice.ActiveSource, 0, Key.Tab);
    args.RoutedEvent = Keyboard.KeyDownEvent;
    InputManager.Current.ProcessInput(args);

    • Marked as answer by dt01rrj Monday, July 26, 2010 2:16 PM
    Monday, July 26, 2010 2:16 PM
  • Hi. The code works fine for me. However, if user press the "Shift" + "Tab", I still want to perform "Tab" function. Do you know how I can ignore any key modifiers?

    Thanks


    Jing Ling
    Wednesday, August 31, 2011 12:23 AM