Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_activate_combo(self):
activations = []
def on_activate():
activations.append(True)
hk = HotKey({k.ctrl, kc.from_char('a')}, on_activate)
hk.press(kc.from_char('b'))
self.assertEqual(0, len(activations))
hk.release(kc.from_char('b'))
self.assertEqual(0, len(activations))
hk.press(kc.from_char('a'))
self.assertEqual(0, len(activations))
hk.release(kc.from_char('a'))
self.assertEqual(0, len(activations))
hk.press(k.ctrl)
self.assertEqual(0, len(activations))
hk.release(k.ctrl)
self.assertEqual(0, len(activations))
hk.press(kc.from_char('a'))
hk.press(k.ctrl)
self.assertEqual(1, len(activations))
hk.press(kc.from_char('a'))
def test_activate_single(self):
activations = []
def on_activate():
activations.append(True)
hk = HotKey({kc.from_char('a')}, on_activate)
hk.press(kc.from_char('b'))
self.assertEqual(0, len(activations))
hk.release(kc.from_char('b'))
self.assertEqual(0, len(activations))
hk.press(kc.from_char('a'))
self.assertEqual(1, len(activations))
hk.release(kc.from_char('a'))
self.assertEqual(1, len(activations))
hk.press(kc.from_char('a'))
self.assertEqual(2, len(activations))
hk.press(kc.from_char('a'))
self.assertEqual(2, len(activations))
hk.release(kc.from_char('a'))
hk.press(k.ctrl)
self.assertEqual(0, len(activations))
hk.release(k.ctrl)
self.assertEqual(0, len(activations))
hk.press(kc.from_char('a'))
hk.press(k.ctrl)
self.assertEqual(1, len(activations))
hk.press(kc.from_char('a'))
hk.press(k.ctrl)
self.assertEqual(1, len(activations))
hk.release(k.ctrl)
hk.press(k.ctrl)
self.assertEqual(2, len(activations))
hk.release(kc.from_char('a'))
hk.press(kc.from_char('a'))
self.assertEqual(3, len(activations))
hk.release(kc.from_char('b'))
self.assertEqual(0, len(activations))
hk.press(kc.from_char('a'))
self.assertEqual(1, len(activations))
hk.release(kc.from_char('a'))
self.assertEqual(1, len(activations))
hk.press(kc.from_char('a'))
self.assertEqual(2, len(activations))
hk.press(kc.from_char('a'))
self.assertEqual(2, len(activations))
hk.release(kc.from_char('a'))
self.assertEqual(2, len(activations))
hk.press(kc.from_char('A'))
self.assertEqual(3, len(activations))
hk.release(kc.from_char('A'))
self.assertEqual(3, len(activations))
def test_activate_combo(self):
activations = []
def on_activate():
activations.append(True)
hk = HotKey({k.ctrl, kc.from_char('a')}, on_activate)
hk.press(kc.from_char('b'))
self.assertEqual(0, len(activations))
hk.release(kc.from_char('b'))
self.assertEqual(0, len(activations))
hk.press(kc.from_char('a'))
self.assertEqual(0, len(activations))
hk.release(kc.from_char('a'))
self.assertEqual(0, len(activations))
hk.press(k.ctrl)
self.assertEqual(0, len(activations))
hk.release(k.ctrl)
self.assertEqual(0, len(activations))
hk.press(kc.from_char('a'))
hk.press(k.ctrl)
self.assertEqual(0, len(activations))
hk.press(kc.from_char('a'))
self.assertEqual(0, len(activations))
hk.release(kc.from_char('a'))
self.assertEqual(0, len(activations))
hk.press(k.ctrl)
self.assertEqual(0, len(activations))
hk.release(k.ctrl)
self.assertEqual(0, len(activations))
hk.press(kc.from_char('a'))
hk.press(k.ctrl)
self.assertEqual(1, len(activations))
hk.press(kc.from_char('a'))
hk.press(k.ctrl)
self.assertEqual(1, len(activations))
hk.release(k.ctrl)
hk.press(k.ctrl)
self.assertEqual(2, len(activations))
hk.release(kc.from_char('a'))
hk.press(kc.from_char('a'))
self.assertEqual(3, len(activations))
def test_activate_single(self):
activations = []
def on_activate():
activations.append(True)
hk = HotKey({kc.from_char('a')}, on_activate)
hk.press(kc.from_char('b'))
self.assertEqual(0, len(activations))
hk.release(kc.from_char('b'))
self.assertEqual(0, len(activations))
hk.press(kc.from_char('a'))
self.assertEqual(1, len(activations))
hk.release(kc.from_char('a'))
self.assertEqual(1, len(activations))
hk.press(kc.from_char('a'))
self.assertEqual(2, len(activations))
hk.press(kc.from_char('a'))
self.assertEqual(2, len(activations))
hk.release(kc.from_char('a'))
self.assertEqual(2, len(activations))
def test_activate_single(self):
activations = []
def on_activate():
activations.append(True)
hk = HotKey({kc.from_char('a')}, on_activate)
hk.press(kc.from_char('b'))
self.assertEqual(0, len(activations))
hk.release(kc.from_char('b'))
self.assertEqual(0, len(activations))
hk.press(kc.from_char('a'))
self.assertEqual(1, len(activations))
hk.release(kc.from_char('a'))
self.assertEqual(1, len(activations))
hk.press(kc.from_char('a'))
self.assertEqual(2, len(activations))
hk.press(kc.from_char('a'))
self.assertEqual(2, len(activations))
hk.release(kc.from_char('a'))
self.assertEqual(2, len(activations))
hk.press(kc.from_char('A'))
self.assertEqual(3, len(activations))
hk.release(kc.from_char('A'))
self.assertEqual(3, len(activations))
def _normalize(self, key):
"""Performs normalisation of a key.
:param key: The key to normalise.
:type key: Key or KeyCode
:return: a key
:rtype: Key or KeyCode
"""
from pynput.keyboard import Key, KeyCode, _NORMAL_MODIFIERS
if isinstance(key, KeyCode) and key.char is not None:
return KeyCode.from_char(key.char.lower())
elif isinstance(key, Key) and key.value in _NORMAL_MODIFIERS:
return _NORMAL_MODIFIERS[key.value]
else:
return key
keyboard_listener = keyboard.Listener(
on_press=on_press,
on_release=on_release)
keyboard_listener.start()
time.sleep(1)
# ============ keyboard control ============
keyboard_ctl = keyboard.Controller()
keyboard_ctl.press('a')
keyboard_ctl.release('a')
keyboard_ctl.press(KeyCode.from_char('b'))
keyboard_ctl.release(KeyCode.from_char('b'))
keyboard_ctl.press(Key.space)
keyboard_ctl.release(Key.space)
keyboard_ctl.press(getattr(Key, 'shift'))
keyboard_ctl.press('c')
keyboard_ctl.release('c')
keyboard_ctl.release(getattr(Key, 'shift'))
keyboard_ctl.press(KeyCode.from_vk(11))
keyboard_ctl.release(KeyCode.from_vk(11))
time.sleep(5)