from pynput import keyboard# 文档:https://pynput.readthedocs.io/en/latest/keyboard.html#monitoring-the-keyboard
# 博客(pynput相关源码):https://blog.csdn.net/qq_39124701/article/details/145230331
# 虚拟键码(十六进制):https://learn.microsoft.com/zh-cn/windows/win32/inputdev/virtual-key-codes## 虚拟键码页面使用方法
## 1.访问虚拟键码页面, https://learn.microsoft.com/zh-cn/windows/win32/inputdev/virtual-key-codes
## 2.按F6将光标聚焦到地址栏## 粘贴这行文本 j删除avascript: document.querySelectorAll("table").forEach((table)=>{if(table.innerText.includes("0x01")){const thead=table.querySelector('thead');const tbody=table.querySelector('tbody');if(thead&&tbody){const th=document.createElement('th');th.textContent=decodeURIComponent('%E5%8D%81%E8%BF%9B%E5%88%B6%E5%80%BC');th.style.width='10%';th.style.textAlign='center';thead.querySelector('tr').insertBefore(th,thead.querySelector('tr').children[2]);tbody.querySelectorAll('tr').forEach(row=>{const td=document.createElement('td');td.style.width='10%';td.style.textAlign='center';const hexValue=row.querySelector('td:nth-child(2)').textContent.trim();const decimalValue=parseInt(hexValue,16);td.textContent=decimalValue;row.insertBefore(td,row.children[2])})}}})## 4.删除地址栏里的中文和#符号# 快捷键字符串列表
# 注意:pynput不能识别"<win>"、"<numpad1>"
hotkey_strings = ["<ctrl>+a","<ctrl>+<alt>+a","<ctrl>+<shift>+a","<ctrl>+<shift>+<alt>+a","<ctrl>+<cmd>+a","<alt>+a","<shift>+a","<cmd>+a","<ctrl>+~","<ctrl>+<f1>", # f1到f24"<ctrl>+1","<ctrl>+-","<ctrl>+=","<ctrl>+<backspace>","<ctrl>+<tab>","<ctrl>+q","<ctrl>+{","<ctrl>+}","<ctrl>+<enter>","<ctrl>+<caps_lock>","<ctrl>+;","<ctrl>+'",# 1.反斜线紧挨着引号会触发转义# 2.非原始字符串模式下, 反斜线+空格会触发无效转义# 3.pynput无法解析带空格的字符串r"<ctrl>+\ ".strip(),"<ctrl>+|","<ctrl>+<shift_l>","<ctrl>+,","<ctrl>+.","<ctrl>+/","<ctrl>+<shift_r>","<ctrl>+<cmd_l>","<ctrl>+<alt_l>","<ctrl>+<space>","<ctrl>+<alt_r>","<ctrl>+<cmd_r>","<ctrl>+<menu>","<ctrl_l>+<ctrl_r>","<ctrl>+<insert>","<ctrl>+<delete>","<ctrl>+<home>","<ctrl>+<end>","<ctrl>+<page_up>","<ctrl>+<page_down>","<ctrl>+<up>","<ctrl>+<down>","<ctrl>+<left>","<ctrl>+<right>","<ctrl>+<print_screen>","<ctrl>+<scroll_lock>","<ctrl>+<pause>","<ctrl>+<num_lock>",# pynput1.7.?不支持小键盘:https://github.com/moses-palmer/pynput/issues/545# 但是可以使用解决方案:https://github.com/moses-palmer/pynput/issues/545#issuecomment-2185323984"<ctrl>+<96>", # 小键盘数字0(96是pynput使用的十进制vk码)"<ctrl>+<105>", # 小键盘数字9"<ctrl>+<106>", # 小键盘*"<ctrl>+<107>", # 小键盘+"<ctrl>+<109>", # 小键盘-"<ctrl>+<110>", # 小键盘."<ctrl>+<111>", # 小键盘/
]# 随机生成快捷键
def generate_random_hotkey():import random# 随机选择一个快捷键字符串random_hotkey_str = random.choice(hotkey_strings)# 检查是否包含单个字符,并随机替换为a到z中的某个字符if '+' in random_hotkey_str:parts = random_hotkey_str.split('+')for i, part in enumerate(parts):if len(part) == 1 and part.isalpha():# 随机选择一个字符random_char = chr(random.randint(ord('a'), ord('z')))parts[i] = random_charelif part.startswith('<f') and part.endswith('>'):# 随机选择一个功能键random_f_key = f"<f{random.randint(1, 12)}>"parts[i] = random_f_keyrandom_hotkey_str = '+'.join(parts)try:# 解析快捷键字符串parsed_hotkey = keyboard.HotKey.parse(random_hotkey_str)print(f"生成的快捷键为: {random_hotkey_str}")return parsed_hotkey, random_hotkey_strexcept ValueError as e:print(f"解析 '{random_hotkey_str}' 失败: {e}")return None, random_hotkey_str# 测试这些快捷键字符串是否合法
def parse_hotkey_strings():# 解析每个快捷键字符串for hotkey_str in hotkey_strings:try:parsed_hotkey = keyboard.HotKey.parse(hotkey_str)print(f"\033[1;32m解析 '{hotkey_str}' 结果为: {parsed_hotkey}\033[0m")except ValueError as e:print(f"\033[1;31m解析 '{hotkey_str}' 失败: {e}\033[0m")# 测试监听小键盘
def test_numpad_hotkey():hotkey_s = '<ctrl>+<105>'def on_activate():print(f"按下了{hotkey_s}")def for_canonical(f):return lambda k: (setattr(k, '_scan', None), f(listener.canonical(k)))hotkey = keyboard.HotKey(keyboard.HotKey.parse(hotkey_s), on_activate)listener = keyboard.Listener(on_press=for_canonical(hotkey.press), on_release=for_canonical(hotkey.release))listener.start()import time;time.sleep(5)listener.stop()listener.join()# 按按键, 输出vk码
def print_vk_on_press():def format_key_info(key):key_info = f" {key}"if hasattr(key, 'char'):key_info += f' char:{key.char}'if hasattr(key, 'vk'):key_info += f' vk:{key.vk}'return key_infodef on_press(key):print(format_key_info(key))def on_release(key):print('{0} released'.format(key))if key == keyboard.Key.esc:import os;os._exit(0) # 强行退出, 因为主线程在睡觉# return Falselistener = keyboard.Listener(on_press=on_press, on_release=on_release)listener.start()import timetime.sleep(10)listener.stop()listener.join()if __name__ == "__main__":pass# 随机生成快捷键字符串for i in range(10):generate_random_hotkey()# 判断快捷键字符串合法性# parse_hotkey_strings()# 测试监听小键盘# test_numpad_hotkey()# 按按键,输出vk码# print_vk_on_press()
选择困难?直接生成pynput快捷键字符串
2025/1/30 16:28:01
来源:https://blog.csdn.net/qq_39124701/article/details/145382156
浏览:
次
关键词:选择困难?直接生成pynput快捷键字符串
版权声明:
本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。
我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com
热文排行
- 本周后半段,北方将遭今春首场沙尘天气
- 编程-设计模式 6:适配器模式
- 如何利用大数据技术来识别和预防网络赌博行为?
- 2024年全国青少年信息素养python编程复赛真题+答案
- LLMs之Llama 3.1:Llama 3.1的简介、安装和使用方法、案例应用之详细攻略
- JVM:SpringBoot TomcatEmbeddedWebappClassLoader
- No module named ‘tvtk.custom.light‘ mayavi
- 解决 Ubuntu 系统中 “Temporary Failure in Name Resolution“ 错误
- YOLOv10有效涨点专栏目录
- 开普勒优化算法( KOA)及其Python和MATLAB实现