• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

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
0
sambha03
Asked:
sambha03
  • 7
  • 5
  • 4
  • +2
1 Solution
 
DeAnCommented:
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
0
 
sambha03Author Commented:
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.
0
 
DeAnCommented:
The VB constants are:
vbKeyR
vbKeySpace

does this help?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
DeAnCommented:
Private Sub WaitForKey(KeyCode As Integer)
    If KeyCode = vbKeyR Or KeyCode = vbKeySpace Then
        MsgBox "vbKeyR Or vbKeySpace"
    End If
End Sub
0
 
sambha03Author Commented:
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
0
 
sambha03Author Commented:
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
0
 
DeAnCommented:
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?
0
 
RMatzkaCommented:
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 ...
0
 
RMatzkaCommented:
You need this declaration at the top of your module or form:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
0
 
sambha03Author Commented:
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
0
 
sambha03Author Commented:
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.
0
 
sambha03Author Commented:
Wont be implementing this exercise anymore. Thanks for the effort. I'll ask community support to remove the question.
0
 
qwastedCommented:
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.
0
 
qwastedCommented:
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.
0
 
qwastedCommented:
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.
0
 
qwastedCommented:
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.
0
 
sambha03Author Commented:
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.
0
 
qwastedCommented:
no problem
0
 
SpideyModCommented:
PAQ'd and points refunded

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 7
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now