Windows平台的热键管理实现
| 99 | |
| 100 | |
| 101 | class WindowsHotkeyManager(HotkeyManager): |
| 102 | """Windows平台的热键管理实现""" |
| 103 | |
| 104 | def register_hotkey(self, hotkey_str): |
| 105 | if not KEYBOARD_AVAILABLE: |
| 106 | return False |
| 107 | |
| 108 | try: |
| 109 | # 先取消已有的热键 |
| 110 | self.unregister_hotkey() |
| 111 | |
| 112 | # 注册新热键 |
| 113 | keyboard.add_hotkey(hotkey_str, self.callback) |
| 114 | self.current_hotkey = hotkey_str |
| 115 | self.is_active = True |
| 116 | return True |
| 117 | except Exception: |
| 118 | return False |
| 119 | |
| 120 | def unregister_hotkey(self, hotkey_str=None): |
| 121 | if not KEYBOARD_AVAILABLE: |
| 122 | return False |
| 123 | |
| 124 | try: |
| 125 | key_to_remove = hotkey_str or self.current_hotkey |
| 126 | if key_to_remove: |
| 127 | keyboard.remove_hotkey(key_to_remove) |
| 128 | if hotkey_str is None or hotkey_str == self.current_hotkey: |
| 129 | self.current_hotkey = None |
| 130 | self.is_active = False |
| 131 | return True |
| 132 | except Exception: |
| 133 | return False |
| 134 | |
| 135 | def register_screenshot_listener(self, hotkey_str, callback): |
| 136 | if not KEYBOARD_AVAILABLE: |
| 137 | return False |
| 138 | |
| 139 | try: |
| 140 | # 先取消已有的截图监听 |
| 141 | self.unregister_screenshot_listener() |
| 142 | |
| 143 | # 注册新的截图监听 |
| 144 | keyboard.add_hotkey(hotkey_str, callback) |
| 145 | self.screenshot_hotkey = hotkey_str |
| 146 | self.screenshot_callback = callback |
| 147 | self.screenshot_active = True |
| 148 | return True |
| 149 | except Exception: |
| 150 | return False |
| 151 | |
| 152 | def unregister_screenshot_listener(self): |
| 153 | if not KEYBOARD_AVAILABLE: |
| 154 | return False |
| 155 | |
| 156 | try: |
| 157 | if self.screenshot_hotkey: |
| 158 | keyboard.remove_hotkey(self.screenshot_hotkey) |
no outgoing calls
no test coverage detected