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
Solved

Keydown Event in vb.net

Posted on 2013-05-23
9
564 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
  • 4
  • 3
  • 2
9 Comments
 
LVL 11

Accepted Solution

by:
madgino earned 400 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 85

Expert Comment

by:Mike Tomlinson
ID: 39191227
Are you trying to trap F2 from anywhere in the app?
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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 85

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 85

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 85

Expert Comment

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

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 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