Python: How do I generate a keypress?

I am opening a process (with os.popen() ) that, for some commands, detects certain keypresses (e.g. ESC - not the character, the key). Is there a way to send keypress events to the process?


Asked by: Kelvin799 | Posted: 01-10-2021






Answer 1

You probably want something like Pexpect. It's been around a while, and there may be a better alternative, now, but it will let you do what you want.

As far as I know, there is no easy way to do that kind of thing with os.popen or the commands in the subprocess module.

Answered by: Charlie351 | Posted: 02-11-2021



Answer 2

The obvious way would be to start the process in it's own shell.
something like os.popen("sh command")

Answered by: Elise855 | Posted: 02-11-2021



Answer 3

What platform is this on?

You may have to actually feed events into the event loop, if it's running on Win32.

Answered by: Maddie964 | Posted: 02-11-2021



Similar questions

How can I use python xlib to generate a single keypress?

I want to make a very simple python 3 script that will generate a single keypress (F15). I don't want to use a bunch of libraries to do this as I only need one key to be pressed and don't need support for the whole keyboard. I know I need to use KeyPress and KeyRelease in order to generate a keyboard event. I'm just not sure where exactly to start and the documentation is a little confusing.


vcf vcard - Python Library to Generate VCF Files?

Closed. This question does not meet Stack Overflow guid...


python - py2exe to generate dlls?

Is there a way using py2exe or some other method to generate dll files instead of exe files? I would want to basically create a normal win32 dll with normal functions but these functions would be coded in python instead of c++.


python - What is a simple way to generate keywords from a text?

I suppose I could take a text and remove high frequency English words from it. By keywords, I mean that I want to extract words that are most the characterizing of the content of the text (tags ) . It doesn't have to be perfect, a good approximation is perfect for my needs. Has anyone done anything like that? Do you known a Perl or Python library that does that? Lingua::EN::Tagger is exactly what I asked ...


How do I generate test data for my Python script?

A equation takes values in the following form : x = [0x02,0x00] # which is later internally converted to in the called function to 0x300 y = [0x01, 0xFF] z = [0x01, 0x0F] How do I generate a series of test values for this function ? for instance I want to send a 100 odd values from a for loop for i in range(0,300): # where a,b are derived for a range x...


In Python, how do I easily generate an image file from some source data?

I have some data that I would like to visualize. Each byte of the source data roughly corresponds to a pixel value of the image. What is the easiest way to generate an image file (bitmap?) using Python?


python - How to generate pdf with epydoc?

I am considering epydoc for the documentation of one module. It looks ok to me and is working fine when I am generating html document. I would like to try to generate the documenation in the pdf format. I've just modified the 'output' setting in my config file. Unfortunately, epydoc fails when generating the pdf file. The error is "Error: Error reading pstat file: [Errno 2] No such file or directory: 'prof...


unicode - Generate random UTF-8 string in Python

I'd like to test the Unicode handling of my code. Is there anything I can put in random.choice() to select from the entire Unicode range, preferably not an external module? Neither Google nor StackOverflow seems to have an answer. Edit: It looks like this is more complex than expected, so I'll rephrase the question - Is the following code sufficient to generate all valid


python - Generate two lists at once

Background The algorithm manipulates financial analytics. There are multiple lists of the same size and they are filtered into other lists for analysis. I am doing the same filtering on different by parallel lists. I could set it up so that a1,b1,c2 occur as a tuple in a list but then the analytics have to stripe the tuples the other way to do analysis (regression of one list against the other, bet...


How can I generate random numbers in Python?

Are there any built-in libraries in Python or Numpy to generate random numbers based on various common distributions, such as: Normal Poisson Exponential Bernoulli And various others? Are there any such libraries with multi-variate distributions?


Generate pretty diff html in Python

I have two chunks of text that I would like to compare and see which words/lines have been added/removed/modified in Python (similar to a Wiki's Diff Output). I have tried difflib.HtmlDiff but it's output is less than pretty. Is there a way in Python (or external library) that would generate clean looking HTML of the diff of two sets of text chunks? (not just line level, but also word/character modificat...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top