Using Arrow Keys

I am trying to capture the use of the arrow keys in my code and it is not being recognized. It seems as if the keys are reserved for the form itself and when I press the arrow keys they just move from one button to the next similar to using the Tab key to maneuver through the various buttons. This code will trap the key down event for the other keys but not the arrow or tab keys.

 

method-id frm1CALENDAR_KeyDown final private.

procedure division using by value sender as object e as type System.Windows.Forms.KeyEventArgs.

* Immediately return if not a valid action key

IF e::KeyCode = type Keys::PageUp

OR e::KeyCode = type Keys::PageDown

OR e::KeyCode = type Keys::Enter

OR e::KeyCode = type Keys::Up

OR e::KeyCode = type Keys::Down

OR e::KeyCode = type Keys::Left

OR e::KeyCode = type Keys::Right

OR e::KeyCode = type Keys::Escape

continue

else

goback.

if e::KeyCode = type Keys::Up

invoke self::btnUp::PerformClick()

goback

end-if.

if e::KeyCode = type Keys::Down

invoke self::btnDown::PerformClick()

goback

end-if.

if e::KeyCode = type Keys::Right

invoke self::btnRight::PerformClick()

goback

end-if.

if e::KeyCode = type Keys::Left

invoke self::btnLeft::PerformClick()

goback

end-if.

if e::KeyCode = type Keys::PageUp

invoke self::btnPgUp::PerformClick()

goback

end-if.

if e::KeyCode = type Keys::PageDown

invoke self::btnPgDn::PerformClick()

goback

end-if.

if e::KeyCode = type Keys::Enter

invoke self::btnSelect::PerformClick()

goback

end-if.

if e::KeyCode = type Keys::Escape

invoke self::btnExit::PerformClick()

goback

end-if.

end method.

  • Some controls do not fire the keydown event for the arrow or tab keys as they use this for navigation within the control itself.

    You can change this behavior by using the PreviewKeyDown event to capture these keys and change them to input keys. They can then be caught by the KeyDown event on the form.

    Here is a snippet of the two handler methods to use. Use the same PreviewKeyDown handler for each of your controls where you want to be able to trap these keys. You should also set the form property KeyPreview to true.

    method-id Form1_KeyDown final private.
           procedure division using by value sender as object e as type System.Windows.Forms.KeyEventArgs.
               evaluate e::KeyCode
                  when type Keys::PageUp 
                    set textBox1::Text to  "pgup"
                  when type Keys::PageDown 
                    set textBox1::Text to  "pgdn"
                  when type Keys::Up
                    set textBox1::Text to "up"
                  when type Keys::Tab
                    set textBox1::Text to  "Tab"
                  when type Keys::Down
                    set textBox1::Text to  "down"
                  when type Keys::Left
                    set textBox1::Text to  "left"
                  when type Keys::Right
                    set textBox1::Text to  "right"
                end-evaluate.
               
           end method.
    
           method-id Form1_PreviewKeyDown final private.
           procedure division using by value sender as object e as type System.Windows.Forms.PreviewKeyDownEventArgs.
    
               evaluate e::KeyCode
                  when type Keys::PageUp 
                  when type Keys::PageDown 
                  when type Keys::Up
                  when type Keys::Tab
                  when type Keys::Down
                  when type Keys::Left
                  when type Keys::Right
                     set e::IsInputKey to true
                end-evaluate.
              
           end method.