?
Solved

AutoIt _IsPressed function to detect keypress

Posted on 2012-08-21
8
Medium Priority
?
7,541 Views
Last Modified: 2012-08-23
I'm using AutoIt to write a program that will measure a person's reaction time.  The user will be asked to hit a specific random number (0 - 9) on the keyboard, and the program will measure the speed with which the user presses the key.

Here is a function from the AutoIt program:

Func GetKeyPress($a)
   While 1
	If _IsPressed('1b') = 1 Then Return(0) ;1b is ESC
	If _IsPressed($a) = 1 Then Return(1)
   Wend
EndFunc

Open in new window

This function works, but it doesn't quite do what I want it to do.

I want the function to return a zero if the user hits the wrong number key, not just if the user hits "Esc".  I suppose I could write a long "Select / Case" statement (which would cover all ten possible cases), but I wonder if there is an easier or more elegant way to accomplish this goal.

Thanks.
0
Comment
Question by:chernavsky
  • 5
  • 3
8 Comments
 
LVL 29

Expert Comment

by:matrixnz
ID: 38319420
If not _IsPressed($a) = 1 Then Return (0)

or

If _IsPressed($a) = 1 Then
  Return 1
Else
  Return 0
EndIf
0
 

Author Comment

by:chernavsky
ID: 38320108
@matrixnz: Sorry, but your suggestions don't work.  They cause the function to return a zero if no key is being pressed.  I need the function to wait until a key is pressed before returning a zero or a one.
0
 
LVL 29

Accepted Solution

by:
matrixnz earned 1200 total points
ID: 38321406
Sorry you're correct didn't test how about:
#include <Misc.au3>
Global $ARRAY[11] = [10, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
MsgBox(0,'', GetKeyPress(31))

Func GetKeyPress($a)
  While 1
    For $x = 1 To $ARRAY[0]
      If _IsPressed($a) Then Return (1)
      If _IsPressed($ARRAY[$x]) Then Return (0)
    Next
  Wend
EndFunc

Open in new window

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.

 

Author Comment

by:chernavsky
ID: 38322667
Matrixnz, that looks like it will work.  I don't have time to test it today --- will test soon and close out the question if your function works.  Thanks.
0
 

Author Comment

by:chernavsky
ID: 38325026
Matrixnz, I tested it, and it seems to work.  But now that I've had a chance to take a closer look at your code, I'm almost surprised that it DOES work.

The array called $ARRAY contains the code for the "correct" digit, as well as the codes for the nine "incorrect" digits.  So, you might expect that about half the time, the function would return a zero even if the user pressed the correct digit, depending on where in the "For" loop the user pressed the key.  In practice, that doesn't seem to happen, but I'm not sure why it doesn't.  Do you know?

If I were to implement this, I would be tempted to remove the code for the correct digit from the array.
0
 
LVL 29

Assisted Solution

by:matrixnz
matrixnz earned 1200 total points
ID: 38325445
Basically it works because of the evaluation order, for example when $a = 31 and $x = 31 $a is evaluated before $Array[$x].  Does that make sense?  If you really want to define it you can remove the number from the array or you could place something like:

#include <Misc.au3>
Global $ARRAY[11] = [10, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
MsgBox(0,'', GetKeyPress(31))

Func GetKeyPress($a)
  While 1
    For $x = 1 To $ARRAY[0]
      If $a == $ARRAY[$x] Then
        If _IsPressed($a) Then Return (1)
      Else
        If _IsPressed($ARRAY[$x]) Then Return (0)
      EndIf
    Next
  Wend
EndFunc

Open in new window

0
 

Author Comment

by:chernavsky
ID: 38325626
Yeah, I understand that the statements are evaluated in that order, but the keypress can theoretically happen during the split-second that occurs after the first statement is evaluated and before the second statement is evaluated.

In any case, you've solved the problem, and I appreciate it.  Thanks.
0
 

Author Closing Comment

by:chernavsky
ID: 38325630
Thanks again.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Progress
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

864 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