How to run shortkeys using Python?

791 views Asked by At

I understand, we can execute Linux Shell Commands using subprocess

  import subprocess
  subprocess.call(["ls", "-l"])

What if i want to run CTRL+C action over the terminal?

My Use Case is:

   1> Open a linux screen
   2> Run a command over first window
   3> Then create a window in the same screen
   4> Run another command over the second window

It is pretty obvious i want to automate some part of my daily routine.

1

There are 1 answers

2
pythonian29033 On BEST ANSWER

Aah! found a solution for you;

if you use ubuntu or backtrack (a debian based linux flavour) then you can install this: apt-get install xautomation

and invoking keystrokes with this is a little easier, for people who like coding in English, but slightly more complicated:

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)