From: Erik K. <er...@ma...> - 2007-10-16 00:01:46
|
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. =20 =20 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()). =20 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: =20 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 } =20 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: =20 for (my $i=3D0; $i<=3D3; $i++) {=20 IsKeyPressed(VK_DOWN()); # Throwaway the 1 while (IsKeyPressed(VK_DOWN()) =3D=3D 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) =3D GetCursorPos();=20 print "Button pressed while mouse at $x, $y\n";=20 SendKeys"{PAUSE 200}";=20 } =20 What game are you controlling? Just curious as I wrote a voice commad program for Silent Hunter 4 with help from GuiTest. =20 Erik =20 From: win...@li... [mailto:win...@li...] On Behalf Of Craig Conway Sent: Monday, October 15, 2007 2:52 PM To: win...@li... Subject: [Winguitest-general] trouble with VK keys =20 Hi,=20 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.=20 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.=20 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.=20 2) I wrote a very short script to see if I could detect mouse clicks and movement. It partially works. Below is the code:=20 #!perl -w=20 use strict;=20 use Win32::GuiTest qw(:ALL :VK);=20 for (my $i=3D0; $i<=3D3; $i++) {=20 while (! IsKeyPressed("a")) {}; ## This works=20 #while (! IsKeyPressed(VK_DOWN)) {}; ## This does not work=20 my ($x, $y) =3D GetCursorPos();=20 print "Button pressed while mouse at $x, $y\n";=20 SendKeys"{PAUSE 200}";=20 }=20 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?=20 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.=20 Thanks very much,=20 Craig |