How to use the ahk.script.ScriptEngine function in ahk

To help you get started, we’ve selected a few ahk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github spyoungtech / ahk / ahk / mouse.py View on Github external
:param button:
    :type button: str
    :return:
    """
    if isinstance(button, str):
        button = button.lower()

    if button in _BUTTONS:
        button = _BUTTONS.get(button)
    elif isinstance(button, int) and button > 3:
        #  for addtional mouse buttons
        button = f'X{button-3}'
    return button


class MouseMixin(ScriptEngine):
    """
    Provides mouse functionality for the AHK class
    """
    def __init__(self, mouse_speed=2, mode=None, **kwargs):
        if mode is None:
            mode = 'Screen'
        self.mode = mode
        self._mouse_speed = mouse_speed
        super().__init__(**kwargs)

    @property
    def mouse_speed(self):
        if callable(self._mouse_speed):
            return self._mouse_speed()
        else:
            return self._mouse_speed
github spyoungtech / ahk / ahk / keyboard.py View on Github external
def stop(self):
        """
        Stops the process if it is running
        """
        if not self.running:
            raise RuntimeError('Hotkey is not running')
        try:
            next(self._gen)
        except StopIteration:
            pass
        finally:
            del self._gen


class KeyboardMixin(ScriptEngine):
    def hotkey(self, *args, **kwargs):
        """
        Convenience function for creating ``Hotkey`` instance using current engine.

        :param args:
        :param kwargs:
        :return:
        """
        return Hotkey(engine=self, *args, **kwargs)

    def key_state(self, key_name, mode=None) -> bool:
        """
        Check the state of a key.

        https://autohotkey.com/docs/commands/GetKeyState.htm
github spyoungtech / ahk / ahk / screen.py View on Github external
import ast
from ahk.script import ScriptEngine
from typing import Tuple, Union, Optional


class ScreenMixin(ScriptEngine):
    def image_search(self, image_path: str,
                     upper_bound: Tuple[int, int]=(0, 0), lower_bound: Tuple[int, int]=None,
                     coord_mode: str='Screen',
                     scale_height: int=None, scale_width: int=None) -> Union[Tuple[int, int], None]:
        """
        `AutoHotkey ImageSearch reference`_

        .. _AutoHotkey ImageSearch reference: https://autohotkey.com/docs/commands/ImageSearch.htm


        :param image_path: path to the image file e.g. C:\location\of\cats.png
        :param upper_bound: a two-tuple of X,Y coordinates for the upper-left corner of the search area e.g. (200, 400)
        defaults to (0,0)

        :param lower_bound: like ``upper_bound`` but for the lower-righthand corner of the search area e.g. (400, 800)
        defaults to screen width and height (lower right-hand corner; ``%A_ScreenWidth%``, ``%A_ScreenHeight%``).
github spyoungtech / ahk / ahk / sound.py View on Github external
from ahk.script import ScriptEngine


class SoundMixin(ScriptEngine):
    def sound_beep(self, frequency=523, duration=150):
        """
        REF: https://autohotkey.com/docs/commands/SoundBeep.htm

        :param frequency: number between 37 and 32767
        :param duration: how long in milliseconds to play the beep
        :return: None
        """

        script = self.render_template('sound/beep.ahk', frequency=frequency, duration=duration)
        self.run_script(script)

    def sound_play(self, filename, blocking=True):
        """
        REF: https://autohotkey.com/docs/commands/SoundPlay.htm
github spyoungtech / ahk / ahk / window.py View on Github external
"""
        if escape:
            keys = escape_sequence_replace(keys)
        script = self._render_template('window/win_send.ahk', keys=keys, raw=raw, delay=delay, blocking=blocking)
        return self.engine.run_script(script, blocking=blocking)

    def __eq__(self, other):
        if not isinstance(other, Window):
            return False
        return self.id == other.id

    def __hash__(self):
        return hash(repr(self))


class WindowMixin(ScriptEngine):
    def __init__(self, *args, **kwargs):
        self.window_encoding = kwargs.pop('window_encoding', None)
        super().__init__(*args, **kwargs)

    def win_get(self, title='', text='', exclude_title='', exclude_text='', encoding=None):
        encoding = encoding or self.window_encoding
        script = self.render_template('window/get.ahk',
                                      subcommand='ID',
                                      title=title,
                                      text=text,
                                      exclude_text=exclude_text,
                                      exclude_title=exclude_title)
        ahk_id = self.run_script(script)
        return Window(engine=self, ahk_id=ahk_id, encoding=encoding)

    def win_set(self, subcommand, *args, blocking=True):