Waiting for image, type command, then waits again

Asked by layanor

I am looking for a simple script that will watch for an image, type the letter c until the image is gone. then wait for the image to come back again indefinitely.

I have looked at many videos and tutorials and they all have huge amounts of code that all differ from each other.

Settings.MoveMouseDelay = 0
running = True

def runHotkey(event):
    global running
    running = False

Env.addHotkey(Key.F1, KeyModifier.ALT, runHotkey)
#for step in range(5): #for i in range(0,40):

hover("1501408565660.png")
for i in range(0,40):
     while exists("1501408565660.png")and running:
          click("1501409544348.png")
     click("1501408565660.png")

     if exists("1501413313685.png"):
          sleep#(30)

     if exists("1501410531123.png"):
          sleep#(30)

     if exists("1501413618782.png"):
          sleep#(30)

This kinda works but there has to be a much better way. Also the script wont end right away when I try to stop it. It lags for more than 3 mins before stopping.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Manfred Hampl
Solved:
Last query:
Last reply:
Revision history for this message
TestMechanic (ndinev) said :
#1

"I am looking for a simple script that will watch for an image, type the letter c until the image is gone. then wait for the image to come back again indefinitely."

Here is possible solution to your initial question:

while True:
    while not exists(image1):
         sleep(1)
    while exists(image1):
        type("c")
        sleep(1)

Revision history for this message
layanor (layanor) said :
#2

This does work. However typing c is going very slowly. Is there a way to make this quickly respond?

Also thank you for helping me out. Cutting all of that extra code out was very helpfull!

Revision history for this message
TestMechanic (ndinev) said :
#3

Change the sleep(1) to sleep(0.1) after type("c")
Sleep time is in seconds - 0.1 is 100ms

However please note that exist command also take some non zero time to be executed

Revision history for this message
layanor (layanor) said :
#4

Thank you very much!

I would like to randomize the keys it hits. One of six keys until the picture is gone.

a,s,d,z,x,c

Do I keep asking questions in this thread or a new one?

Revision history for this message
TestMechanic (ndinev) said :
#5

Do you really need them random ... or just shoot them in particular order is ok.

Random means that some key may never be send...

Revision history for this message
layanor (layanor) said :
#6

Yes random. I know it is a odd request :)

Also, how do I lock these key presses to a specific window so I can be doing other things as these are typing?

Revision history for this message
layanor (layanor) said :
#7

test

Revision history for this message
Best Manfred Hampl (m-hampl) said :
#8

For a random letter you can use python standard functions. Something like

myList=["a","s","d","z","x","c"]
import random
for x in range(50):
  print myList[random.randint(0,5)]

Revision history for this message
layanor (layanor) said :
#9

Thanks Manfred Hampl, that solved my question.