Repository navigation
feat: 支持在脚本链中开关隐私屏 #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
antecanis8
wants to merge
1
commit into
OneDragon-Anything:main
Choose a base branch
from
antecanis8:feat/privacy-screen
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+915
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
83 changes: 83 additions & 0 deletions
83
src/one_dragon/base/controller/pc_button/pc_button_listener.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| from collections.abc import Callable | ||
| from concurrent.futures import Future, ThreadPoolExecutor | ||
|
|
||
| from pynput import keyboard, mouse | ||
|
|
||
| from one_dragon.utils import thread_utils | ||
|
|
||
|
|
||
| class PcButtonListener: | ||
|
|
||
| def __init__(self, | ||
| on_button_tap: Callable[[str], None], | ||
| listen_keyboard: bool = False, | ||
| listen_mouse: bool = False, | ||
| listen_gamepad: bool = False, | ||
| ): | ||
| self.keyboard_listener = keyboard.Listener(on_press=self._on_keyboard_press) | ||
| self.mouse_listener = mouse.Listener(on_click=self._on_mouse_click) | ||
| self.gamepad_listener = None | ||
|
|
||
| self.on_button_tap: Callable[[str], None] = on_button_tap | ||
|
|
||
| self.listen_keyboard: bool = listen_keyboard | ||
| self.listen_mouse: bool = listen_mouse | ||
| self.listen_gamepad: bool = listen_gamepad | ||
|
|
||
| self._executor = ThreadPoolExecutor(thread_name_prefix='od_key_mouse_btn_listener', max_workers=8) | ||
|
|
||
| def _on_keyboard_press(self, event): | ||
| if isinstance(event, keyboard.Key): | ||
| k = event.name | ||
| elif isinstance(event, keyboard.KeyCode): | ||
| # 处理小键盘按键和特殊按键 | ||
| if event.char is not None: | ||
| k = event.char | ||
| elif hasattr(event, 'vk') and event.vk is not None: | ||
| # 使用虚拟键码来识别小键盘按键 | ||
| k = self._get_numpad_key_name(event.vk) | ||
| elif hasattr(event, 'vk'): | ||
| k = f'vk_{event.vk}' # vk 为 None 的情况 | ||
| else: | ||
| k = 'unknown' # 没有 vk 属性 | ||
| else: | ||
| return | ||
|
|
||
| # 确保按键名称不为空 | ||
| if k is None or k == '': | ||
| return | ||
|
|
||
| self._call_button_tap_callback(k) | ||
|
|
||
| def _get_numpad_key_name(self, vk: int) -> str: | ||
| """ | ||
| 根据虚拟键码获取小键盘按键名称 | ||
| :param vk: 虚拟键码 | ||
| :return: 按键名称 | ||
| """ | ||
| # 小键盘数字键: vk 96-105 对应 numpad_0 到 numpad_9 | ||
| if 96 <= vk <= 105: | ||
| return f'numpad_{vk - 96}' | ||
|
|
||
| # 其他按键返回通用格式 | ||
| return f'vk_{vk}' | ||
|
|
||
| def _on_mouse_click(self, x, y, button: mouse.Button, pressed): | ||
| if pressed == 1: | ||
| self._call_button_tap_callback('mouse_' + button.name) | ||
|
|
||
| def _call_button_tap_callback(self, key: str) -> None: | ||
| if self.on_button_tap is not None: | ||
| future: Future = self._executor.submit(self.on_button_tap, key) | ||
| future.add_done_callback(thread_utils.handle_future_result) | ||
|
|
||
| def start(self): | ||
| if self.listen_keyboard: | ||
| self.keyboard_listener.start() | ||
| if self.listen_mouse: | ||
| self.mouse_listener.start() | ||
|
|
||
| def stop(self): | ||
| self.keyboard_listener.stop() | ||
| self.mouse_listener.stop() | ||
| self._executor.shutdown(wait=False, cancel_futures=True) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from one_dragon.base.config.yaml_config import YamlConfig | ||
|
|
||
|
|
||
| class RunConfig(YamlConfig): | ||
| """千机链专有的运行行为配置,落在 `config/run.yml`。""" | ||
|
|
||
| def __init__(self, is_mock: bool = False): | ||
| YamlConfig.__init__( | ||
| self, | ||
| 'run', | ||
| is_mock=is_mock, sample=False, copy_from_sample=False, | ||
| ) | ||
|
|
||
| @property | ||
| def privacy_screen_close_key(self) -> str: | ||
| """隐私屏开关热键的主键(修饰键固定为 Ctrl+Alt+Shift)。""" | ||
| return self.get('privacy_screen_close_key', 'f12') | ||
|
|
||
| @privacy_screen_close_key.setter | ||
| def privacy_screen_close_key(self, new_value: str) -> None: | ||
| self.update('privacy_screen_close_key', new_value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: OneDragon-Anything/OneDragon-ScriptChainer
Length of output: 4039
🏁 Script executed:
Repository: OneDragon-Anything/OneDragon-ScriptChainer
Length of output: 23411
🏁 Script executed:
Repository: OneDragon-Anything/OneDragon-ScriptChainer
Length of output: 10871
🏁 Script executed:
Repository: OneDragon-Anything/OneDragon-ScriptChainer
Length of output: 961
🌐 Web query:
site:pynput.readthedocs.io/en/1.8.1 keyboard KeyCode char on_press character pynput 1.8.1💡 Result:
🏁 Script executed:
Repository: OneDragon-Anything/OneDragon-ScriptChainer
Length of output: 2466
🏁 Script executed:
Repository: OneDragon-Anything/OneDragon-ScriptChainer
Length of output: 3472
🏁 Script executed:
Repository: OneDragon-Anything/OneDragon-ScriptChainer
Length of output: 2547
🏁 Script executed:
Repository: OneDragon-Anything/OneDragon-ScriptChainer
Length of output: 4284
🏁 Script executed:
Repository: OneDragon-Anything/OneDragon-ScriptChainer
Length of output: 4136
让解析器识别录制到的
-。在 Windows 的 US 键盘布局下录制
-时,PcButtonListener会原样提交event.char。KeySettingCard会保存这个值,但key_to_vk只映射名称minus,不识别字面值-。创建隐私屏时,解析失败会使主键回退为f12,所以实际热键与录制值不同。Suggested fix
'minus': 0xBD, + '-': 0xBD,🤖 Prompt for AI Agents