Hi guys,
I've been working on games lately and excel is a great tool for that except I couldn't figure how to properly use the arrow keys for the controls. My results of using GetAsyncKeyState have been mediocre.
The OnKey is what I need since I've seen a Tetris on the internet which responds very nicely to the control keys, namely it counts the number of times you hit a key and it does the same number of jumps. This is exactly how my spin buttons behave right now.
The onkey controls work well for me when the game is stopped but when I have another macro (a loop for instance) running in the background, the key response is null.
If I use both keys and buttons the keys start working AFTER I do the same command using spin buttons, like hitting the spin button enables the key. The problem with that is that there is always a corruption of the controls (if I hit a "move horizontal" spin button) all 4 keys will trigger horizontal moves after that (including the up/down keys).
Here are my macros:
Public stp As Boolean
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub SetKeys()
Application.OnKey "{Right}", "Right"
Application.OnKey "{Left}", "Left"
Application.OnKey "{Up}", "Up"
Application.OnKey "{Down}", "Down"
End Sub
Private Sub ResetKeys()
Application.OnKey "{Right}"
Application.OnKey "{Left}"
Application.OnKey "{Up}"
Application.OnKey "{Down}"
End Sub
Private Sub Right()
On Error Resume Next
Range("B2") = Range("B2") + 1
End Sub
Private Sub Left()
On Error Resume Next
Range("B2") = Range("B2") - 1
End Sub
Private Sub Up()
On Error Resume Next
Range("C2") = Range("C2") + 1
End Sub
Private Sub Down()
On Error Resume Next
Range("C2") = Range("C2") - 1
End Sub
Private Sub Horizontal_Change()
Range("B2") = Horizontal.Value
End Sub
Private Sub Vertical_Change()
Range("C2") = Vertical.Value
End Sub
Private Sub Count()
stp = False
For n = 1 To 1000
DoEvents
Application.EnableEvents = True
If stp Then Exit Sub
Range("B5") = Range("B5") + 1
DoEvents
Sleep 200
Next n
End Sub
Sub StopCount()
stp = True
End Sub
Here Count is just a generic counter which if I run, the keys won't work right or at all. I also have 2 spin buttons on the sheet for horizontal/vertical movement of the point (x,y)=(B2,C2).
They work great at all times. I am trying to do the same with the arrow keys. Of course whey I open the sheet I run "SetKeys" before anything else. After that I try the arrow keys. Everything looks great, the point (B2,C2) moves as I wish on a 2D scatter plot. After that I start the "Count" and while this is running the arrow keys become dead. If I click the Horizontal button then the arrow keys come back to life but now the up/down keys also move the point on horizontal. After that I click the "Vertical" spin button and as a consequence the ALL the arrow keys have a vertical effect on the point, which really means the left key will do the job of the up key also and the right key will do the job of the down key.
Of course I put this:
Sub Horizontal_Change()
Range("b2") = Horizontal.Value
End Sub
Sub Vertical_Change()
Range("c2") = Vertical.Value
End Sub
On Sheet1 and the rest on Module1. I am not sure why, but that's how I manage to make it work.
Thanks, Geo
I've been working on games lately and excel is a great tool for that except I couldn't figure how to properly use the arrow keys for the controls. My results of using GetAsyncKeyState have been mediocre.
The OnKey is what I need since I've seen a Tetris on the internet which responds very nicely to the control keys, namely it counts the number of times you hit a key and it does the same number of jumps. This is exactly how my spin buttons behave right now.
The onkey controls work well for me when the game is stopped but when I have another macro (a loop for instance) running in the background, the key response is null.
If I use both keys and buttons the keys start working AFTER I do the same command using spin buttons, like hitting the spin button enables the key. The problem with that is that there is always a corruption of the controls (if I hit a "move horizontal" spin button) all 4 keys will trigger horizontal moves after that (including the up/down keys).
Here are my macros:
Public stp As Boolean
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub SetKeys()
Application.OnKey "{Right}", "Right"
Application.OnKey "{Left}", "Left"
Application.OnKey "{Up}", "Up"
Application.OnKey "{Down}", "Down"
End Sub
Private Sub ResetKeys()
Application.OnKey "{Right}"
Application.OnKey "{Left}"
Application.OnKey "{Up}"
Application.OnKey "{Down}"
End Sub
Private Sub Right()
On Error Resume Next
Range("B2") = Range("B2") + 1
End Sub
Private Sub Left()
On Error Resume Next
Range("B2") = Range("B2") - 1
End Sub
Private Sub Up()
On Error Resume Next
Range("C2") = Range("C2") + 1
End Sub
Private Sub Down()
On Error Resume Next
Range("C2") = Range("C2") - 1
End Sub
Private Sub Horizontal_Change()
Range("B2") = Horizontal.Value
End Sub
Private Sub Vertical_Change()
Range("C2") = Vertical.Value
End Sub
Private Sub Count()
stp = False
For n = 1 To 1000
DoEvents
Application.EnableEvents = True
If stp Then Exit Sub
Range("B5") = Range("B5") + 1
DoEvents
Sleep 200
Next n
End Sub
Sub StopCount()
stp = True
End Sub
Here Count is just a generic counter which if I run, the keys won't work right or at all. I also have 2 spin buttons on the sheet for horizontal/vertical movement of the point (x,y)=(B2,C2).
They work great at all times. I am trying to do the same with the arrow keys. Of course whey I open the sheet I run "SetKeys" before anything else. After that I try the arrow keys. Everything looks great, the point (B2,C2) moves as I wish on a 2D scatter plot. After that I start the "Count" and while this is running the arrow keys become dead. If I click the Horizontal button then the arrow keys come back to life but now the up/down keys also move the point on horizontal. After that I click the "Vertical" spin button and as a consequence the ALL the arrow keys have a vertical effect on the point, which really means the left key will do the job of the up key also and the right key will do the job of the down key.
Of course I put this:
Sub Horizontal_Change()
Range("b2") = Horizontal.Value
End Sub
Sub Vertical_Change()
Range("c2") = Vertical.Value
End Sub
On Sheet1 and the rest on Module1. I am not sure why, but that's how I manage to make it work.
Thanks, Geo