← Back to team overview

sikuli-driver team mailing list archive

[Question #270104]: Script runs simultaneous with hotkey function

 

New question #270104 on Sikuli:
https://answers.launchpad.net/sikuli/+question/270104

I am running into an issue with a hotkey function. I am running this through the IDE and using version 1.0.1. When I run this, I see the steps in my log file. When I press the hotkey to pause the script, it runs the pause_unpause_script function and writes to the log as it should. What happens next I cannot figure out. The script keeps running the steps and breaks where appropriate, but it should not be continuing. It should be stuck in the infinite loop of the pause function until I press the hotkey again. 

Script:

import sys
import logging
import os
import __builtin__ as pyth
from datetime import *
from time import time, sleep
from math import *
from random import *
from sikuli.Sikuli import *

PAUSED = False

def setup_logger(file_name='me.log', file_mode='w+', logger_name=__name__):
    return_log = logging.getLogger(logger_name)
    return_log.setLevel(level=logging.INFO)
    handler = logging.FileHandler(filename=file_name, mode=file_mode)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M %p')
    handler.setFormatter(formatter)
    return_log.addHandler(handler)
    return_log.propagate = False
    return return_log


def intWithCommas(x):
    if pyth.type(x) not in [pyth.type(0), pyth.type(0L)]:
        raise TypeError("Parameter must be an integer.")
    if x < 0:
        return '-' + intWithCommas(-x)
    result = ''
    while x >= 1000:
        x, r = divmod(x, 1000)
        result = ",%03d%s" % (r, result)
    return "%d%s" % (x, result)


def pause_unpause_script(event=None):
    global PAUSED
    PAUSED = not PAUSED
    if PAUSED:
        logger.info('Pausing script...Press Pause to unpause...')
        while PAUSED:
            sleep(10)
            popup("Waited ten seconds")
    else:
        logger.info('Unpausing script...Press Pause Lock to pause...')
        # TODO: Add clicking of 'Confirm' box and event windows
        

# ========================== #
# ====    Add HotKeys   ==== #
# ========================== #
Env.addHotkey(Key.F1, KeyModifier.CTRL, exit_script)
Env.addHotkey(Key.PAUSE, 0, pause_unpause_script)

logger = setup_logger(file_name="C:\\test.log")

i = 0
while True:
    if i >= 25:
        break
    i += 1
    logger_step = "Step: %s" % intWithCommas(i)
    logger.info(logger_step)
    sleep(2)

Log Output:

08/07/2015 10:25 AM - __main__ - INFO - Step: 1
08/07/2015 10:25 AM - __main__ - INFO - Step: 2
08/07/2015 10:25 AM - __main__ - INFO - Step: 3
08/07/2015 10:25 AM - __main__ - INFO - Step: 4
08/07/2015 10:25 AM - __main__ - INFO - Step: 5
08/07/2015 10:25 AM - __main__ - INFO - Step: 6
08/07/2015 10:25 AM - __main__ - INFO - Step: 7
08/07/2015 10:25 AM - __main__ - INFO - Step: 8
08/07/2015 10:25 AM - __main__ - INFO - Step: 9
08/07/2015 10:25 AM - __main__ - INFO - Step: 10
08/07/2015 10:25 AM - __main__ - INFO - Pausing script...Press Pause to unpause...
08/07/2015 10:25 AM - __main__ - INFO - Step: 11
08/07/2015 10:25 AM - __main__ - INFO - Step: 12
08/07/2015 10:25 AM - __main__ - INFO - Step: 13
08/07/2015 10:25 AM - __main__ - INFO - Step: 14
08/07/2015 10:25 AM - __main__ - INFO - Step: 15
08/07/2015 10:25 AM - __main__ - INFO - Step: 16
08/07/2015 10:25 AM - __main__ - INFO - Step: 17
08/07/2015 10:25 AM - __main__ - INFO - Step: 18
08/07/2015 10:25 AM - __main__ - INFO - Step: 19
08/07/2015 10:25 AM - __main__ - INFO - Step: 20
08/07/2015 10:25 AM - __main__ - INFO - Step: 21
08/07/2015 10:25 AM - __main__ - INFO - Step: 22
08/07/2015 10:26 AM - __main__ - INFO - Step: 23
08/07/2015 10:26 AM - __main__ - INFO - Step: 24
08/07/2015 10:26 AM - __main__ - INFO - Step: 25
08/07/2015 10:26 AM - __main__ - INFO - Unpausing script...Press Pause Lock to pause...

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.