允许自定义组合和关键字触发自定义函数的键盘侦听器
keyboard-listener的Python项目详细描述
键盘监听程序
键盘侦听器是一个模块,允许您创建自定义热键(组合)或自定义关键字,并将它们绑定到Python中的自定义函数。它非常容易使用,可以通过pip安装:
pip install keyboard_listener
组合是绑定到函数的自定义热键。当按下组合键时,执行该功能。组合非常容易设置,可以在下面的示例中看到。在
Keywords是自定义字符串,当输入(或者更确切地说,当它们的字符按顺序输入时)将触发要执行的函数。关键字也显示在下面的示例中。在
在运行之前,KeyboardListener对象需要用自定义组合和自定义关键字词典实例化。请参考下面的示例以了解如何设置这些。如果没有传递字典,程序将假定没有设置组合或关键字。在
示例:
fromkeyboard_listenerimportKeyboardListener,Combo,KeyWorddeffunction_1(arguments):# do somethingdeffunction_2(arguments):# do something elsecombinations={'function 1':Combo(['alt'],'f',function_1,arguments),#Function 1 is executed when the user presses Alt+F'function 2':Combo(['ctr','alt'],'g',function_2,arguments),#Function 2 is executed when the user presses Alt+G'function 3':Combo(['shift','alt'],'H',function_2,arguments),#Be mindful when setting up Combos that include 'shift'. If the Combo includes the shift key, the character must be uppercase.}keywords={'keyword_1':KeyWord('keyword1',function_1,arguments),#Function 1 is executed when the user types 'keyword1''keyword_2':KeyWord('keyword1',function_2,arguments)#Function 2 is executed when the user types 'keyword2'}keyboard_listener=KeyboardListener(combinations=combinations,keywords=keywords)keyboard_listener.run()
- 项目
标签: