Best Python code snippet using keyboard
test_inputcontroller.py
Source:test_inputcontroller.py  
...30        # Try to invoke the handler31        test_event = Event(KEYDOWN, {32            'key': K_RETURN33        })34        controller.invoke_handlers(test_event)35        self.assertTrue(test_object.test_function_has_been_called)36    def test_invoke_handler_mousedown(self):37        controller = InputController()38        test_object = TestObject()39        controller.register_handler(InputAction.PRESS, Key.CONFIRM, test_object.test_function)40        self.assertTrue(len(controller.__subscribers__[(InputAction.PRESS, Key.CONFIRM)]) > 0)41        # Try to invoke the handler42        test_event = Event(MOUSEBUTTONDOWN, {43            'button': 144        })45        controller.invoke_handlers(test_event)46        self.assertTrue(test_object.test_function_has_been_called)47    def test_invoke_handler_mousemotion(self):48        controller = InputController()49        test_object = TestObject()50        controller.register_handler(InputAction.MOTION, None, test_object.test_function)51        self.assertTrue(len(controller.__subscribers__[(InputAction.MOTION, None)]) > 0)52        # Try to invoke the handler53        test_event = Event(MOUSEMOTION, {})54        controller.invoke_handlers(test_event)55        self.assertTrue(test_object.test_function_has_been_called)56    def test_invoke_handler_accepting_input(self):57        controller = InputController()58        test_object = TestObject()59        controller.register_handler(InputAction.PRESS, Key.CONFIRM, test_object.test_function)60        self.assertTrue(len(controller.__subscribers__[(InputAction.PRESS, Key.CONFIRM)]) > 0)61        # Try to invoke the handler62        test_event = Event(KEYDOWN, {63            'key': K_RETURN64        })65        controller.invoke_handlers(test_event)66        self.assertTrue(test_object.test_function_has_been_called)67        # Disable accepting input and try to invoke the handler68        test_object.test_function_has_been_called = False69        test_object.accepting_input = False70        # Try to invoke the handler again71        test_event = Event(KEYDOWN, {72            'key': K_RETURN73        })74        controller.invoke_handlers(test_event)75        self.assertFalse(test_object.test_function_has_been_called)76    def test_remove_handler(self):77        controller = InputController()78        test_object = TestObject()79        controller.register_handler(InputAction.PRESS, Key.CONFIRM, test_object.test_function)80        self.assertTrue(len(controller.__subscribers__[(InputAction.PRESS, Key.CONFIRM)]) > 0)81        controller.unregister_handlers(test_object)82        self.assertTrue(len(controller.__subscribers__[(InputAction.PRESS, Key.CONFIRM)]) == 0)83        # Try (and fail) to invoke the handler84        test_event = Event(KEYDOWN, {85            'key': K_RETURN86        })87        controller.invoke_handlers(test_event)...test_eventbus.py
Source:test_eventbus.py  
...27        # Try to invoke the handler28        test_event = Event(USEREVENT, {29            EVENT_TYPE: EventType.E_START_BATTLE30        })31        bus.invoke_handlers(test_event)32        self.assertTrue(test_object.test_function_has_been_called)33    def test_remove_handler(self):34        bus = EventBus()35        test_object = TestObject()36        bus.register_handler(EventType.E_START_BATTLE, test_object.test_function)37        self.assertTrue(len(bus.__subscribers__[EventType.E_START_BATTLE]) > 0)38        bus.unregister_handlers(test_object)39        self.assertTrue(len(bus.__subscribers__[EventType.E_START_BATTLE]) == 0)40        # Try (and fail) to invoke the handler41        test_event = Event(USEREVENT, {42            EVENT_TYPE: EventType.E_START_BATTLE43        })44        bus.invoke_handlers(test_event)45        self.assertFalse(test_object.test_function_has_been_called)46    def test_invocation_state(self):47        bus = EventBus()48        test_object = TestObject()49        # The test value should start at 050        self.assertTrue(test_object.test_value == 0)51        bus.register_handler(EventType.E_START_BATTLE, test_object.test_function2)52        # Modify the test value outside of the handler53        test_object.test_value = 254        # Invoke the handler55        test_event = Event(USEREVENT, {56            EVENT_TYPE: EventType.E_START_BATTLE57        })58        bus.invoke_handlers(test_event)59        # Assert that the callback method uses the current object and not a snapshot60        self.assertTrue(test_object.test_value == 3)61    def test_invoke_handler_accepting_input(self):62        bus = EventBus()63        test_object = TestObject()64        bus.register_handler(EventType.E_START_BATTLE, test_object.test_function2)65        # Try to invoke the handler with a local event66        test_event = Event(USEREVENT, {67            EVENT_TYPE: EventType.E_START_BATTLE68        })69        bus.invoke_handlers(test_event)70        self.assertTrue(test_object.test_value == 1)71        # Try (and fail) to invoke the handler after disabling accepting events72        test_object.accepts_events = False73        test_event = Event(USEREVENT, {74            EVENT_TYPE: EventType.E_START_BATTLE,75        })76        bus.invoke_handlers(test_event)77        self.assertTrue(test_object.test_value == 1)78    def test_local_only_handler(self):79        bus = EventBus()80        test_object = TestObject()81        bus.register_handler(EventType.E_START_BATTLE, test_object.test_function2, local_only=True)82        # Try to invoke the handler with a local event83        test_event = Event(USEREVENT, {84            EVENT_TYPE: EventType.E_START_BATTLE85        })86        bus.invoke_handlers(test_event)87        self.assertTrue(test_object.test_value == 1)88        # Try (and fail) to invoke the handler with a network event89        test_event = Event(USEREVENT, {90            EVENT_TYPE: EventType.E_START_BATTLE,91            FROM_NETWORK: True92        })93        bus.invoke_handlers(test_event)..._generic.py
Source:_generic.py  
...11    def __init__(self):12        self.handlers = []13        self.listening = False14        self.queue = Queue()15    def invoke_handlers(self, event):16        for handler in self.handlers:17            try:18                if handler(event):19                    # Stop processing this hotkey.20                    return 121            except Exception as e:22                traceback.print_exc()23    def start_if_necessary(self):24        """25        Starts the listening thread if it wans't already.26        """27        self.lock.acquire()28        try:29            if not self.listening:30                self.init()31                self.listening = True32                self.listening_thread = Thread(target=self.listen)33                self.listening_thread.daemon = True34                self.listening_thread.start()35                self.processing_thread = Thread(target=self.process)36                self.processing_thread.daemon = True37                self.processing_thread.start()38        finally:39            self.lock.release()40    def pre_process_event(self, event):41        raise NotImplementedError('This method should be implemented in the child class.')42    def process(self):43        """44        Loops over the underlying queue of events and processes them in order.45        """46        assert self.queue is not None47        while True:48            event = self.queue.get()49            if self.pre_process_event(event):50                self.invoke_handlers(event)51            self.queue.task_done()52            53    def add_handler(self, handler):54        """55        Adds a function to receive each event captured, starting the capturing56        process if necessary.57        """58        self.start_if_necessary()59        self.handlers.append(handler)60    def remove_handler(self, handler):61        """ Removes a previously added event handler. """62        while handler in self.handlers:...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!!
