Your question seems appropriate for this group, at least for me as I’ve tackled similar problems and used GuiTest to help me do it. 


Your reference to VK_DOWN on the IsKeyPressed line should have extra () on it, since it is a subroutine in guitest.pc, not a constant or scalar.  You have to use parens, so it would be IsKeyPressed(VK_DOWN()).


Also beware that IsKeyPressed() may not returning what you might expect (simple 1 or 0), since it’s just a wrapper for Windows’ GetAsyncKeyState().  I’ve had strange experiences with it, it doesn’t seem 100% reliable under stress.  Just wrapping it in a while loop and assuming it will return a positive when the key is pressed may be insufficient, despite the name IsKeyPressed which implies that’s how it would behave.  Try this first:


use Time::HiRes qw/usleep/; # This is lets us sleep less than 1 second

while (1) {

      print IsKeyPressed(0x28) . “\n”;

      usleep(100);  # sleep 1/10th of a second



Then run that and press the key you’re wondering about.  Make sure you understand it, I believe there’s 4 different possible return codes.  The key thing I had to do call it one extra time to clear the 1 state before you actually do the real check.  Another thing is instead of testing for truth, do the inverse:  Assume it is being pressed unless the function returns 0.  I don’t know why that helped me but it did.  J  So your final code might look like:


for (my $i=0; $i<=3; $i++) {
  IsKeyPressed(VK_DOWN());  # Throwaway the 1

  while (IsKeyPressed(VK_DOWN()) == 0) {

     # Do nothing(eek!).  BTW you might want to sleep a little here using the example above, at least usleep(50).  If not this will probably gobble 100% cpu


  my ($x, $y) = GetCursorPos();
  print "Button pressed while mouse at $x, $y\n";
  SendKeys"{PAUSE 200}";


What game are you controlling?  Just curious as I wrote a voice commad program for Silent Hunter 4 with help from GuiTest.




From: [] On Behalf Of Craig Conway
Sent: Monday, October 15, 2007 2:52 PM
Subject: [Winguitest-general] trouble with VK keys



   I hate to spam a newsgroup with newbie questions, so if there is a better forum for my question, please point me there and I won't clutter this mailing list again.

  I'm using Win32::GuiTest v1.50.5 to control a game and want to add a calibration step where the user clicks on certain areas of the screen so my program knows roughly where certain icons will be.  The questions below relate to that.

1) Recorder/Win32GuiTest.exe pretty much does what I need.  Am I correct in assuming the source code for it is not Perl?  If it is Perl, is the source code available?  I don't see it in the distribution.

2) I wrote a very short script to see if I could detect mouse clicks and movement.  It partially works.  Below is the code:

#!perl -w
use strict;

use Win32::GuiTest qw(:ALL :VK);

for (my $i=0; $i<=3; $i++) {
  while (! IsKeyPressed("a")) {};  ## This works
  #while (! IsKeyPressed(VK_DOWN)) {};  ## This does not work
  my ($x, $y) = GetCursorPos();
  print "Button pressed while mouse at $x, $y\n";
  SendKeys"{PAUSE 200}";

This is supposed to wait for four keypresses of the specified key and print the location of the mouse cursor each time.  It works fine when the 'a' key is used, but I can't get any of the VK* keys to work.  I've tried VK_LBUTTON, VK_DOWN, and VK_NUMPAD0.  Any idea what I'm doing wrong?  Is there a better way to wait for a mouse click or a key press?

3) I'd like to use Win32::GuiTest v1.54 or v1.55, but I don't have a C compiler or nmake on my system.  Is there a PPM-able version of 1.55?  I saw the emails about the latest version being released on CPAN, but PPM still only finds v1.30.

  Thanks very much,