Question detect F2 keypress on any control

mrhendrik

Member
Joined
Sep 26, 2010
Messages
6
Programming Experience
Beginner
Hi,

I want to detect F2 keypress as a shortcut to show something.. this is like F1 to show help in vb.net..

thanks..
 
Solved

Nevermind.. i got it..

in form load
VB.NET:
For Each COntrol as COntrol In Me.Controls
AddHandler Control.KeyDown, AddressOf ControlKeyDown
Next

Make ControlKeyDown
VB.NET:
Private Sub ControlKeyDown (ByVal sender as Object, ByVal e as System.Windows.Forms.KeyEventArgs)

If e.KeyValue = 113 Then
<Code Here>
End If
 
In the form properties, set KeyPreview to True, this allows the form to handle any Key Event made by any control on the form, including the form itself.
Then under the form's KeyDown event put
VB.NET:
If e.KeyData = Keys.F2 Then
MsgBox("F2 was pressed")
End If
 
Last edited:
In addition, since this is for help topics you probably want to know which control is active and you can get this from ActiveControl property, also for controls that is active in child containers.
 

Latest posts

Back
Top