?
Solved

Capture key presed inside a sub

Posted on 2003-03-01
19
Medium Priority
?
203 Views
Last Modified: 2010-05-01
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
Comment
Question by:sambha03
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 4
  • +2
19 Comments
 
LVL 3

Expert Comment

by:DeAn
ID: 8049031
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
 

Author Comment

by:sambha03
ID: 8049053
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
 
LVL 3

Expert Comment

by:DeAn
ID: 8049073
The VB constants are:
vbKeyR
vbKeySpace

does this help?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:DeAn
ID: 8049092
Private Sub WaitForKey(KeyCode As Integer)
    If KeyCode = vbKeyR Or KeyCode = vbKeySpace Then
        MsgBox "vbKeyR Or vbKeySpace"
    End If
End Sub
0
 

Author Comment

by:sambha03
ID: 8049164
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
 

Author Comment

by:sambha03
ID: 8049216
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
 
LVL 3

Expert Comment

by:DeAn
ID: 8049262
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
 

Expert Comment

by:RMatzka
ID: 8049408
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
 

Expert Comment

by:RMatzka
ID: 8049414
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
 

Author Comment

by:sambha03
ID: 8051334
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
 

Author Comment

by:sambha03
ID: 8051554
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
 

Author Comment

by:sambha03
ID: 8055562
Wont be implementing this exercise anymore. Thanks for the effort. I'll ask community support to remove the question.
0
 

Expert Comment

by:qwasted
ID: 8057307
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
 

Expert Comment

by:qwasted
ID: 8057341
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
 

Expert Comment

by:qwasted
ID: 8057785
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
 

Expert Comment

by:qwasted
ID: 8058130
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
 

Author Comment

by:sambha03
ID: 8059283
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
 

Expert Comment

by:qwasted
ID: 8060071
no problem
0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 8061888
PAQ'd and points refunded

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question