如何在Python中实现组合键(包括修饰键)? [英] How to implement key combinations (including modifier keys) in Python?

查看:2004
本文介绍了如何在Python中实现组合键(包括修饰键)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道弓箭如何在Python中实现两键组合.我正在使用turtle.onkey()函数,因为我正在使用乌龟图形,但是它似乎不是很通用,因为我找不到找到键组合的方法.因此,是否可以使用其他方式 python 中实现组合键?非常感谢您对此问题的任何帮助! :)

I am wondering about bow to implement two-key combinations in Python. I am utilizing the turtle.onkey() function since I am using turtle graphics, but it seems like it is not very versatile, as I cannot find a way for it to take key combinations. So, is there any other way to implement key combinations in python? Any help regarding this issue is very much appreciated! :)

推荐答案

我使用pykeyboard(在Python 2.7中)同时按下"两个键盘键作为热键. pykeyboard软件包随 PyUserInput 安装一起安装.

I use pykeyboard ( with Python 2.7 ) to "simultaneously press" 2 keyboard keys as a hotkey. The pykeyboard package comes installed with the PyUserInput installation.

代码示例:如果我想自动让我的程序按热键组合(CTRL + P)来打印某些东西,而无需用手指在键盘上,这是通过pykeyboard进行的操作.

Code Example : If I want to automatically have my program hit the hotkey combination ( CTRL + P ) to print something without me laying a finger on the keyboard, here is how I would do it through pykeyboard.

import pykeyboard
keyboard_object = pykeyboard.PyKeyboard()

我首先创建如上所述的键盘对象.

I begin by creating my keyboard object as shown above.

keyboard_object.press_key   ( keyboard_object.control_key )
keyboard_object.tap_key     ( "P" )  # Upper-Case
keyboard_object.release_key ( keyboard_object.control_key )

那应该会弹出打印窗口,我可以让我的脚本按Enter键,使它继续以默认设置打印.

That should bring up the print window and I could have it continue forward to print on the default setting by having my script hit the Enter Key.

keyboard_object.tap_key ( keyboard_object.enter_key )

我什至可以拥有自己的脚本类型自定义字符串.如果我想以自动形式通过指定的字符串保存文件,这将很有用.我将从(CTRL + S)热键组合开始.

I can even have my script type custom strings. This can be useful if I want to save a file by a specified string in an automated form. I'll begin with the ( CTRL + S ) hotkey combination.

keyboard_object.press_key   ( keyboard_object.control_key )
keyboard_object.tap_key     ( "S" )  # Upper-Case
keyboard_object.release_key ( keyboard_object.control_key )

应弹出另存为"窗口,提示我指定要保存的文件名.所以我将其作为字符串传递.

The "Save-As" window should pop up, prompting me to specify the filename that I want to have saved. So I'll pass that as a string.

keyboard_object.type_string ( "MyFilename-03_22_2016.html" )
keyboard_object.tap_key ( keyboard_object.enter_key )

Voila!

这篇关于如何在Python中实现组合键(包括修饰键)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆