Solved

Keydown Event in vb.net

Posted on 2013-05-23
9
573 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 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 86

Expert Comment

by:Mike Tomlinson
ID: 39191227
Are you trying to trap F2 from anywhere in the app?
0
Independent Software Vendors: 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!

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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:…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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