How to detect key presses in VB.NET?
How do I make VB constantly check for key presses?
Keys on keyboard show up when debugging step by step, but this :( Here is my push code:
Private Sub Form_Main_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
'Keypress Q, A, or Z if the picture is seen for the first time
'Otherwise one accuracy point is deducted
If e.KeyChar = Chr(97) Then 'key a
If PictureBox.Visible = True Then
If MainArray(X) = 1 Then
Timer_End = TimeOfDay.Millisecond
PictureBox.Image.Dispose()
PictureBox.Image = Nothing
PictureBox.Visible = False
Accuracy = Accuracy + 1
CalcTime()
Else
Timer_End = TimeOfDay.Millisecond
PictureBox.Image.Dispose()
PictureBox.Image = Nothing
PictureBox.Visible = False
Accuracy = Accuracy - 1
CalcTime()
End If
End If
End If
'Keypress for second occurance
If e.KeyChar = Chr(108) Then 'key l
If PictureBox.Visible = True Then
If MainArray(X) = 2 Then
Timer_End = TimeOfDay.Millisecond
PictureBox.Image.Dispose()
PictureBox.Image = Nothing
PictureBox.Visible = False
Accuracy = Accuracy + 1
CalcTime()
Else
Timer_End = TimeOfDay.Millisecond
PictureBox.Image.Dispose()
PictureBox.Image = Nothing
PictureBox.Visible = False
Accuracy = Accuracy - 1
CalcTime()
End If
End If
End If
End Sub
I tried adding this to the code, but it didn't help:
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Do While PictureBox.Visible = True
Application.DoEvents()
Loop
End Sub
0
Tim
a source
to share
2 answers
Form.KeyPress should be raised every time a key is pressed. Your event handler should be called regardless of whether you are debugging or not. It might be caused, but it just doesn't do what you expect? Try putting some debug output to limit the processing of your event handler.
0
a source to share
This link can help:
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.aspx#
There should also be examples of detecting specific keystrokes using the KeyEventArgs class.
0
a source to share