How to pass a keystroke (ALT+TAB) using Popen.communicate (on Linux)?
You probably can't do exactly what your question says - pass keystrokes using Popen. You can send bytes to the stdin
of the process you've opened, but it's almost certainly not looking there for them. Keyboard events are different from data coming on stdin
.
The pyautogui library library could be useful for this purpose, however. Once it's installed, you could launch your viewer with Popen
, as you've done, and then use
pyautogui.hotkey('alt', 'tab')
To send alt+tab to the foreground application. You may need to add a short delay to make sure ristretto has finished launching before sending the keys. See pyautogui's keyboard documentation for more details about how to use it.
Sending Arrow Keys to Popen
I found someone who was trying to solve the opposite problem, create a program that could recognize the arrow keys: Recognizing arrow keys with stdin
I also found
http://compgroups.net/comp.unix.programmer/how-to-send-up-arrow-key-to-popen-child/537480
which says:
"\x1B[A" for up
"\x1B[B" for down
So if \x1B is the escape character than you just append [A for up, [B for down, [C for right and [D for left and so on.
Take a look at http://en.wikipedia.org/wiki/ANSI_escape_sequences for a list of the different codes.
Simulate keystroke in Linux with Python
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)
Why alt+tab don't work when using xshell 4.0?
Your issue sounds like you have XShell set as "always on top"
First, make sure the Transparent option is disabled under the View option. Then, disable the following two options:
- Make window opaque on mouse over
- Always on top when transparent
Also, if the problem persists, please try resetting the registry key. To remove Xshell registry keys:
- Click on the Windows Start menu and click Run.
- Enter regedit and press enter.
- Delete the following registry node:
\HKEY_CURRENT_USER\Software\NetSarang\Xshell
How to echo with different colors in the Windows command line
I wanted to to print one single line in a different color.
Use ANSI Escape Sequences.
Windows before 10 - no native support for ANSI colors on the console
For Windows version below 10, the Windows command console doesn't support output coloring by default. You could install either Cmder, ConEmu, ANSICON or Mintty (used by default in GitBash and Cygwin) to add coloring support to your Windows command console.
Windows 10 - Command Line Colors
Starting from Windows 10 the Windows console support ANSI Escape Sequences and some colors by default. The feature shipped with the Threshold 2 Update in Nov 2015.
MSDN Documentation
Update (05-2019): The ColorTool enables you to change the color scheme of the console. It's part of the Microsoft Terminal project.
Demo
Batch Command
The win10colors.cmd
was written by Michele Locati:
The text below is stripped of special characters and will not work. You must copy it from here.
@echo off
cls
echo [101;93m STYLES [0m
echo ^<ESC^>[0m [0mReset[0m
echo ^<ESC^>[1m [1mBold[0m
echo ^<ESC^>[4m [4mUnderline[0m
echo ^<ESC^>[7m [7mInverse[0m
echo.
echo [101;93m NORMAL FOREGROUND COLORS [0m
echo ^<ESC^>[30m [30mBlack[0m (black)
echo ^<ESC^>[31m [31mRed[0m
echo ^<ESC^>[32m [32mGreen[0m
echo ^<ESC^>[33m [33mYellow[0m
echo ^<ESC^>[34m [34mBlue[0m
echo ^<ESC^>[35m [35mMagenta[0m
echo ^<ESC^>[36m [36mCyan[0m
echo ^<ESC^>[37m [37mWhite[0m
echo.
echo [101;93m NORMAL BACKGROUND COLORS [0m
echo ^<ESC^>[40m [40mBlack[0m
echo ^<ESC^>[41m [41mRed[0m
echo ^<ESC^>[42m [42mGreen[0m
echo ^<ESC^>[43m [43mYellow[0m
echo ^<ESC^>[44m [44mBlue[0m
echo ^<ESC^>[45m [45mMagenta[0m
echo ^<ESC^>[46m [46mCyan[0m
echo ^<ESC^>[47m [47mWhite[0m (white)
echo.
echo [101;93m STRONG FOREGROUND COLORS [0m
echo ^<ESC^>[90m [90mWhite[0m
echo ^<ESC^>[91m [91mRed[0m
echo ^<ESC^>[92m [92mGreen[0m
echo ^<ESC^>[93m [93mYellow[0m
echo ^<ESC^>[94m [94mBlue[0m
echo ^<ESC^>[95m [95mMagenta[0m
echo ^<ESC^>[96m [96mCyan[0m
echo ^<ESC^>[97m [97mWhite[0m
echo.
echo [101;93m STRONG BACKGROUND COLORS [0m
echo ^<ESC^>[100m [100mBlack[0m
echo ^<ESC^>[101m [101mRed[0m
echo ^<ESC^>[102m [102mGreen[0m
echo ^<ESC^>[103m [103mYellow[0m
echo ^<ESC^>[104m [104mBlue[0m
echo ^<ESC^>[105m [105mMagenta[0m
echo ^<ESC^>[106m [106mCyan[0m
echo ^<ESC^>[107m [107mWhite[0m
echo.
echo [101;93m COMBINATIONS [0m
echo ^<ESC^>[31m [31mred foreground color[0m
echo ^<ESC^>[7m [7minverse foreground ^<-^> background[0m
echo ^<ESC^>[7;31m [7;31minverse red foreground color[0m
echo ^<ESC^>[7m and nested ^<ESC^>[31m [7mbefore [31mnested[0m
echo ^<ESC^>[31m and nested ^<ESC^>[7m [31mbefore [7mnested[0m
Related Topics
Differencebetween C.Utf-8 and En_Us.Utf-8 Locales
Capturing Output of Python Script Run Inside a Docker Container
How to Default Python3.8 on My MAC Using Homebrew
How to Delete a Directory Created with Tempfile.Mkdtemp
How to Deal with Linux/Python Dependencies
How to Download Python from Command-Line
How to Kill Zombie Processes Created by Multiprocessing Module
Howto Do Python Command-Line Autocompletion But Not Only at the Beginning of a String
Pyodbc:Can't Open the Driver Even If It Exists
Install Tkinter and Python Locally
Fake Serial Communication Under Linux
Paramiko Error: Error Reading Ssh Protocol Banner
Python: When to Use Pty.Fork() Versus Os.Fork()
Create Single Python Executable Module
Python Valueerror: Embedded Null Byte When Reading Png File from Bash Pipe