In any language/script, how can I send keys via pixel/pic?

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Rose
Newbie
Posts: 1
Joined: Wed Jul 11, 2007 5:11 pm

In any language/script, how can I send keys via pixel/pic?

Post by Rose » Wed Jul 11, 2007 5:14 pm

I want to be able to send a keypress (such as left, right, etc) based on pixel color on the screen. For example, if this pixel equals this value, send this keypress. What is the best way to do this?

I might add that this would have to be scanning the screen constantly looking at the same spot until the moment it spots the desired pixel, then sending the key

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Jul 12, 2007 8:21 pm

Here's a demo program. See the comments and read help for functions you're not familiar with. This will detect black and white on the pixel located in the center of your screen.

Code: Select all

//Setting OnEvent to detect the press of the Esc key
//the press will trigger subroutine "Esc" which will set
//variable Stop_Flag to 1.  When the Stop_Flag variable is detected as 1
//by the Stop_Flag if> statement,  The script will jump to label EOF and end.
OnEvent>KEY_DOWN,VK27,0,Esc
Let>Stop_Flag=0

//Using this to get a point in the middle of the screen
//You can designate any point or points in the GetPixelColor> function.
//Next three lines for demo purposes only.
GetScreenRes>Scr_X,Scr_Y
Let>Scr_X=%Scr_X%/2
Let>Scr_Y=%Scr_Y%/2

//An endless loop that will continuously detect the color at the
//designated location using GetPixelColor.  If> statements will jump
//to specific subroutines based on specific colors being detected. 
Label>GetPixelColorLoop
GetPixelColor>Scr_X,Scr_Y,color
If>Stop_Flag=1,EOF
If>%color%=16777215,White
If>%color%=0,Black
//A short wait to keep the processor from overheating
Wait>0.001
Goto>GetPixelColorLoop

Label>EOF

SRT>White
  //Modal message for demonstration purposes
  MDL>Screen Position %Scr_X%,%Scr_Y% is white
  //Press anykey

  //wait some amount of time for the screen to change
  //or it will continue pressing the designated key every
  //thousandth of a second.
  Wait>2
END>White

SRT>Black
  MDL>Screen Position %Scr_X%,%Scr_Y% is black
  //Press anykey
  Wait>2
END>Black

SRT>Esc
  Let>Stop_Flag=1
END>Esc

User avatar
pgriffin
Automation Wizard
Posts: 460
Joined: Wed Apr 06, 2005 5:56 pm
Location: US and Europe

Post by pgriffin » Fri Jul 13, 2007 1:17 am

Or, if you are looking constantly at the same spot, you might try

WaitPixelColor>ColorCode,X,Y,Timeout...

So

WaitPixelColor>16777215,100,333,60

would wait 60 seconds for the pixel at 100,333 to turn white.

then based on the value of WPC_RESULT, you could perform what ever action needed.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts