Click here to Skip to main content
14,452,230 members
Rate this:
Please Sign up or sign in to vote.
In frmMain I have:

AddHandler Me.PreviewKeyDown, AddressOf Me.TrapKeyPreview
AddHandler Me.KeyDown, AddressOf Me.TrapKeyDown
Me.KeyPreview = True


There are then two additional subs:

Private Sub TrapKeyPreview(ByVal sender As Object, _
                       ByVal e As PreviewKeyDownEventArgs)
    Select Case (e.KeyCode)
        Case Keys.Down, Keys.Up
            e.IsInputKey = True
    End Select
End Sub

Private Sub TrapKeydown(ByVal sender As Object, _
                        ByVal e As KeyEventArgs)
    Select Case (e.KeyCode)
        Case Keys.Up
            'Process up arrow key
        Case Keys.Down
            'Process down arrow key
    End Select
    e.Handled = True
End Sub


When I put this into a simple app, it performs as intended. You can have other textboxes, etc. that you can TAB between but the up arrow and down arrow are handled by TrapKeyDown.

When I put this code into a larger app with many controls it behaves inconsistently with regard to arrow up. Arrow down key strokes appear to be buffered and then applied when I hit the next Up Arrow.

Any ideas would be appreciated.

Carl
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

I ran into a problem where I needed to capture Page Up, Page Down, Home and End (to use for quick keys for navigation); however, the standard events were not capturing them. Not sure if this helps you in your situation; but, maybe worth a look into.

Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
           Select Case keyData
               Case Keys.PageDown, Keys.PageUp, Keys.End, Keys.Home
                   RaiseEvent CommandKeyPress(keyData)
           End Select
           MyBase.ProcessCmdKey(msg, keyData)
       End Function
   
Comments
w4uoa 16-Nov-11 13:25pm
   
Thanks. I'll try it later today and report back!
Carl
w4uoa 16-Nov-11 14:51pm
   
I'm sure I'm overlooking the obvious but....

I do not get a CommandKeyPress as a valid event. Do I need to import a module?
Pete BSC 16-Nov-11 15:32pm
   
It's not an event, it's a function that you can override:

Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, keyData As System.Windows.Forms.Keys) As Boolean

Member of System.Windows.Forms.Form
Summary:
Processes a command key.

Parameters:
msg: A System.Windows.Forms.Message, passed by reference, that represents the Win32 message to process.
keyData: One of the System.Windows.Forms.Keys values that represents the key to process.

Return Values:

true if the keystroke was processed and consumed by the control; otherwise, false to allow further processing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100