OnKey interference issues

geo77

New Member
Joined
May 26, 2009
Messages
31
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
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Geo,

I've read your PM and your post but do not have time to research / answer at the moment (have a graduation ceremony to go to) but will hopefully get back with you today.

Take care,

Owen
 
Upvote 0
Geo,

I've had time to review what you are doing and I understand better your problems:

Count is just a generic counter which if I run, the keys won't work right or at all

The reason the keys won't work while you are running the 'Count' subroutine is that the onkey changes you made are not being implemented by the excel application - it is busy running the 'Count' code. The spin buttons are being controlled by regular keystrokes which is why they are working for you.

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.

This odd behavior is because once you select a spin button the arrow keys are now controlling the selected spin button, the onkey functions are not being processed.

My results of using GetAsyncKeyState have been mediocre.

I use GetAsyncKeyState exclusively for the games I write and it works flawlessly for me so I don't know what problems you are having. If you need assistance with using them, PM me and I will see what I can do to assist.

Take care.

Owen
 
Upvote 0
Thanks Owen,

That sounds great. I am not sure what is wrong with my account settings. Every option looks right and still I cannot receive email. It might be the website. I get error and warning messages for almost every action from posting to trivial searching of the board.

I will read the link and try applying the info. If worse comes to worst I need to figure out the code the other fellow is using.

Regards, Geo
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,039
Members
449,063
Latest member
ak94

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top