NEWBIE ALERT: Event Handling
I have alot of experience in working in VBA for excel, but not so much experience in creating my own structures so it's all still a bit foggy.
I'm trying to capture the event for when the F1 key is pressed, not just when the form is active, but anytime the program is running. The code below expressing the handling is automatically generated for me in VB Express... But I'd like to change the situation to encompass anytime the program is open. I guess I have to reference events that happen in windows in general, but i don't know how to do it. I think i have to create the even in a class module??? If someone could kind of shove me in the right direction, I'd be very much appreciative.
Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = "112" Then
Call SaveWholeForm()
End If
End Sub
Re: NEWBIE ALERT: Event Handling
search the forum for registerhotkey/unregisterhotkey api's to use system wide hotkeys. i wouldn't recommend using the F1 key though. it's used as a help key by windows
Re: NEWBIE ALERT: Event Handling
1. You need to set the form.KeyPreview property to True. In form designer, you click on the form to select it and look in the properties window for the KeyPreview property and change it to True (default value is False)
2. Paste in this code
Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F1 Then
SaveWholeForm()
End If
End Sub
Edit: Never mind... I didn't know that you need a system wide keyboard hook
Re: NEWBIE ALERT: Event Handling
It is a very bad idea to use F1 as a hotkey. That is the standard Windows shortcut for Help, so users might want to open Help in some other app and they get your app doing something else. Use a hotkey that is unlikely to be in use elsewhere and always use at least two modifier keys.