'''
(ok)
How to listen Key combination?
Here is a modified version that also prints a message when you release that key combination!
'''
from pynput.keyboard import Listener, Key

class MockButton:
    def __init__(self, keys: list, cb_fn):
        self.cb_fn = cb_fn
        self.combination = set(keys)
        # self.currently_pressed = set()
        self.currently_pressed = None
        self.is_pressed = False

        listener = Listener(on_press=self._on_press, on_release=self._on_release)
        listener.start()

    def _on_press(self, key):
        if key in self.combination:
            # self.currently_pressed.add(key)
            self.currently_pressed = key

            self.is_pressed = True
            # print('pressed!')
            self.cb_fn(0)

    def _on_release(self, key):
        try:
            self.currently_pressed = None

            if self.is_pressed and self.currently_pressed == None:
                self.is_pressed = False
                # print('released!')
                self.cb_fn(1)

        except KeyError:
            pass


if __name__ == '__main__':
    keys = [Key.alt, Key.ctrl]
    cb_fn = lambda x: print(x)
    btn = MockButton(keys, cb_fn)
    input()
