• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 618
  • Last Modified:

Keydown Event in vb.net

OK I am having some issues with my Key Down Event in my program. I have the event on my form  using the following syntax:

Private Sub Truntim_Keydown(byVal sender as object, byval e as keyEventArgs) Handles mybase.KEydown
Dim mainform as frmMain = me.midiparent
if e.keycode=system.windows.Forms.Keys.F2 then
Me.sampledata()
end if
end sub


The issue I am seeing is that the keydown event will sometimes get called and sometimes will not get called. I think its because maybe the focus is not on the main form however I do not know this to be true.

Can anyone help me out?
0
cmdolcet
Asked:
cmdolcet
  • 4
  • 3
  • 2
1 Solution
 
cmdolcetAuthor Commented:
Thanks Madgino,
If I wanted to use a timer for the keyboard event how could I use the code I supplied in the timer?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Are you trying to trap F2 from anywhere in the app?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
madginoCommented:
Well, you leave your code unchanged, in a timer event you use other code that makes your window to always have focus. (like here)
This means that Truntim_Keydown will be triggered on any F2.

Both solutions (keyboard hook & timer for focus) seems to be not very complicated to implement, maybe you have time to try them both and see which one have better results.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You didn't answer my question...if you only need to detect F2 for your application, then use IMessageFilter():
Public Class Form1

    Public Shared Filter As MyMessageFilter

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Filter = New MyMessageFilter
        Application.AddMessageFilter(Filter)

        AddHandler Form1.Filter.F2, AddressOf F2_Pressed ' <-- you can wire up the "F2" event in what ever Form you like
    End Sub

    Private Shared Sub F2_Pressed()
        Debug.Print("Form1 has detected F2")
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim frm2 As New Form2
        frm2.Show()
    End Sub

End Class

Public Class Form2

    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        AddHandler Form1.Filter.F2, AddressOf F2_Pressed
    End Sub

    Private Shared Sub F2_Pressed()
        Debug.Print("Form2 has detected F2")
    End Sub

End Class

Public Class MyMessageFilter
    Implements IMessageFilter

    Public Event F2()

    Private Const WM_KEYDOWN As Integer = &H100

    Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements IMessageFilter.PreFilterMessage
        Select Case m.Msg
            Case WM_KEYDOWN
                Select Case m.WParam.ToInt32
                    Case Keys.F2
                        RaiseEvent F2()
                        Return True

                End Select
        End Select

        Return False
    End Function

End Class

Open in new window


A low level keyboard hook is overkill if it's just for your app, and a timer is a messy way to do it.
0
 
cmdolcetAuthor Commented:
The IMessagefilter keeps saying it it not defined?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
This is a standard WinForms app no?  Show me your implementation please...
0
 
cmdolcetAuthor Commented:
Imports LMIObjectLibrary
Imports PartfileLibrary
Imports System.Threading
Imports SequenceEditorLibrary

Inherits System.Windows.Forms.Form
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
No...I meant all the code.  =)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now