Link to home
Start Free TrialLog in
Avatar of ahmed021399
ahmed021399

asked on

trapping function keys

Can anyone tell how i can do something when F12 key is pressed. It seems it cannot be trapped in KeyPress event like the other keys using the ASCII code. Please help
ASKER CERTIFIED SOLUTION
Avatar of HobbitHouse
HobbitHouse

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Éric Moreau
The KeyPress event is only good for trapping ASCII characters (letters, numbers, punctuation, delete, enter, back, ...)
When possible, use the intrinsic constants (keycode in this case) for clearer code.  

vbKeyF12 = &h7B = 123


If yopu need to trap F12 key in entire form, first set Heypreview property of form to true, after that use some like this:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
if KeyCode=vbkeyf12 then
  Debug.Print KeyCode
end if
End Sub
This might be overkill, but it will capture that key you want. It's actually meant to capture keys outside (as well as inside) your application.

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=24607&lngWId=1

Since you want to use F12, I guess it wouldn't really hurt other applications.
Where you read:
"If yopu need to trap F12 key in entire form, first set Heypreview "
it must be:
"If you need to trap F12 key in entire form, first set KeyPreview "
Sorry.
Isn't "Heypreview" a lot like Caller-ID? :-)
Avatar of ahmed021399
ahmed021399

ASKER

Richie your code is just for the active application, right?