Question Accesing a Fn Keys

vicdeveloper

Member
Joined
Jun 26, 2016
Messages
11
Programming Experience
Beginner
Hello,

I want to know how can I make an option to use F1 or Fn keys when my form loads.

I guess that it need to be related with KeyDown or KeyPress Events, but, I dont know how to do it.

The things that I want to do with this is, call a form or function when Fn key is pressed.

See ya,
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
When you say Fn, do you just mean any standard function key, i.e. F1 - F12, or do you mean the hardware Fn key that many keyboards use to provide multiple functions from a single key?

If it's the latter then you should forget about it. That key is basically for the hardware only. It will behave differently on different keyboards and won't even be present on some. Using it for anything other than what the manufacturer intended is rarely, if ever, justified.

If it's the former then you should also forget. The standard functionality for the F1 in Windows is for Help. Using it for any other purpose is back practice. You should use some other key or key combination, e.g. F2 or Ctrl+Shift+Q. In that case you would indeed handle the appropriate KeyDown event. If you want to react to a key or key combination regardless of which control has focus then you should handle the KeyDown event of the form, but make sure you set the KeyPreview property to True.
 

vicdeveloper

Member
Joined
Jun 26, 2016
Messages
11
Programming Experience
Beginner
Hello dude,

I have and found the solution.

It need to be added this code in the KeyDown event and the KeyPreview is needed to set True.

If e.KeyCode = Keys.F3 Then
MsgBox("You pressed F3 Key. :)")
End If

See ya,
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
Hello dude,

I have and found the solution.

It need to be added this code in the KeyDown event and the KeyPreview is needed to set True.



See ya,

Just be aware that that code will react to F3, Ctrl+F3, Shift+F3, Alt+F3, Ctrl+Shift+F3, etc. That's because KeyCode only cares about the last key that was depressed and not any modifier keys that are already depressed. If you want to react to F3 alone then you should use KeyData rather than KeyCode. KeyData includes the last key to be depressed and current modifiers, so testing for F3 will only detect F3 alone.
 
Top Bottom