Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all visitors
i have problem with function key.
i want to use event Form_key_press.
in the code i use like this:
VB
Private Sub frmmain_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

       'When press escape key will alert message box "Escape"
       If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Escape) Then
           MsgBox("Escape")
       End If

       'When press F9 key alert empty message
       If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.F9) Then
           MsgBox("F9")
       End If
   End Sub


Does anybody know how i can consider the function key in code?

Best Regards,
TONY
Posted
Updated 15-Aug-11 18:37pm
v2

Use KeyDown event
VB
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
  If e.KeyCode = Keys.F9 Then
    MsgBox("f9")
  End If
End Sub
 
Share this answer
 
v3
Comments
soeun tony 16-Aug-11 1:03am    
Thanks for your reply
Do you mean use Form_KeyDown Event?
I use keyDown Event but i don't see e.keyChar.
what property should i use?

Best Regards,
Prerak Patel 16-Aug-11 1:17am    
My mistake, I pasted the wrong code. Use KeyCode as in answer.
Toniyo Jackson 16-Aug-11 3:45am    
Correct 5!
soeun tony 16-Aug-11 21:09pm    
Thanks for your reply
Now it is ok now
i use the same that you said above
Thanks
Prerak Patel 16-Aug-11 23:41pm    
You are welcome. So, now you can mark it as answer.
<br />
<br />
Use KeyUp Event<br />
<pre lang="vb"><br />
Imports System.Windows.Forms.KeyPressEventArgs<br />
<br />
  Private Sub demo_keyup(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp<br />
    If e.KeyCode = Keys.F1 Then<br />
      MsgBox("f1:HELP")<br />
<br />
    End If<br />
  End Sub<br />
</pre><br />
<br />
 This is another format to using keyup functionality .<br />
   <br />
 
Share this answer
 
Comments
soeun tony 16-Aug-11 21:09pm    
Thanks for your reply
Now it is ok
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900