?
Solved

Keydown Event in vb.net

Posted on 2013-05-23
9
Medium Priority
?
600 Views
Last Modified: 2013-07-02
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
Comment
Question by:cmdolcet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 11

Accepted Solution

by:
madgino earned 1600 total points
ID: 39190987
0
 

Author Comment

by:cmdolcet
ID: 39191221
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 39191227
Are you trying to trap F2 from anywhere in the app?
0
Stressed Out?

Watch some penguins on the livecam!

 
LVL 11

Expert Comment

by:madgino
ID: 39191888
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 39192083
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
 

Author Comment

by:cmdolcet
ID: 39194510
The IMessagefilter keeps saying it it not defined?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 39196660
This is a standard WinForms app no?  Show me your implementation please...
0
 

Author Comment

by:cmdolcet
ID: 39201521
Imports LMIObjectLibrary
Imports PartfileLibrary
Imports System.Threading
Imports SequenceEditorLibrary

Inherits System.Windows.Forms.Form
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 39201537
No...I meant all the code.  =)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question