Created On:  24 July 2012

Problem:

Customer would like to use the Enter key as a Tab key when entering data in a Windows Presentation Foundation application.  How can I trap the Enter key and make it behave as if a Tab were pressed instead?

Resolution:

You can capture the KeyDown event on the WPF Window class or on a specific control like a TextBox and check if the Enter key was pressed.
If it was then you can fire a new KeyDown event for the Tab key every time the Enter key is pressed.

Example program swapkeyWPF is attached.

Code looks like:

*----------------------------------------------------------------------------------------------------*
*                                           SWAPKEYWPF                                               *
*                                                                                                    *
* This example program demonstrates how to capture a keystroke in a WPF application and then to swap *
* that key function with a new function by generating a different keystroke.                         *
*                                                                                                    *
* In this program pressing the Enter key will cause a Tab key to be generated so that Enter can be   *
* used to tab between fields.                                                                        *
*                                                                                                    *
* Pressing the letter "A" while in one of the textbox controls will cause a "Z" to be output instead *
*----------------------------------------------------------------------------------------------------*

$set ilusing"System.Windows.Input"
 class-id swapkeyWPF.Window1 is partial
    inherits type System.Windows.Window.
 working-storage section.
 method-id NEW.
 procedure division.
     invoke self::InitializeComponent()
     invoke textBox1::Focus
     goback.
 end method.
 method-id Window_KeyDown final private.
 01 e1 type KeyEventArgs.
 procedure division using by value sender as object e as type System.Windows.Input.KeyEventArgs.
 *>  if generating a keystroke such as Enter or Tab you would use the code below which replaces Enter key with Tab key
     if e::Key = type Key::Enter
        set e::Handled to true
        set e1 to new KeyEventArgs(type Keyboard::PrimaryDevice, type Keyboard::PrimaryDevice::ActiveSource,
            0, type Key::Tab)
        set e1::RoutedEvent to type Keyboard::KeyDownEvent
        invoke type InputManager::Current::ProcessInput(e1)
     end-if
 end method.
 method-id button1_Click final private.
 procedure division using by value sender as object e as type System.Windows.RoutedEventArgs.
     invoke self::Close
 end method.
 method-id textBox_KeyDown final private.
 procedure division using by value sender as object e as type System.Windows.Input.KeyEventArgs.
 *>  if generating a key value that should appear in a textbox control you would use the code below.
 *>  This will output the letter "Z" everytime "A" is pressed.
 *>  This traps the keydown event within the textbox control and not in the window like the enter and tab keys.
     if e::Key = type Key::A
        set e::Handled to true
        invoke type TextCompositionManager::StartComposition(new TextComposition(type InputManager::Current,
            sender as type TextBox, "Z"))
     end-if
 end method.
 end class.

Incident #2584112