Capture Function Key press event

  • Thread starter Thread starter MS Newsgroups
  • Start date Start date
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
 
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 -
 
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.
 
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
 
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
 
Back
Top