本文介绍了使用Python在Linux中模拟按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在python中模拟按键?我也想同时按下多个键.

How can I simulate a keystroke in python? I also want to press multiple keys simultaneously.

类似的东西:

keystroke('CTRL+F4')

keystroke('Shift+A')

推荐答案

尽管它特定于X,但是您可以安装xautomation软件包(在基于Debian的系统上为apt-get install xautomation),并使用xte模拟按键,例如:

Although it's specific to X, you can install the xautomation package (apt-get install xautomation on Debian-based systems) and use xte to simulate keypresses, e.g.:

from subprocess import Popen, PIPE

control_f4_sequence = '''keydown Control_L
key F4
keyup Control_L
'''

shift_a_sequence = '''keydown Shift_L
key A
keyup Shift_L
'''

def keypress(sequence):
    p = Popen(['xte'], stdin=PIPE)
    p.communicate(input=sequence)

keypress(shift_a_sequence)
keypress(control_f4_sequence)

这篇关于使用Python在Linux中模拟按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 05:04