If you wish to use the "F1" key you are going to need to hook the keyboard using some API calls. You will then need to determine if you wish to continue to process the F1 Help key as your own or send it to windows. Is there a reason you used the keyascii code for the escape key? If you choose to battle wits with the witless be prepared to lose.
F1 can not be detected in the KeyPress event. Use Form_KeyDown event.
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF1 Then
............
End If
End Sub
Make KeyPreview=True on the Form at Design-Time or
Me.KeyPreview = True
in the Form_Load event.
You can use Me.ActiveControl to know what control if any had the focus. It is a good idea to check for Nothing in the ActiveControl.
If Not(Me.ActiveControl Is Nothing) Then
.....F1 for particular control
End if
If you do that, can you intercept and prevent the help file (if present) from running. If so I have been doing things the hard way. If you choose to battle wits with the witless be prepared to lose.
What I gave intercepts keysterokes at the Form level. The "but for" in the original question is puzzling. Does it mean except or does it mean "the same as something else but intercepting F1"? I use it for activating my help file at the form level.
The key strokes will still go to the control unless set the Keycode = 0. Preventing "help File".? I dont know.
The docs say this about the control's "HelpContextID Property"
"If you've created a Microsoft Windows operating environment Help file for your application and set the application's HelpFile property, when a user presses the F1 key, Visual Basic automatically calls Help and searches for the topic identified by the current context number."
I do not know where the "intercept occurs", before or after KeyPreview.
Here are the details. I put a HelpContextId in a control. It does not exist in the Help File. I attached my Help file through Project Properties. I set KeyPreview=True on the form. I used the following code.
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF1 Then
''''KeyCode = 0 ' Activate to kill Help
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF1 Then
KeyCode = 0
End If
End Sub
When I press F1, the KeyDown always occurs but the KeyUp never does. Because I do not have context Help, I get an error message but the Help file is displayed at the first page. If, however I set Keycode=0 in the KeyDown event, no error message occurs because the F1 was cancelled and Help was not invoked.
I believe the above ndicates that there is no need for API calls or subclassing hooks to intercept any keys in KeyDown. It will not prevent access to the Help files unless you wanbt it to.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.