How can i wait till R or spacebar is pressed?
I need something that does the following:
do while A>B
....
....
Do nothing until "r" or spacebar is pressed.
if "R or r" then ....
if " " then ....
...
..
wend
Visual Basic Classic
Last Comment
SpideyMod
8/22/2022 - Mon
DeAn
not sure exactly what you're looking for here.
this maybe?
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 32
MsgBox "Spacebar pressed"
Case 82
MsgBox "R or r pressed"
End Select
End Sub
sambha03
ASKER
No I need it to check for key pressed inside my own function and not in keydown event. I want it to wait till the keys are pressed and then do something.
Thanks RMatzka. KeyPressed(" ") works great. I guess I should have been more specific earlier. I need to wait until any key is pressed and then take action depening on if it is "r", " " or any other key. I can capture specific keys using the function but i am not sure how to implement soemthing like:
for 1=1 to 10
Do Until (some key is pressed) <---- how to implement this?
' wait till some key is pressed
Loop
if (KeyPressed(" ")) then action1
else
if ((KeyPressed("r")) then action2
else
action3
next
sambha03
ASKER
Is there a way to cancel the key captured using GetAsyncKeyState ? Whatever key I capture is getting printed on my textbox, which i want to avoid.
sambha03
ASKER
Wont be implementing this exercise anymore. Thanks for the effort. I'll ask community support to remove the question.
Are you sure you don't need it anymore? i was just a little too late. Well here it is anyway.
Private KeyPressed As Boolean
Private Sub Form_Load()
Print IIf(CalledFunction, "True", "False")
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
' Form1.KeyPreview = True (design-time only(important))
Select Case KeyCode
Case vbKeyR, vbKeySpace
KeyPressed = True
End Select
End Sub
Public CalledFunction() As Boolean
Do While Not KeyPressed
DoEvents
' your loop code here
Loop
CalledFunction = True
End Function
Don't forget the DoEvents statement, otherwise you can't do anything else.
qwasted
As for the cancelling of the key pressed, thats exactly what Im looking for too. I noticed you had that problem before (ie: Esc+R). I'll let you know if I find an answer to our common problem.
qwasted
As for the cancelling of the key pressed, thats exactly what Im looking for too. I noticed you had that problem before (ie: Esc+R). I'll let you know if I find an answer to our common problem.
So far this is the best I can find. Complex as it gets, but i think, for me anyway, it would save time to try and understand it than find a work-around.
Hope it helps.
sambha03
ASKER
Thanks qwasted for the pdf link. I'll check it out. As of now I dont need a solution to this question but thanks for the effort.I appreciate it.
this maybe?
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 32
MsgBox "Spacebar pressed"
Case 82
MsgBox "R or r pressed"
End Select
End Sub