Best Python code snippet using fMBT_python
_keyboard_tests.py
Source:_keyboard_tests.py
1# -*- coding: utf-8 -*-2"""3Side effects are avoided using two techniques:4- Low level OS requests (keyboard._os_keyboard) are mocked out by rewriting5the functions at that namespace. This includes a list of dummy keys.6- Events are pumped manually by the main test class, and accepted events7are tested against expected values.8Fake user events are appended to `input_events`, passed through9keyboard,_listener.direct_callback, then, if accepted, appended to10`output_events`. Fake OS events (keyboard.press) are processed11and added to `output_events` immediately, mimicking real functionality.12"""13from __future__ import print_function14import unittest15import time16import keyboard17from ._keyboard_event import KeyboardEvent, KEY_DOWN, KEY_UP18dummy_keys = {19 'space': [(0, [])],20 'a': [(1, [])],21 'b': [(2, [])],22 'c': [(3, [])],23 'A': [(1, ['shift']), (-1, [])],24 'B': [(2, ['shift']), (-2, [])],25 'C': [(3, ['shift']), (-3, [])],26 'alt': [(4, [])],27 'left alt': [(4, [])],28 'left shift': [(5, [])],29 'right shift': [(6, [])],30 'left ctrl': [(7, [])],31 'backspace': [(8, [])],32 'caps lock': [(9, [])],33 '+': [(10, [])],34 ',': [(11, [])],35 '_': [(12, [])],36 'none': [],37 'duplicated': [(20, []), (20, [])],38}39def make_event(event_type, name, scan_code=None, time=0):40 return KeyboardEvent(event_type=event_type, scan_code=scan_code or dummy_keys[name][0][0], name=name, time=time)41# Used when manually pumping events.42input_events = []43output_events = []44def send_instant_event(event):45 if keyboard._listener.direct_callback(event):46 output_events.append(event)47# Mock out side effects.48keyboard._os_keyboard.init = lambda: None49keyboard._os_keyboard.listen = lambda callback: None50keyboard._os_keyboard.map_name = dummy_keys.__getitem__51keyboard._os_keyboard.press = lambda scan_code: send_instant_event(make_event(KEY_DOWN, None, scan_code))52keyboard._os_keyboard.release = lambda scan_code: send_instant_event(make_event(KEY_UP, None, scan_code))53keyboard._os_keyboard.type_unicode = lambda char: output_events.append(KeyboardEvent(event_type=KEY_DOWN, scan_code=999, name=char))54# Shortcuts for defining test inputs and expected outputs.55# Usage: d_shift + d_a + u_a + u_shift56d_a = [make_event(KEY_DOWN, 'a')]57u_a = [make_event(KEY_UP, 'a')]58du_a = d_a+u_a59d_b = [make_event(KEY_DOWN, 'b')]60u_b = [make_event(KEY_UP, 'b')]61du_b = d_b+u_b62d_c = [make_event(KEY_DOWN, 'c')]63u_c = [make_event(KEY_UP, 'c')]64du_c = d_c+u_c65d_ctrl = [make_event(KEY_DOWN, 'left ctrl')]66u_ctrl = [make_event(KEY_UP, 'left ctrl')]67du_ctrl = d_ctrl+u_ctrl68d_shift = [make_event(KEY_DOWN, 'left shift')]69u_shift = [make_event(KEY_UP, 'left shift')]70du_shift = d_shift+u_shift71d_alt = [make_event(KEY_DOWN, 'alt')]72u_alt = [make_event(KEY_UP, 'alt')]73du_alt = d_alt+u_alt74du_backspace = [make_event(KEY_DOWN, 'backspace'), make_event(KEY_UP, 'backspace')]75du_capslock = [make_event(KEY_DOWN, 'caps lock'), make_event(KEY_UP, 'caps lock')]76d_space = [make_event(KEY_DOWN, 'space')]77u_space = [make_event(KEY_UP, 'space')]78du_space = [make_event(KEY_DOWN, 'space'), make_event(KEY_UP, 'space')]79trigger = lambda e=None: keyboard.press(999)80triggered_event = [KeyboardEvent(KEY_DOWN, scan_code=999)]81class TestKeyboard(unittest.TestCase):82 def tearDown(self):83 keyboard.unhook_all()84 #self.assertEquals(keyboard._hooks, {})85 #self.assertEquals(keyboard._hotkeys, {})86 def setUp(self):87 #keyboard._hooks.clear()88 #keyboard._hotkeys.clear()89 del input_events[:]90 del output_events[:]91 keyboard._recording = None92 keyboard._pressed_events.clear()93 keyboard._physically_pressed_keys.clear()94 keyboard._logically_pressed_keys.clear()95 keyboard._hotkeys.clear()96 keyboard._listener.init()97 keyboard._word_listeners = {} 98 def do(self, manual_events, expected=None):99 input_events.extend(manual_events)100 while input_events:101 event = input_events.pop(0)102 if keyboard._listener.direct_callback(event):103 output_events.append(event)104 if expected is not None:105 to_names = lambda es: '+'.join(('d' if e.event_type == KEY_DOWN else 'u') + '_' + str(e.scan_code) for e in es)106 self.assertEqual(to_names(output_events), to_names(expected))107 del output_events[:]108 keyboard._listener.queue.join()109 def test_event_json(self):110 event = make_event(KEY_DOWN, u'á \'"', 999)111 import json112 self.assertEqual(event, KeyboardEvent(**json.loads(event.to_json())))113 def test_is_modifier_name(self):114 for name in keyboard.all_modifiers:115 self.assertTrue(keyboard.is_modifier(name))116 def test_is_modifier_scan_code(self):117 for i in range(10):118 self.assertEqual(keyboard.is_modifier(i), i in [4, 5, 6, 7])119 def test_key_to_scan_codes_brute(self):120 for name, entries in dummy_keys.items():121 if name in ['none', 'duplicated']: continue122 expected = tuple(scan_code for scan_code, modifiers in entries)123 self.assertEqual(keyboard.key_to_scan_codes(name), expected)124 def test_key_to_scan_code_from_scan_code(self):125 for i in range(10):126 self.assertEqual(keyboard.key_to_scan_codes(i), (i,))127 def test_key_to_scan_code_from_letter(self):128 self.assertEqual(keyboard.key_to_scan_codes('a'), (1,))129 self.assertEqual(keyboard.key_to_scan_codes('A'), (1,-1))130 def test_key_to_scan_code_from_normalized(self):131 self.assertEqual(keyboard.key_to_scan_codes('shift'), (5,6))132 self.assertEqual(keyboard.key_to_scan_codes('SHIFT'), (5,6))133 self.assertEqual(keyboard.key_to_scan_codes('ctrl'), keyboard.key_to_scan_codes('CONTROL'))134 def test_key_to_scan_code_from_sided_modifier(self):135 self.assertEqual(keyboard.key_to_scan_codes('left shift'), (5,))136 self.assertEqual(keyboard.key_to_scan_codes('right shift'), (6,))137 def test_key_to_scan_code_underscores(self):138 self.assertEqual(keyboard.key_to_scan_codes('_'), (12,))139 self.assertEqual(keyboard.key_to_scan_codes('right_shift'), (6,))140 def test_key_to_scan_code_error_none(self):141 with self.assertRaises(ValueError):142 keyboard.key_to_scan_codes(None)143 def test_key_to_scan_code_error_empty(self):144 with self.assertRaises(ValueError):145 keyboard.key_to_scan_codes('')146 def test_key_to_scan_code_error_other(self):147 with self.assertRaises(ValueError):148 keyboard.key_to_scan_codes({})149 def test_key_to_scan_code_list(self):150 self.assertEqual(keyboard.key_to_scan_codes([10, 5, 'a']), (10, 5, 1))151 def test_key_to_scan_code_empty(self):152 with self.assertRaises(ValueError):153 keyboard.key_to_scan_codes('none')154 def test_key_to_scan_code_duplicated(self):155 self.assertEqual(keyboard.key_to_scan_codes('duplicated'), (20,))156 def test_parse_hotkey_simple(self):157 self.assertEqual(keyboard.parse_hotkey('a'), (((1,),),))158 self.assertEqual(keyboard.parse_hotkey('A'), (((1,-1),),))159 def test_parse_hotkey_separators(self):160 self.assertEqual(keyboard.parse_hotkey('+'), keyboard.parse_hotkey('plus'))161 self.assertEqual(keyboard.parse_hotkey(','), keyboard.parse_hotkey('comma'))162 def test_parse_hotkey_keys(self):163 self.assertEqual(keyboard.parse_hotkey('left shift + a'), (((5,), (1,),),))164 self.assertEqual(keyboard.parse_hotkey('left shift+a'), (((5,), (1,),),))165 def test_parse_hotkey_simple_steps(self):166 self.assertEqual(keyboard.parse_hotkey('a,b'), (((1,),),((2,),)))167 self.assertEqual(keyboard.parse_hotkey('a, b'), (((1,),),((2,),)))168 def test_parse_hotkey_steps(self):169 self.assertEqual(keyboard.parse_hotkey('a+b, b+c'), (((1,),(2,)),((2,),(3,))))170 def test_parse_hotkey_example(self):171 alt_codes = keyboard.key_to_scan_codes('alt')172 shift_codes = keyboard.key_to_scan_codes('shift')173 a_codes = keyboard.key_to_scan_codes('a')174 b_codes = keyboard.key_to_scan_codes('b')175 c_codes = keyboard.key_to_scan_codes('c')176 self.assertEqual(keyboard.parse_hotkey("alt+shift+a, alt+b, c"), ((alt_codes, shift_codes, a_codes), (alt_codes, b_codes), (c_codes,)))177 def test_parse_hotkey_list_scan_codes(self):178 self.assertEqual(keyboard.parse_hotkey([1, 2, 3]), (((1,), (2,), (3,)),))179 def test_parse_hotkey_deep_list_scan_codes(self):180 result = keyboard.parse_hotkey('a')181 self.assertEqual(keyboard.parse_hotkey(result), (((1,),),))182 def test_parse_hotkey_list_names(self):183 self.assertEqual(keyboard.parse_hotkey(['a', 'b', 'c']), (((1,), (2,), (3,)),))184 def test_is_pressed_none(self):185 self.assertFalse(keyboard.is_pressed('a'))186 def test_is_pressed_true(self):187 self.do(d_a)188 self.assertTrue(keyboard.is_pressed('a'))189 def test_is_pressed_true_scan_code_true(self):190 self.do(d_a)191 self.assertTrue(keyboard.is_pressed(1))192 def test_is_pressed_true_scan_code_false(self):193 self.do(d_a)194 self.assertFalse(keyboard.is_pressed(2))195 def test_is_pressed_true_scan_code_invalid(self):196 self.do(d_a)197 self.assertFalse(keyboard.is_pressed(-1))198 def test_is_pressed_false(self):199 self.do(d_a+u_a+d_b)200 self.assertFalse(keyboard.is_pressed('a'))201 self.assertTrue(keyboard.is_pressed('b'))202 def test_is_pressed_hotkey_true(self):203 self.do(d_shift+d_a)204 self.assertTrue(keyboard.is_pressed('shift+a'))205 def test_is_pressed_hotkey_false(self):206 self.do(d_shift+d_a+u_a)207 self.assertFalse(keyboard.is_pressed('shift+a'))208 def test_is_pressed_multi_step_fail(self):209 self.do(u_a+d_a)210 with self.assertRaises(ValueError):211 keyboard.is_pressed('a, b')212 def test_send_single_press_release(self):213 keyboard.send('a', do_press=True, do_release=True)214 self.do([], d_a+u_a)215 def test_send_single_press(self):216 keyboard.send('a', do_press=True, do_release=False)217 self.do([], d_a)218 def test_send_single_release(self):219 keyboard.send('a', do_press=False, do_release=True)220 self.do([], u_a)221 def test_send_single_none(self):222 keyboard.send('a', do_press=False, do_release=False)223 self.do([], [])224 def test_press(self):225 keyboard.press('a')226 self.do([], d_a)227 def test_release(self):228 keyboard.release('a')229 self.do([], u_a)230 def test_press_and_release(self):231 keyboard.press_and_release('a')232 self.do([], d_a+u_a)233 def test_send_modifier_press_release(self):234 keyboard.send('ctrl+a', do_press=True, do_release=True)235 self.do([], d_ctrl+d_a+u_a+u_ctrl)236 def test_send_modifiers_release(self):237 keyboard.send('ctrl+shift+a', do_press=False, do_release=True)238 self.do([], u_a+u_shift+u_ctrl)239 def test_call_later(self):240 triggered = []241 def fn(arg1, arg2):242 assert arg1 == 1 and arg2 == 2243 triggered.append(True)244 keyboard.call_later(fn, (1, 2), 0.01)245 self.assertFalse(triggered)246 time.sleep(0.05)247 self.assertTrue(triggered)248 def test_hook_nonblocking(self):249 self.i = 0250 def count(e):251 self.assertEqual(e.name, 'a')252 self.i += 1253 hook = keyboard.hook(count, suppress=False)254 self.do(d_a+u_a, d_a+u_a)255 self.assertEqual(self.i, 2)256 keyboard.unhook(hook)257 self.do(d_a+u_a, d_a+u_a)258 self.assertEqual(self.i, 2)259 keyboard.hook(count, suppress=False)260 self.do(d_a+u_a, d_a+u_a)261 self.assertEqual(self.i, 4)262 keyboard.unhook_all()263 self.do(d_a+u_a, d_a+u_a)264 self.assertEqual(self.i, 4)265 def test_hook_blocking(self):266 self.i = 0267 def count(e):268 self.assertIn(e.name, ['a', 'b'])269 self.i += 1270 return e.name == 'b'271 hook = keyboard.hook(count, suppress=True)272 self.do(d_a+d_b, d_b)273 self.assertEqual(self.i, 2)274 keyboard.unhook(hook)275 self.do(d_a+d_b, d_a+d_b)276 self.assertEqual(self.i, 2)277 keyboard.hook(count, suppress=True)278 self.do(d_a+d_b, d_b)279 self.assertEqual(self.i, 4)280 keyboard.unhook_all()281 self.do(d_a+d_b, d_a+d_b)282 self.assertEqual(self.i, 4)283 def test_on_press_nonblocking(self):284 keyboard.on_press(lambda e: self.assertEqual(e.name, 'a') and self.assertEqual(e.event_type, KEY_DOWN))285 self.do(d_a+u_a)286 def test_on_press_blocking(self):287 keyboard.on_press(lambda e: e.scan_code == 1, suppress=True)288 self.do([make_event(KEY_DOWN, 'A', -1)] + d_a, d_a)289 def test_on_release(self):290 keyboard.on_release(lambda e: self.assertEqual(e.name, 'a') and self.assertEqual(e.event_type, KEY_UP))291 self.do(d_a+u_a)292 def test_hook_key_invalid(self):293 with self.assertRaises(ValueError):294 keyboard.hook_key('invalid', lambda e: None)295 def test_hook_key_nonblocking(self):296 self.i = 0297 def count(event):298 self.i += 1299 hook = keyboard.hook_key('A', count)300 self.do(d_a)301 self.assertEqual(self.i, 1)302 self.do(u_a+d_b)303 self.assertEqual(self.i, 2)304 self.do([make_event(KEY_DOWN, 'A', -1)])305 self.assertEqual(self.i, 3)306 keyboard.unhook_key(hook)307 self.do(d_a)308 self.assertEqual(self.i, 3)309 def test_hook_key_blocking(self):310 self.i = 0311 def count(event):312 self.i += 1313 return event.scan_code == 1314 hook = keyboard.hook_key('A', count, suppress=True)315 self.do(d_a, d_a)316 self.assertEqual(self.i, 1)317 self.do(u_a+d_b, u_a+d_b)318 self.assertEqual(self.i, 2)319 self.do([make_event(KEY_DOWN, 'A', -1)], [])320 self.assertEqual(self.i, 3)321 keyboard.unhook_key(hook)322 self.do([make_event(KEY_DOWN, 'A', -1)], [make_event(KEY_DOWN, 'A', -1)])323 self.assertEqual(self.i, 3)324 def test_on_press_key_nonblocking(self):325 keyboard.on_press_key('A', lambda e: self.assertEqual(e.name, 'a') and self.assertEqual(e.event_type, KEY_DOWN))326 self.do(d_a+u_a+d_b+u_b)327 def test_on_press_key_blocking(self):328 keyboard.on_press_key('A', lambda e: e.scan_code == 1, suppress=True)329 self.do([make_event(KEY_DOWN, 'A', -1)] + d_a, d_a)330 def test_on_release_key(self):331 keyboard.on_release_key('a', lambda e: self.assertEqual(e.name, 'a') and self.assertEqual(e.event_type, KEY_UP))332 self.do(d_a+u_a)333 def test_block_key(self):334 blocked = keyboard.block_key('a')335 self.do(d_a+d_b, d_b)336 self.do([make_event(KEY_DOWN, 'A', -1)], [make_event(KEY_DOWN, 'A', -1)])337 keyboard.unblock_key(blocked)338 self.do(d_a+d_b, d_a+d_b)339 def test_block_key_ambiguous(self):340 keyboard.block_key('A')341 self.do(d_a+d_b, d_b)342 self.do([make_event(KEY_DOWN, 'A', -1)], [])343 def test_remap_key_simple(self):344 mapped = keyboard.remap_key('a', 'b')345 self.do(d_a+d_c+u_a, d_b+d_c+u_b)346 keyboard.unremap_key(mapped)347 self.do(d_a+d_c+u_a, d_a+d_c+u_a)348 def test_remap_key_ambiguous(self):349 keyboard.remap_key('A', 'b')350 self.do(d_a+d_b, d_b+d_b)351 self.do([make_event(KEY_DOWN, 'A', -1)], d_b)352 def test_remap_key_multiple(self):353 mapped = keyboard.remap_key('a', 'shift+b')354 self.do(d_a+d_c+u_a, d_shift+d_b+d_c+u_b+u_shift)355 keyboard.unremap_key(mapped)356 self.do(d_a+d_c+u_a, d_a+d_c+u_a)357 def test_stash_state(self):358 self.do(d_a+d_shift)359 self.assertEqual(sorted(keyboard.stash_state()), [1, 5])360 self.do([], u_a+u_shift)361 def test_restore_state(self):362 self.do(d_b)363 keyboard.restore_state([1, 5])364 self.do([], u_b+d_a+d_shift)365 def test_restore_modifieres(self):366 self.do(d_b)367 keyboard.restore_modifiers([1, 5])368 self.do([], u_b+d_shift)369 def test_write_simple(self):370 keyboard.write('a', exact=False)371 self.do([], d_a+u_a)372 def test_write_multiple(self):373 keyboard.write('ab', exact=False)374 self.do([], d_a+u_a+d_b+u_b)375 def test_write_modifiers(self):376 keyboard.write('Ab', exact=False)377 self.do([], d_shift+d_a+u_a+u_shift+d_b+u_b)378 # restore_state_after has been removed after the introduction of `restore_modifiers`.379 #def test_write_stash_not_restore(self):380 # self.do(d_shift)381 # keyboard.write('a', restore_state_after=False, exact=False)382 # self.do([], u_shift+d_a+u_a)383 def test_write_stash_restore(self):384 self.do(d_shift)385 keyboard.write('a', exact=False)386 self.do([], u_shift+d_a+u_a+d_shift)387 def test_write_multiple(self):388 last_time = time.time()389 keyboard.write('ab', delay=0.01, exact=False)390 self.do([], d_a+u_a+d_b+u_b)391 self.assertGreater(time.time() - last_time, 0.015)392 def test_write_unicode_explicit(self):393 keyboard.write('ab', exact=True)394 self.do([], [KeyboardEvent(event_type=KEY_DOWN, scan_code=999, name='a'), KeyboardEvent(event_type=KEY_DOWN, scan_code=999, name='b')])395 def test_write_unicode_fallback(self):396 keyboard.write(u'áb', exact=False)397 self.do([], [KeyboardEvent(event_type=KEY_DOWN, scan_code=999, name=u'á')]+d_b+u_b)398 def test_start_stop_recording(self):399 keyboard.start_recording()400 self.do(d_a+u_a)401 self.assertEqual(keyboard.stop_recording(), d_a+u_a)402 def test_stop_recording_error(self):403 with self.assertRaises(ValueError):404 keyboard.stop_recording()405 def test_record(self):406 queue = keyboard._queue.Queue()407 def process():408 queue.put(keyboard.record('space', suppress=True))409 from threading import Thread410 t = Thread(target=process)411 t.daemon = True412 t.start()413 # 0.01s sleep failed once already. Better solutions?414 time.sleep(0.01)415 self.do(du_a+du_b+du_space, du_a+du_b)416 self.assertEqual(queue.get(timeout=0.5), du_a+du_b+du_space)417 def test_play_nodelay(self):418 keyboard.play(d_a+u_a, 0)419 self.do([], d_a+u_a)420 def test_play_stash(self):421 self.do(d_ctrl)422 keyboard.play(d_a+u_a, 0)423 self.do([], u_ctrl+d_a+u_a+d_ctrl)424 def test_play_delay(self):425 last_time = time.time()426 events = [make_event(KEY_DOWN, 'a', 1, 100), make_event(KEY_UP, 'a', 1, 100.01)]427 keyboard.play(events, 1)428 self.do([], d_a+u_a)429 self.assertGreater(time.time() - last_time, 0.005)430 def test_get_typed_strings_simple(self):431 events = du_a+du_b+du_backspace+d_shift+du_a+u_shift+du_space+du_ctrl+du_a432 self.assertEqual(list(keyboard.get_typed_strings(events)), ['aA ', 'a'])433 def test_get_typed_strings_backspace(self):434 events = du_a+du_b+du_backspace435 self.assertEqual(list(keyboard.get_typed_strings(events)), ['a'])436 events = du_backspace+du_a+du_b437 self.assertEqual(list(keyboard.get_typed_strings(events)), ['ab'])438 def test_get_typed_strings_shift(self):439 events = d_shift+du_a+du_b+u_shift+du_space+du_ctrl+du_a440 self.assertEqual(list(keyboard.get_typed_strings(events)), ['AB ', 'a'])441 def test_get_typed_strings_all(self):442 events = du_a+du_b+du_backspace+d_shift+du_a+du_capslock+du_b+u_shift+du_space+du_ctrl+du_a443 self.assertEqual(list(keyboard.get_typed_strings(events)), ['aAb ', 'A'])444 def test_get_hotkey_name_simple(self):445 self.assertEqual(keyboard.get_hotkey_name(['a']), 'a')446 def test_get_hotkey_name_modifiers(self):447 self.assertEqual(keyboard.get_hotkey_name(['a', 'shift', 'ctrl']), 'ctrl+shift+a')448 def test_get_hotkey_name_normalize(self):449 self.assertEqual(keyboard.get_hotkey_name(['SHIFT', 'left ctrl']), 'ctrl+shift')450 def test_get_hotkey_name_plus(self):451 self.assertEqual(keyboard.get_hotkey_name(['+']), 'plus')452 def test_get_hotkey_name_duplicated(self):453 self.assertEqual(keyboard.get_hotkey_name(['+', 'plus']), 'plus')454 def test_get_hotkey_name_full(self):455 self.assertEqual(keyboard.get_hotkey_name(['+', 'left ctrl', 'shift', 'WIN', 'right alt']), 'ctrl+alt+shift+windows+plus')456 def test_get_hotkey_name_multiple(self):457 self.assertEqual(keyboard.get_hotkey_name(['ctrl', 'b', '!', 'a']), 'ctrl+!+a+b')458 def test_get_hotkey_name_from_pressed(self):459 self.do(du_c+d_ctrl+d_a+d_b)460 self.assertEqual(keyboard.get_hotkey_name(), 'ctrl+a+b')461 def test_read_hotkey(self):462 queue = keyboard._queue.Queue()463 def process():464 queue.put(keyboard.read_hotkey())465 from threading import Thread466 t = Thread(target=process)467 t.daemon = True468 t.start()469 time.sleep(0.01)470 self.do(d_ctrl+d_a+d_b+u_ctrl)471 self.assertEqual(queue.get(timeout=0.5), 'ctrl+a+b')472 def test_read_event(self):473 queue = keyboard._queue.Queue()474 def process():475 queue.put(keyboard.read_event(suppress=True))476 from threading import Thread477 t = Thread(target=process)478 t.daemon = True479 t.start()480 time.sleep(0.01)481 self.do(d_a, [])482 self.assertEqual(queue.get(timeout=0.5), d_a[0])483 def test_read_key(self):484 queue = keyboard._queue.Queue()485 def process():486 queue.put(keyboard.read_key(suppress=True))487 from threading import Thread488 t = Thread(target=process)489 t.daemon = True490 t.start()491 time.sleep(0.01)492 self.do(d_a, [])493 self.assertEqual(queue.get(timeout=0.5), 'a')494 def test_wait_infinite(self):495 self.triggered = False496 def process():497 keyboard.wait()498 self.triggered = True499 from threading import Thread500 t = Thread(target=process)501 t.daemon = True # Yep, we are letting this thread loose.502 t.start()503 time.sleep(0.01)504 self.assertFalse(self.triggered)505 def test_wait_until_success(self):506 queue = keyboard._queue.Queue()507 def process():508 queue.put(keyboard.wait(queue.get(timeout=0.5), suppress=True) or True)509 from threading import Thread510 t = Thread(target=process)511 t.daemon = True512 t.start()513 queue.put('a')514 time.sleep(0.01)515 self.do(d_a, [])516 self.assertTrue(queue.get(timeout=0.5))517 def test_wait_until_fail(self):518 def process():519 keyboard.wait('a', suppress=True)520 self.fail()521 from threading import Thread522 t = Thread(target=process)523 t.daemon = True # Yep, we are letting this thread loose.524 t.start()525 time.sleep(0.01)526 self.do(d_b)527 def test_add_hotkey_single_step_suppress_allow(self):528 keyboard.add_hotkey('a', lambda: trigger() or True, suppress=True)529 self.do(d_a, triggered_event+d_a)530 def test_add_hotkey_single_step_suppress_args_allow(self):531 arg = object()532 keyboard.add_hotkey('a', lambda a: self.assertIs(a, arg) or trigger() or True, args=(arg,), suppress=True)533 self.do(d_a, triggered_event+d_a)534 def test_add_hotkey_single_step_suppress_single(self):535 keyboard.add_hotkey('a', trigger, suppress=True)536 self.do(d_a, triggered_event)537 def test_add_hotkey_single_step_suppress_removed(self):538 keyboard.remove_hotkey(keyboard.add_hotkey('a', trigger, suppress=True))539 self.do(d_a, d_a)540 def test_add_hotkey_single_step_suppress_removed(self):541 keyboard.remove_hotkey(keyboard.add_hotkey('ctrl+a', trigger, suppress=True))542 self.do(d_ctrl+d_a, d_ctrl+d_a)543 self.assertEqual(keyboard._listener.filtered_modifiers[dummy_keys['left ctrl'][0][0]], 0)544 def test_remove_hotkey_internal(self):545 remove = keyboard.add_hotkey('shift+a', trigger, suppress=True)546 self.assertTrue(all(keyboard._listener.blocking_hotkeys.values()))547 self.assertTrue(all(keyboard._listener.filtered_modifiers.values()))548 self.assertNotEqual(keyboard._hotkeys, {})549 remove()550 self.assertTrue(not any(keyboard._listener.filtered_modifiers.values()))551 self.assertTrue(not any(keyboard._listener.blocking_hotkeys.values()))552 self.assertEqual(keyboard._hotkeys, {})553 def test_remove_hotkey_internal_multistep_start(self):554 remove = keyboard.add_hotkey('shift+a, b', trigger, suppress=True)555 self.assertTrue(all(keyboard._listener.blocking_hotkeys.values()))556 self.assertTrue(all(keyboard._listener.filtered_modifiers.values()))557 self.assertNotEqual(keyboard._hotkeys, {})558 remove()559 self.assertTrue(not any(keyboard._listener.filtered_modifiers.values()))560 self.assertTrue(not any(keyboard._listener.blocking_hotkeys.values()))561 self.assertEqual(keyboard._hotkeys, {})562 def test_remove_hotkey_internal_multistep_end(self):563 remove = keyboard.add_hotkey('shift+a, b', trigger, suppress=True)564 self.do(d_shift+du_a+u_shift)565 self.assertTrue(any(keyboard._listener.blocking_hotkeys.values()))566 self.assertTrue(not any(keyboard._listener.filtered_modifiers.values()))567 self.assertNotEqual(keyboard._hotkeys, {})568 remove()569 self.assertTrue(not any(keyboard._listener.filtered_modifiers.values()))570 self.assertTrue(not any(keyboard._listener.blocking_hotkeys.values()))571 self.assertEqual(keyboard._hotkeys, {})572 def test_add_hotkey_single_step_suppress_with_modifiers(self):573 keyboard.add_hotkey('ctrl+shift+a', trigger, suppress=True)574 self.do(d_ctrl+d_shift+d_a, triggered_event)575 def test_add_hotkey_single_step_suppress_with_modifiers_fail_unrelated_modifier(self):576 keyboard.add_hotkey('ctrl+shift+a', trigger, suppress=True)577 self.do(d_ctrl+d_shift+u_shift+d_a, d_shift+u_shift+d_ctrl+d_a)578 def test_add_hotkey_single_step_suppress_with_modifiers_fail_unrelated_key(self):579 keyboard.add_hotkey('ctrl+shift+a', trigger, suppress=True)580 self.do(d_ctrl+d_shift+du_b, d_shift+d_ctrl+du_b)581 def test_add_hotkey_single_step_suppress_with_modifiers_unrelated_key(self):582 keyboard.add_hotkey('ctrl+shift+a', trigger, suppress=True)583 self.do(d_ctrl+d_shift+du_b+d_a, d_shift+d_ctrl+du_b+triggered_event)584 def test_add_hotkey_single_step_suppress_with_modifiers_release(self):585 keyboard.add_hotkey('ctrl+shift+a', trigger, suppress=True)586 self.do(d_ctrl+d_shift+du_b+d_a+u_ctrl+u_shift, d_shift+d_ctrl+du_b+triggered_event+u_ctrl+u_shift)587 def test_add_hotkey_single_step_suppress_with_modifiers_out_of_order(self):588 keyboard.add_hotkey('ctrl+shift+a', trigger, suppress=True)589 self.do(d_shift+d_ctrl+d_a, triggered_event)590 def test_add_hotkey_single_step_suppress_with_modifiers_repeated(self):591 keyboard.add_hotkey('ctrl+a', trigger, suppress=True)592 self.do(d_ctrl+du_a+du_b+du_a, triggered_event+d_ctrl+du_b+triggered_event)593 def test_add_hotkey_single_step_suppress_with_modifiers_release(self):594 keyboard.add_hotkey('ctrl+a', trigger, suppress=True, trigger_on_release=True)595 self.do(d_ctrl+du_a+du_b+du_a, triggered_event+d_ctrl+du_b+triggered_event)596 def test_add_hotkey_single_step_suppress_with_modifier_superset_release(self):597 keyboard.add_hotkey('ctrl+a', trigger, suppress=True, trigger_on_release=True)598 self.do(d_ctrl+d_shift+du_a+u_shift+u_ctrl, d_ctrl+d_shift+du_a+u_shift+u_ctrl)599 def test_add_hotkey_single_step_suppress_with_modifier_superset(self):600 keyboard.add_hotkey('ctrl+a', trigger, suppress=True)601 self.do(d_ctrl+d_shift+du_a+u_shift+u_ctrl, d_ctrl+d_shift+du_a+u_shift+u_ctrl)602 def test_add_hotkey_single_step_timeout(self):603 keyboard.add_hotkey('a', trigger, timeout=1, suppress=True)604 self.do(du_a, triggered_event)605 def test_add_hotkey_multi_step_first_timeout(self):606 keyboard.add_hotkey('a, b', trigger, timeout=0.01, suppress=True)607 time.sleep(0.03)608 self.do(du_a+du_b, triggered_event)609 def test_add_hotkey_multi_step_last_timeout(self):610 keyboard.add_hotkey('a, b', trigger, timeout=0.01, suppress=True)611 self.do(du_a, [])612 time.sleep(0.05)613 self.do(du_b, du_a+du_b)614 def test_add_hotkey_multi_step_success_timeout(self):615 keyboard.add_hotkey('a, b', trigger, timeout=0.05, suppress=True)616 self.do(du_a, [])617 time.sleep(0.01)618 self.do(du_b, triggered_event)619 def test_add_hotkey_multi_step_suffix_timeout(self):620 keyboard.add_hotkey('a, b, a', trigger, timeout=0.01, suppress=True)621 self.do(du_a+du_b, [])622 time.sleep(0.05)623 self.do(du_a, du_a+du_b)624 self.do(du_b+du_a, triggered_event)625 def test_add_hotkey_multi_step_allow(self):626 keyboard.add_hotkey('a, b', lambda: trigger() or True, suppress=True)627 self.do(du_a+du_b, triggered_event+du_a+du_b)628 def test_add_hotkey_single_step_nonsuppress(self):629 queue = keyboard._queue.Queue()630 keyboard.add_hotkey('ctrl+shift+a+b', lambda: queue.put(True), suppress=False)631 self.do(d_shift+d_ctrl+d_a+d_b)632 self.assertTrue(queue.get(timeout=0.5))633 def test_add_hotkey_single_step_nonsuppress_repeated(self):634 queue = keyboard._queue.Queue()635 keyboard.add_hotkey('ctrl+shift+a+b', lambda: queue.put(True), suppress=False)636 self.do(d_shift+d_ctrl+d_a+d_b)637 self.do(d_shift+d_ctrl+d_a+d_b)638 self.assertTrue(queue.get(timeout=0.5))639 self.assertTrue(queue.get(timeout=0.5))640 def test_add_hotkey_single_step_nosuppress_with_modifiers_out_of_order(self):641 queue = keyboard._queue.Queue()642 keyboard.add_hotkey('ctrl+shift+a', lambda: queue.put(True), suppress=False)643 self.do(d_shift+d_ctrl+d_a)644 self.assertTrue(queue.get(timeout=0.5))645 def test_add_hotkey_single_step_suppress_regression_1(self):646 keyboard.add_hotkey('a', trigger, suppress=True)647 self.do(d_c+d_a+u_c+u_a, d_c+d_a+u_c+u_a)648 def test_remap_hotkey_single(self):649 keyboard.remap_hotkey('a', 'b')650 self.do(d_a+u_a, d_b+u_b)651 def test_remap_hotkey_complex_dst(self):652 keyboard.remap_hotkey('a', 'ctrl+b, c')653 self.do(d_a+u_a, d_ctrl+du_b+u_ctrl+du_c)654 def test_remap_hotkey_modifiers(self):655 keyboard.remap_hotkey('ctrl+shift+a', 'b')656 self.do(d_ctrl+d_shift+d_a+u_a, du_b)657 def test_remap_hotkey_modifiers_repeat(self):658 keyboard.remap_hotkey('ctrl+shift+a', 'b')659 self.do(d_ctrl+d_shift+du_a+du_a, du_b+du_b)660 def test_remap_hotkey_modifiers_state(self):661 keyboard.remap_hotkey('ctrl+shift+a', 'b')662 self.do(d_ctrl+d_shift+du_c+du_a+du_a, d_shift+d_ctrl+du_c+u_shift+u_ctrl+du_b+d_ctrl+d_shift+u_shift+u_ctrl+du_b+d_ctrl+d_shift)663 def test_remap_hotkey_release_incomplete(self):664 keyboard.remap_hotkey('a', 'b', trigger_on_release=True)665 self.do(d_a, [])666 def test_remap_hotkey_release_complete(self):667 keyboard.remap_hotkey('a', 'b', trigger_on_release=True)668 self.do(du_a, du_b)669 def test_parse_hotkey_combinations_scan_code(self):670 self.assertEqual(keyboard.parse_hotkey_combinations(30), (((30,),),))671 def test_parse_hotkey_combinations_single(self):672 self.assertEqual(keyboard.parse_hotkey_combinations('a'), (((1,),),))673 def test_parse_hotkey_combinations_single_modifier(self):674 self.assertEqual(keyboard.parse_hotkey_combinations('shift+a'), (((1, 5), (1, 6)),))675 def test_parse_hotkey_combinations_single_modifiers(self):676 self.assertEqual(keyboard.parse_hotkey_combinations('shift+ctrl+a'), (((1, 5, 7), (1, 6, 7)),))677 def test_parse_hotkey_combinations_multi(self):678 self.assertEqual(keyboard.parse_hotkey_combinations('a, b'), (((1,),), ((2,),)))679 def test_parse_hotkey_combinations_multi_modifier(self):680 self.assertEqual(keyboard.parse_hotkey_combinations('shift+a, b'), (((1, 5), (1, 6)), ((2,),)))681 def test_parse_hotkey_combinations_list_list(self):682 self.assertEqual(keyboard.parse_hotkey_combinations(keyboard.parse_hotkey_combinations('a, b')), keyboard.parse_hotkey_combinations('a, b'))683 def test_parse_hotkey_combinations_fail_empty(self):684 with self.assertRaises(ValueError):685 keyboard.parse_hotkey_combinations('')686 def test_add_hotkey_multistep_suppress_incomplete(self):687 keyboard.add_hotkey('a, b', trigger, suppress=True)688 self.do(du_a, [])689 self.assertEqual(keyboard._listener.blocking_hotkeys[(1,)], [])690 self.assertEqual(len(keyboard._listener.blocking_hotkeys[(2,)]), 1)691 def test_add_hotkey_multistep_suppress_incomplete(self):692 keyboard.add_hotkey('a, b', trigger, suppress=True)693 self.do(du_a+du_b, triggered_event)694 def test_add_hotkey_multistep_suppress_modifier(self):695 keyboard.add_hotkey('shift+a, b', trigger, suppress=True)696 self.do(d_shift+du_a+u_shift+du_b, triggered_event)697 def test_add_hotkey_multistep_suppress_fail(self):698 keyboard.add_hotkey('a, b', trigger, suppress=True)699 self.do(du_a+du_c, du_a+du_c)700 def test_add_hotkey_multistep_suppress_three_steps(self):701 keyboard.add_hotkey('a, b, c', trigger, suppress=True)702 self.do(du_a+du_b+du_c, triggered_event)703 def test_add_hotkey_multistep_suppress_repeated_prefix(self):704 keyboard.add_hotkey('a, a, c', trigger, suppress=True, trigger_on_release=True)705 self.do(du_a+du_a+du_c, triggered_event)706 def test_add_hotkey_multistep_suppress_repeated_key(self):707 keyboard.add_hotkey('a, b', trigger, suppress=True)708 self.do(du_a+du_a+du_b, du_a+triggered_event)709 self.assertEqual(keyboard._listener.blocking_hotkeys[(2,)], [])710 self.assertEqual(len(keyboard._listener.blocking_hotkeys[(1,)]), 1)711 def test_add_hotkey_multi_step_suppress_regression_1(self):712 keyboard.add_hotkey('a, b', trigger, suppress=True)713 self.do(d_c+d_a+u_c+u_a+du_c, d_c+d_a+u_c+u_a+du_c)714 def test_add_hotkey_multi_step_suppress_replays(self):715 keyboard.add_hotkey('a, b, c', trigger, suppress=True)716 self.do(du_a+du_b+du_a+du_b+du_space, du_a+du_b+du_a+du_b+du_space)717 def test_add_word_listener_success(self):718 queue = keyboard._queue.Queue()719 def free():720 queue.put(1)721 keyboard.add_word_listener('abc', free)722 self.do(du_a+du_b+du_c+du_space)723 self.assertTrue(queue.get(timeout=0.5))724 def test_add_word_listener_no_trigger_fail(self):725 queue = keyboard._queue.Queue()726 def free():727 queue.put(1)728 keyboard.add_word_listener('abc', free)729 self.do(du_a+du_b+du_c)730 with self.assertRaises(keyboard._queue.Empty):731 queue.get(timeout=0.01)732 def test_add_word_listener_timeout_fail(self):733 queue = keyboard._queue.Queue()734 def free():735 queue.put(1)736 keyboard.add_word_listener('abc', free, timeout=1)737 self.do(du_a+du_b+du_c+[make_event(KEY_DOWN, name='space', time=2)])738 with self.assertRaises(keyboard._queue.Empty):739 queue.get(timeout=0.01)740 def test_duplicated_word_listener(self):741 keyboard.add_word_listener('abc', trigger)742 keyboard.add_word_listener('abc', trigger)743 def test_add_word_listener_remove(self):744 queue = keyboard._queue.Queue()745 def free():746 queue.put(1)747 keyboard.add_word_listener('abc', free)748 keyboard.remove_word_listener('abc')749 self.do(du_a+du_b+du_c+du_space)750 with self.assertRaises(keyboard._queue.Empty):751 queue.get(timeout=0.01)752 def test_add_word_listener_suffix_success(self):753 queue = keyboard._queue.Queue()754 def free():755 queue.put(1)756 keyboard.add_word_listener('abc', free, match_suffix=True)757 self.do(du_a+du_a+du_b+du_c+du_space)758 self.assertTrue(queue.get(timeout=0.5))759 def test_add_word_listener_suffix_fail(self):760 queue = keyboard._queue.Queue()761 def free():762 queue.put(1)763 keyboard.add_word_listener('abc', free)764 self.do(du_a+du_a+du_b+du_c)765 with self.assertRaises(keyboard._queue.Empty):766 queue.get(timeout=0.01)767 #def test_add_abbreviation(self):768 # keyboard.add_abbreviation('abc', 'aaa')769 # self.do(du_a+du_b+du_c+du_space, [])770if __name__ == '__main__':...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!