locked
Simulating keyboard input RRS feed

  • Question

  • I use the following code listed below to simulate a keyboard press of the page down key for a listbox, when my application is in focus (the active window) it works fine however my application is a background application and when the code is called from a background application it falls over with the following exception. Obviously from that I can see that the problem is the input source where it considers it to be null if the code is called when my application isn't active. So the question is how can I get this code to work when the application is in the background?

    Many thanks

    InnerException: System.ArgumentNullException
    Message="Value cannot be null.\r\nParameter name: inputSource"
    Source="PresentationCore"
    ParamName="inputSource"
    StackTrace:
    at System.Windows.Input.KeyEventArgs..ctor(KeyboardDevice keyboard, PresentationSource inputSource, Int32 timestamp, Key key)

    internal void ProcessPageDownKey()
            {            
                KeyEventArgs keyEventArgs = new 
                    KeyEventArgs(InputManager.Current.PrimaryKeyboardDevice,InputManager.Current.PrimaryKeyboardDevice.ActiveSource, System.Environment.ProcessorCount, Key.PageDown);
                keyEventArgs.RoutedEvent = UIElement.KeyDownEvent;
                globalListBox.RaiseEvent(keyEventArgs);
                keyEventArgs.RoutedEvent = UIElement.KeyUpEvent;
                globalListBox.RaiseEvent(keyEventArgs);
            }
    
    ...
    ...
    
    this.ProcessPageDownKey();
    Tuesday, January 26, 2010 6:20 PM

Answers