Capture Function Key press event

M

MS Newsgroups

Hi,

I would like to capture when a user presses F5 in my Windows form
application, but the keypress event does not seem to fire when a function
key is pressed, Is there another way of doing this ?

This code works fine for all keys except function keys

Protected Overrides Sub OnKeyPress(ByVal e As
System.Windows.Forms.KeyPressEventArgs)
Dim myKey As Char
myKey = e.KeyChar
MsgBox(myKey.ToString)
End Sub


Regards

Niclas
 
G

Gary

Niclas,

Set the form's "KeyPreview" prop. to False and try the following code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

If e.KeyCode = Keys.F5 Then

MessageBox.Show("F5 pressed")

End If

End Sub

Let me know if this works.

- Gary -
 
A

Armin Zingler

MS Newsgroups said:
I would like to capture when a user presses F5 in my Windows form
application, but the keypress event does not seem to fire when a
function key is pressed, Is there another way of doing this ?

Use the KeyDown event. Keypress only fires for keys that create chars.
 
P

Prateek

Hi Niclas,

KeyPress event will not fire for function keys as this event only traps
printable keys. You will need to use the KeyUp or KeyDown events in this
case.

-Prateek

Hi,

I would like to capture when a user presses F5 in my Windows form
application, but the keypress event does not seem to fire when a function
key is pressed, Is there another way of doing this ?

This code works fine for all keys except function keys

Protected Overrides Sub OnKeyPress(ByVal e As
System.Windows.Forms.KeyPressEventArgs)
Dim myKey As Char
myKey = e.KeyChar
MsgBox(myKey.ToString)
End Sub


Regards

Niclas
 
H

Herfried K. Wagner [MVP]

Hello,

MS Newsgroups said:
I would like to capture when a user presses F5 in my Windows
form application, but the keypress event does not seem to fire
when a function key is pressed, Is there another way of doing this ?

Set the form's 'KeyPreview' property to 'True' and add this code:

\\\
Private Sub Form1_KeyDown( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs _
) Handles MyBase.KeyDown
If e.KeyCode = Keys.F5 Then
MsgBox("F5 pressed!")
End If
End Sub
///

HTH,
Herfried K. Wagner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top