Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyDown event not firing with .NET WinForms?

I already have KeyPreview set to true in the form properties

I'm working on a small program, and I'm having a problem where it seems that some of the controls on it inside groupboxes are not triggering the KeyDown event on my form when I press and release any arrow key, just the KeyUp event. Is there something wrong with my code that might be causing this?

Specifically, I've enabled KeyPreview on the form, and set breakpoints on e.SuppressKeyPress = True in both subroutines, and only the one for frmMain_KeyUp hits the breakpoint.

I added in the two GroupBox events hoping that might mitigate the issue, but no such luck. However, I have a custom control on the form that is specifically coded to ignore these keypresses, and the code works as expected on it.

  Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown, GroupBox1.KeyDown, GroupBox2.KeyDown
      e.SuppressKeyPress = True
      Select Case e.KeyCode
          Case Keys.Left
              ScrollDir = ScrollDir Or 1
          Case Keys.Right
              ScrollDir = ScrollDir Or 2
          Case Keys.Down
              ScrollDir = ScrollDir Or 4
          Case Keys.Up
              ScrollDir = ScrollDir Or 8
          Case Else
              e.SuppressKeyPress = False
      End Select
      tScroll.Enabled = True
      tScroll_Tick(Nothing, Nothing)
  End Sub

  Private Sub frmMain_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp, GroupBox1.KeyUp, GroupBox2.KeyUp

      e.SuppressKeyPress = True
      Select Case e.KeyCode
          Case Keys.Left
              ScrollDir = ScrollDir And (Not 1)
          Case Keys.Right
              ScrollDir = ScrollDir And (Not 2)
          Case Keys.Down
              ScrollDir = ScrollDir And (Not 4)
          Case Keys.Up
              ScrollDir = ScrollDir And (Not 8)
          Case Else
              e.SuppressKeyPress = False
      End Select
      If ScrollDir = 0 Then tScroll.Enabled = False
  End Sub

The code in the user control that "ignores" keypresses is as such:

    Private Sub TileDropDown_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyValue = 40 OrElse e.KeyValue = 38 OrElse e.KeyValue = 39 OrElse e.KeyValue = 37 Then

            e.SuppressKeyPress = True
        End If
    End Sub
like image 782
Sukasa Avatar asked Oct 22 '09 17:10

Sukasa


People also ask

How do I trigger Keydown event?

keydown: This event is triggered when a key is pressed down. keypress: This event is triggered when a key is pressed. This event fails to recognise keys such as tab, shift, ctrl, backspace etc. keyup: This event is triggered when a key is released.

How use Keydown event in VB NET?

How use KeyDown event in VB NET? Select your VB.Net source code view in Visual Studio and select TextBox1 from the top-left Combobox and select KeyDown from top-right combobox , then you will get keydown event sub-routine in your source code editor.

Should I use Keydown or Keyup?

Occur in sequence when a user presses and releases a key. KeyDown occurs when the user presses a key. KeyUp occurs when the user releases a key.

Is Keydown an event?

The keydown event is fired when a key is pressed. Unlike the deprecated keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered.


1 Answers

Some controls intercept the arrow keys in the keydown event, but not in the keyup event. One solution is to derive the control class and override ProcessCmdKey:

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keydata As Keys) As Boolean

If keydata = Keys.Right Or keydata = Keys.Left Or keydata = Keys.Up Or keydata = Keys.Down Then
  OnKeyDown(New KeyEventArgs(keydata))
  ProcessCmdKey = True
Else
  ProcessCmdKey = MyBase.ProcessCmdKey(msg, keydata)
  End If
End Function
like image 164
xpda Avatar answered Oct 12 '22 18:10

xpda