Solved

how to trigger function by keypressed

Posted on 2006-10-31
8
188 Views
Last Modified: 2010-04-23
I gonna pop up the message box everytime i pressed "F12" in my keyboard. ( or i pressed Ctrl + Y")

Private Function testing ()
    Msgbox ("Hello")
End Function

How to do it?
0
Comment
Question by:kengkit
8 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17847574
Do you want this only when YOUR app is in focus?  Or when ANY app is in focus?
0
 

Author Comment

by:kengkit
ID: 17847585
in fact.. i juz wants tis when my app is in focus

but it's good if u can tell me both.
0
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 17847679
I'm not sure how to do it if your app doesn't have focus (I haven't brushed up on my keyboard hooking), but here's how you can do it if your app has focus:

    Private Function testing()
        Msgbox("Hello")
    End Function

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Control + Keys.Y Or keyData = Keys.F12 Then
            testing()
            Return True
        End If
    End Function
0
 
LVL 6

Expert Comment

by:manch
ID: 17847701
'U Can use the IMessageFilter Interface for it
' Creates a message filter.
Public Class TestMessageFilter
   Implements IMessageFilter

   Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) _
   As Boolean Implements IMessageFilter.PreFilterMessage
      ' Blocks all the messages relating to the left mouse button.
      If ((m.Msg >= 513) And (m.Msg <= 515)) Then
         Console.WriteLine("Processing the messages : " & m.Msg)
         Return True
      End If
      Return False
   End Function
End Class

Try It
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 20 total points
ID: 17847705
Here is an example of the hook approach that allows you trap keypresses while your app is not in focus:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21584180.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

867 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

19 Experts available now in Live!

Get 1:1 Help Now