0
\$\begingroup\$

I'm working on a multiplatform open source clone of tinytask, specifically on the play/stop feature.

Now, it looks like:

    def on_press(self, key):
        if not self.recording:
            return False
        b = time.perf_counter()
        self.capture.append(f"time.sleep ({b - self.last_time})")
        self.last_time = b

        try:
            self.capture.append(f"pyautogui.keyDown({repr(key.char)})")

        except AttributeError:
            if key == keyboard.Key.alt:
                if platform.system() == "Darwin":
                    self.capture.append(f"pyautogui.keyDown('option')")
                else:
                    self.capture.append(f"pyautogui.keyDown('alt')")
            ##################################################
            #### I cut here because it's a bit repetitive ####
            ##################################################
            else:
                self.capture.append(f"### {key} is not supported yet")
    def on_release(self, key):
        if not self.recording:
            return False
        #self.count_perf()
        if key == keyboard.Key.alt:
            if platform.system() == "Darwin":
                self.capture.append(f"pyautogui.keyUp('option')")
            else:
                self.capture.append(f"pyautogui.keyUp('alt')")
            ##################################################
            #### I cut here because it's a bit repetitive ####
            ##################################################
        else:
            self.capture.append(f"pyautogui.keyUp({repr(key)})")

In order to make the play feature as library-agnostic (possibly to add the option later to change the backend) as possible, I refactored with the following function:

    def write_keyboard_action(self, engine="pyautogui", move="", key=""):
        """
        Append keyboard actions to the class variable capture
        Keyword Arguments:
        - engine: the module which will be used for the replay
        - move: keyDown | keyUp
        - key: The key pressed
        """
        suffix = "(" + repr(key) + ")"
        if move == "keyDown":
            # Corner case: Multiple successive keyDown
            if move + suffix in self._capture[-1]:
                move = 'press'
                self._capture[-1] = engine + "." + move + suffix
        self._capture.append(engine + "." + move + suffix)

To have something like:

    def on_release(self, key):
        if not self.recording:
            return False
        if key == keyboard.Key.alt:
            if platform.system() == "Darwin":
                self.write_keyboard_action(move='keyUp', key='option')
            else:
                self.write_keyboard_action(move='keyUp', key='alt')
            ##################################################
            #### I cut here because it's a bit repetitive ####
            ##################################################  
        else:
            self.write_keyboard_action(move='keyUp', key=key)
    def on_press(self, key):
        if not self.recording:
            return False
        b = time.perf_counter()
        timeout = int(b - self.last_time)
        if timeout > 0:
            self._capture.append(f"time.sleep({timeout})")
        self.last_time = b

        try:
            # Ignore presses on Fn key
            if key.char:
                self.write_keyboard_action(move='keyDown', key=key.char)

        except AttributeError:
            if key == keyboard.Key.alt:
                if platform.system() == "Darwin":
                    self.write_keyboard_action(move="keyDown", key='option')
                else:
                    self.write_keyboard_action(move="keyDown", key='alt')
            ##################################################
            #### I cut here because it's a bit repetitive ####
            ################################################## 
            else:
                self._capture.append(f"### {key} is not supported yet")

But I have the feeling that something better than this is possible. Any suggestion ?

\$\endgroup\$
2
  • \$\begingroup\$ Your title should explain what your code does, not what you want done to it. PS: AlexV should have made that part of their edit... \$\endgroup\$ – FreezePhoenix Feb 20 '20 at 12:18
  • \$\begingroup\$ What does your code do? Make that the title. \$\endgroup\$ – FreezePhoenix Feb 20 '20 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.