הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Thursday, October 30, 2014 4:42 AM
I have a space invaders game, and I use arrows to control the space ship to go left and right, however, I also have a 'Pause' button, which stops the timer that acts as a movement component. However, with the button in place, the arrow keys will no longer move the spaceship sideways, and instead cause the button to gain focus. Is there any way to disable that, so that I can still control my spaceship with the arrow keys, without changing them to something else, and to still have the button there?
Thanks in advance!
All replies (2)
Thursday, October 30, 2014 7:25 AM ✅Answered | 1 vote
This will detect arrow keys, or any keys I suppose, no matter what control has focus. If the app is running and is the active app on the desktop this function for the Form it is in will detect keystrokes for that Form.
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
If keyData = Keys.Left Then
Me.Text = "Left"
ElseIf keyData = Keys.Right Then
Me.Text = "Right"
End If
Return Nothing
End Function
La vida loca
Thursday, October 30, 2014 11:15 AM ✅Answered | 1 vote
Here is an example of being able to use arrows keys as well as have arrow key sensitive controls like buttons, scrollbars, etc on a form. With this concept you also have the ability to use the forms key related events like KeyUp, KeyDown contol events (not to be confused with arrow key up or arrow key down).
Add a button to a new form with this code: -
Public Class Form2
Const Increment As Integer = 5
Dim bmp As Bitmap
Dim rct As Rectangle
Dim F(4) As Boolean
Private Sub Form2_Load(sender As Object, e As System.EventArgs) Handles Me.Load
rct = New Rectangle(30, 30, 100, 100)
bmp = SystemIcons.Error.ToBitmap
Me.KeyPreview = True
Timer1.Interval = 20
Me.DoubleBuffered = True
Me.ClientSize = New Size(500, 500)
End Sub
Private Sub Form2_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawImage(bmp, rct)
End Sub
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If F(4) Then Return False
If keyData = Keys.Left Then F(0) = True
If keyData = Keys.Right Then F(1) = True
If keyData = Keys.Up Then F(2) = True
If keyData = Keys.Down Then F(3) = True
If F(0) Or F(1) Or F(2) Or F(3) Then
Button1.Enabled = False
'disable all other relevant controls here also
F(4) = True : Timer1.Start()
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Private Sub Form2_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Left Then F(0) = True
If e.KeyCode = Keys.Right Then F(1) = True
If e.KeyCode = Keys.Up Then F(2) = True
If e.KeyCode = Keys.Down Then F(3) = True
End Sub
Private Sub Form2_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.Left Then F(0) = False
If e.KeyCode = Keys.Right Then F(1) = False
If e.KeyCode = Keys.Up Then F(2) = False
If e.KeyCode = Keys.Down Then F(3) = False
End Sub
Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
' do something
End Sub
Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
If Not F(0) And Not F(1) And Not F(2) And Not F(3) Then
F(4) = False
Button1.Enabled = True
're-enable all other relevant controls too
Timer1.Stop()
Exit Sub
End If
If F(0) Then rct.X -= Increment
If F(1) Then rct.X += Increment
If F(2) Then rct.Y -= Increment
If F(3) Then rct.Y += Increment
Me.Invalidate()
End Sub
End Class
If you try this example you will see that the controls are temporarily disabled when you use the Arrow keys to move a sprite on the form. Diagonal movement is possible by pressing more than one arrow key.
Leon Stanley - ♪Don't pay the ferryman - till he gets you over to the other side♪ . um . I apply this to corporations - not to God.