#622 – The Source of a Keyboard Event

The source of a keyboard event is the control that had keyboard focus when the key was pressed, referenced by the KeyEventArgs.Source property in a keyboard event handler.  You can consider the source as–the control where the keyboard event is taking place.

The source can be different from the sender of the event, which is the control to which the event handler was attached.

For example, if you have a TextBox contained in a StackPanel and you press a key while the TextBox has focus, the TextBox is the source of the event.  If you then attach a handler for one of the keyboard events to the StackPanel, the StackPanel is the sender of the event.

    <StackPanel KeyUp="StackPanel_KeyUp">
        <TextBox Text="I'm a TextBox" HorizontalAlignment="Center"
                 KeyUp="TextBox_KeyUp"/>
    </StackPanel>

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #622 – The Source of a Keyboard Event

  1. Pingback: Dew Drop – August 10, 2012 (#1,382) | Alvin Ashcraft's Morning Dew

Leave a comment