Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

F1 key press 2

Status
Not open for further replies.

dominiopt

Technical User
Sep 10, 2001
75
PT
How can i get similar feature but for "F1" key press ?

Private Sub Form_KeyPress(KeyAscii As Integer)

If KeyAscii = 27 Then
MsgBox Me.Name
End If

End Sub

Thanks
Fernando
 
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.
[machinegun][ducky]
 
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
Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
John,

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.
[machinegun][ducky]
 
Foada,

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. Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Foada,

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.

Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Thanks John,

I have been doing things the hard way. If you choose to battle wits with the witless be prepared to lose.
[machinegun][ducky]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top