Solved

Keydown Event in vb.net

Posted on 2013-05-23
9
549 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now