class TestKeys(TestCase): def setUp(self): self.ahk = AHK() self.thread = None self.hotkey = None def tearDown(self): if self.thread is not None: self.thread.join(timeout=3) if self.ahk.key_state('a'): self.ahk.key_up('a') if self.ahk.key_state('Control'): self.ahk.key_up('Control') self.ahk.set_capslock_state('off') notepad = self.ahk.find_window(title=b'Untitled - Notepad') if notepad: notepad.close() if self.hotkey and self.hotkey.running: self.hotkey.stop() def test_key_wait_pressed(self): start = time.time() self.thread = threading.Thread(target=a_down) self.thread.start() self.ahk.key_wait('a', timeout=5) end = time.time() assert end - start < 5 def test_key_wait_released(self): start = time.time() a_down() self.thread = threading.Thread(target=release_a) self.thread.start() self.ahk.key_wait('a', timeout=2) def test_key_wait_timeout(self): self.assertRaises(TimeoutError, self.ahk.key_wait, 'f', timeout=1) def test_key_state_when_not_pressed(self): self.assertFalse(self.ahk.key_state('a')) def test_key_state_pressed(self): self.ahk.key_down('Control') self.assertTrue(self.ahk.key_state('Control')) def test_hotkey(self): self.hotkey = self.ahk.hotkey(hotkey='a', script='Run Notepad') self.thread = threading.Thread(target=a_down) self.thread.start() self.hotkey.start() time.sleep(1) self.assertIsNotNone(self.ahk.find_window(title=b'Untitled - Notepad')) def test_hotkey_stop(self): self.hotkey = self.ahk.hotkey(hotkey='a', script='Run Notepad') self.hotkey.start() assert self.hotkey.running self.hotkey.stop() self.ahk.key_press('a') self.assertIsNone(self.ahk.find_window(title=b'Untitled - Notepad'))
def main(): ahk = AHK() ahk.key_wait('p') print(ahk.mouse_position) num + 1 main()