Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Keystroke Within Loop

Status
Not open for further replies.

Stainnd

Programmer
Jul 9, 2001
65
US
If I want a procedure to run every time a specific key is pressed, I would use the code:

Application.OnKey("Key",Procedure)

My problem is that if "Key" is pressed while a Sub is running, it won't be processed to after the Sub completes. What I would like to do is have a Sub that is running react to a keystroke. For instance, in the classic game Nibbles, have a block move up until the left key is pressed, which would cause the block to move to the left, etc. Any ideas?

-Mike

-Mike
 
This would be very difficult in VBA as it generally shuts down all user interaction until the code has finished. the only real exception is the ESC key which stops the code

Rgds, Geoff

Never test the depth of water with both feet

Help us to help you by reading FAQ222-2244 before you ask a question
 
I figured it out thanks to the magic of open source. I don't actually understand why it works, but I know that it does work. In general declarations, include the following code:

Declare Function GetAsyncKeyState Lib "User32.dll" (ByVal vKey As Long) As Long

Const VK_LEFT As Long = &H25

Then create a new function with this code:

Do
If GetAsyncKeyState(VK_LEFT) <> 0 Then
MsgBox "Hello!"
Exit Do
End If
Loop

The if you press the Left key, the function will show the MsgBox and exit the loop. I'm sure y'all can find some very interesting ways of using this code (personally, I'm going to create an Excel version of Nibbles right now).

-Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top