AutoIt _IsPressed function to detect keypress

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.
chernavskyAsked:
Who is Participating?
 
matrixnzConnect With a Mentor Commented:
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
 
matrixnzCommented:
If not _IsPressed($a) = 1 Then Return (0)

or

If _IsPressed($a) = 1 Then
  Return 1
Else
  Return 0
EndIf
0
 
chernavskyAuthor Commented:
@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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
chernavskyAuthor Commented:
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
 
chernavskyAuthor Commented:
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
 
matrixnzConnect With a Mentor Commented:
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
 
chernavskyAuthor Commented:
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
 
chernavskyAuthor Commented:
Thanks again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.