Click here to Skip to main content
15,900,713 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I am trying to fire ESC,F1 and F2 keys from code, Only "ESC" is getting fired not F1 and F2. Is there any mistake in the following code?

VB
If e.KeyChar = Microsoft.VisualBasic.ChrW(27) Then
       Panel7.Visible = True
       Panel7.Location = New Point(1, 1)
       txtsub.Focus()
End If
If e.KeyChar = Microsoft.VisualBasic.ChrW(112) Then
       Panel8.Visible = True
       Panel8.Location = New Point(1, 1)
       txtsub.Focus()
End If
If e.KeyChar = Microsoft.VisualBasic.ChrW(113) Then
       Panel9.Visible = True
       Panel9.Location = New Point(1, 1)
       txtsub.Focus()
End If
Posted
Updated 13-Sep-10 5:44am
v3
Comments
Dalek Dave 13-Sep-10 11:42am    
Edited for Code Block.
HimanshuJoshi 13-Sep-10 11:44am    
Edited for grammar and spelling
Kschuler 13-Sep-10 16:49pm    
what event is this code in?
William Winner 13-Sep-10 17:54pm    
@Kschuler...e.KeyChar is only available in the KeyPress event...

Your problem is that you've trapped this through the KeyPress event.

You need to look up the documentation on the code when you're having an issue. From MSDN:

"The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events."

So, you need to use the KeyUp event instead of the KeyPress event. Also, the KeyUp event is more readable and provides more options. For example, instead of writing

VB
If e.KeyChar = Chr(112) Then


you can write

VB
If e.KeyCode = Keys.F1 Then


It's a lot easier for someone debugging your code to read the second.

Also, as a grammatical note, when you say that you're trying to "fire" a key from code, it means that you're trying to tell the computer that that key was pressed. You're tying to "trap" the key.
 
Share this answer
 
Comments
Jatinder Gupta 13-Sep-10 20:57pm    
Thanks the Problem has been solved
Jatinder Gupta 13-Sep-10 21:06pm    
Nextly how to add the combination of key is that correct as define under.

If e.KeyCode = Keys.F1 And e.KeyCode = Keys.ControlKey Then
MsgBox("F1+Ctr")
End If
Jatinder Gupta 13-Sep-10 21:14pm    
Thanks I have got the code but now again a slit problem occers when I press F1+Ctr it shows the message first "F1" then "F1+Ctr". Could we not use "F1" and "F1+Ctr" in a same event

If e.KeyCode = Keys.F1 Then
MsgBox("F1 Pressed")
End If
If e.KeyCode = Keys.F2 And e.Control = True Then
MsgBox("F2+Ctr")
End If
If e.KeyCode = Keys.A And e.Alt = True Then
MsgBox("Alt+A")
End If
Dalek Dave 14-Sep-10 11:19am    
Good Call.
As far as your new question (from the comment left on my last answer), the user should always press ctrl, shift, or alt first. That's the way it works everywhere.

If I am in Word, highlight some text, and hold "c" and then hit ctrl to initiate a copy event, it won't work. What will happen is that the highlighted text will first become the letter "c", then "c" will be repeated until control is pressed. At that point, no text will be highlighted, so nothing will be copied.

It should always be assumed that ctrl, alt, and shift should be pressed first.

If, however, you are pressing Ctrl first and then F1 and you are getting duplicate messages, the problem is in your code. Let's say, you have written
VB
If e.KeyCode = Keys.F1 Then
  MessageBox.Show("F1")
End If
If e.KeyCode = Keys.F1 and e.Control Then
  MessageBox.Show("F1 + Ctrl")
End If

you will get two messages. Do you see why?

Also, just a few little tips since you appear to be new to .Net.

First, MsgBox shouldn't be used anymore. That's an old VB6 structure. You should be using MessageBox.Show("some text").

Secondly, you don't need to test boolean values against true or false. That is just added steps the application will have to take.
VB
If e.Control Then
'...
'
'is the same as
If e.Control = True

The difference is that e.Control = True has to first test that condition and then check whether that test returned true or false.

Finally, if you are testing a lot of possibilities for e.KeyCode, you would be better off using the Select Case structure. It would look like:
VB
Dim output as String = ""

Select Case e.KeyCode
  Case Keys.F1
    output = "F1"
  Case Keys.F2
    output = "F2"
End Select

If e.Control Then
  output = output & "+Ctrl"
End If

MessageBox.Show(output)
 
Share this answer
 
v2
Comments
Dalek Dave 14-Sep-10 11:19am    
Good answer.
Jatinder Gupta 14-Sep-10 20:56pm    
Thanks sir, good solution.....

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