Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, January 4, 2013 10:12 AM
I want to capture the key press event in application level not in form level in vb.net windows application.
I can able to capture the key press (F1 key) event in the form level.
**Code below: This is in form level event capturing
**
Private Sub customer_add_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.F1 Then
Magbox("F1 key pressed")
End IF
End Sub
But i want this type of event capture in application level. So that I can write this code in one place for all the forms. If the user hit the F1 key in any place of the application then the msgbox will show.
All replies (7)
Friday, January 4, 2013 8:07 PM âś…Answered
A hook will be system-wide, not application wide (although you may be able to filter it). See here for an example:
http://www.supportforums.net/showthread.php?tid=916
http://sim0n.wordpress.com/2009/03/28/vbnet-keyboard-hook-class/
Friday, January 4, 2013 10:42 AM
Selva,
What you ask is in the context of a windows forms application impossible (possible is, and I've seen the code in this forum) to get it for all controls on a form.
But not for more forms (be aware that a form is not an executable and that the form is a class to make a form, but is not always the same) There are MDI forms, ShowDialog forms (modal forms) and Show forms. The latter have almost no connection with the events which are thrown.
Success
Cor
Friday, January 4, 2013 10:53 AM
Selva,
First you create a class and inherit the form class and use Myclass form.
Class MyClass
Inherit Form
end Class
Selva : So that I can write this code in one place for all the forms
In this class you capture your key press event for all your form.
Art Of Living Is Art Of Giving
Friday, January 4, 2013 11:55 AM
I'm asking related to windows hooks....
Friday, January 4, 2013 3:07 PM
I'm asking related to windows hooks....
But you asked for Application. Do you want a hook or an appliction MessageFilter? Bing for Stephan Toub's hook or look in help for MessageFilter.
Friday, January 4, 2013 3:08 PM
This appears to work for two blank forms with no controls on them.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form2.Show()
AddHandler Form2.KeyDown, AddressOf Form1_KeyDown
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F1 Then
MessageBox.Show("F1 key pressed")
End If
End Sub
End Class
You've taught me everything I know but not everything you know.
Friday, January 4, 2013 3:15 PM
And this works for capturing F1 @ controls on either form.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form2.Show()
For Each MECOntrol As Control In Me.Controls
AddHandler MECOntrol.KeyDown, AddressOf ControlKeyDown
Next
For Each F2COntrol As Control In Form2.Controls
AddHandler F2COntrol.KeyDown, AddressOf ControlKeyDown
Next
End Sub
Private Sub ControlKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = Keys.F1 Then
MessageBox.Show("F1 key pressed")
End If
End Sub
End Class
You've taught me everything I know but not everything you know.