Avatar of sambha03
sambha03
 asked on

Capture key presed inside a sub

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

Avatar of undefined
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.
DeAn

The VB constants are:
vbKeyR
vbKeySpace

does this help?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
DeAn

Private Sub WaitForKey(KeyCode As Integer)
    If KeyCode = vbKeyR Or KeyCode = vbKeySpace Then
        MsgBox "vbKeyR Or vbKeySpace"
    End If
End Sub
sambha03

ASKER
Thanks for the help but thats not what i m looking for.
I need to capture key pressed inside my own function.

to give a better example, lets say  i have to implement soemthing like as follows ( please excuse my vb syntax :-)  

i=0
while i< 10
 wait until some key pressed
 if key pressed is r then i=i+2
 if key pressed is spacebar then i=i+3

wend
sambha03

ASKER
Thanks for the help but thats not what i m looking for.
I need to capture key pressed inside my own function.

to give a better example, lets say  i have to implement soemthing like as follows ( please excuse my vb syntax :-)  

i=0
while i< 10
 wait until some key pressed
 if key pressed is r then i=i+2
 if key pressed is spacebar then i=i+3

wend
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
DeAn

this the same as what you typed, but in correct syntax

Dim i As Integer
i = 0
While i < 10
    If KeyCode = vbKeyR Then i = i + 2
    If KeyCode = vbKeySpace Then i = i + 3
Wend

sorry, I still don't understand what you are trying to accomplish ;-)

this code will require 5 "R" presses or 4 Space presses, or a combo of the two.

could you explain what you're function is doing, or paste the code into your comment?
RMatzka

Maybe this is what you are looking for?

Function KeyPressed(Key As String) As Boolean
   KeyPressed = ((GetAsyncKeyState(Asc(UCase(Key))) And &H8000) = &H8000)
End Function

You can call this function from your function at any time, e.g.
if KeyPressed("r") then ...
if KeyPressed(" ") then ...
RMatzka

You need this declaration at the top of your module or form:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
sambha03

ASKER
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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
qwasted

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
qwasted

http://archive.devx.com/premier/mgznarch/vbpj/1999/08aug99/cb0899.pdf

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.
qwasted

no problem
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
SpideyMod

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.