In Python, is there a way to prevent the user from entering anything via keyboard, so that terminal output does not get cluttered by unneeded input?
In my case, the user is supposed to press and hold a key to register a specific keyboard - so, the actual input (as in input()) is not needed, but only clutters the output and menu.
I would recommend pygame. http://www.pygame.org/download.shtml It allows you to read all keyboard keys as you like. Here's an example of how the code would look: How to get keyboard input in pygame?
It sounds like you want the terminal contents to redraw themselves on user input?
https://docs.python.org/3/howto/curses.html or https://docs.python.org/2.7/howto/curses.html depending on python version.
See also this question Dynamic terminal printing with python
Related
A program I'm writing takes user commands from input(), executes corresponding functions, and displays relevant text.
After about 5 commands-worth of text, the terminal becomes cluttered even when the window is maximized. What I would like to do is clear the terminal after every five commands, but only clear the text that precedes (is above) the fifth command and its output text.
More specifically, after the user has typed in the fifth command, upon pressing Return (entering the command), I want commands 1-4 and their corresponding outputs to clear off the screen but have command 5 and its output remain at the top of the terminal.
For demonstration, here is what I want the screen would look like during this process:
The above becomes the below:
Using the os module and os.system('cls') or os.system('clear') functions will not exactly work in this situation. I don't want to clear all of the text on the screen, just the text before a certain point.
So, how can I do this on Windows with Python?
Note: If the solutions are simple, I would like both a method of obliterating the text so that it cannot be scrolled back up to as well as a method that would allow users to see previous commands and text.
Using simple terminal output, there isn't really a good way to do this. Even the operation of "clearing the screen" is outside what is normally considered simple terminal output, which is why you end up calling an external program to do it.
However, a different way of handling terminal output is to use the curses library. This library allows you extensive control over exactly how your output appears on the screen, and in fact includes functions like deleteln and insdelln to delete lines of text from the screen.
I am reading user input text with getstr(). Instead of waiting for the user to press enter, I would like to read the input each time it is changed and re-render other parts of the screen based on the input.
Is this possible with getstr()? How? If not, what's the simplest/easiest alternative?
Not with getstr(), but it's certainly possible with curses. You just have to read each keypress one at a time, via getch() -- and, if you want an editable buffer, you have to recreate something like the functionality of getstr() yourself. (I'd post an example, but what I have is in C rather than Python.)
I'm creating a script for a game because I want to automate a certain part of it. So far I have:
import win32api, win32con, time
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
click(100,655)
time.sleep(3)
click(740,580)
time.sleep(1)
raw_input(100)
So far, I click on the correct page I need to go to, then I click on the textbox where I can enter a number, but after selecting the textbox I cannot quite figure out how to enter a number. I thought to use raw_input, but it has acted like a print statement instead.
The raw_input function isn't going to simulate keystrokes to another program. What it will do is print the prompt to its console, wait for you to type a response to that console, and return what you typed to your script. Completely useless here.
What you actually want is a way to send keyboard events to the app, the same way you're sending mouse events.
If you can depend on Windows Scripting Host being present (which I think is always there in Vista and XPSP3 and later, and can be installed for earlier XP), you can just use it instead of doing things at the low level:
wshell = win32com.client.Dispatch("WScript.Shell")
wshell.SendKeys("foo")
Otherwise, you'll need to get a handle to the window (that's explained in the win32api docs, so I assume you already know it) then something like this:
def sendkey(hwnd, keycode):
win32api.PostMessage(hwnd, win32con.WM_CHAR, keycode, 0)
This won't handle special keys like tab, escape, or return properly. For that, you need to instead send WM_KEYDOWN and WM_KEYUP. But for your use, WM_CHAR is what you want.
You also need a function to look up the keycode for each character in your string. For '100' it's actually just ord('1'), ord('0'), ord('0'), but that's not true for everything.
You may want to look at SendKeys and similar modules that wrap all of this up for you.
Or you may want to use a higher-level automation library like AutoPy (there are many of these, and if you search SO you'll find details about all of them).
Or you may want to forget about trying to automate the browser in terms of mouse clicks and key events and instead deal with it at the appropriate (web) level by using selenium.
Or you may want to forget about automating the browser and instead just simulate a browser in your own script by using mechanize.
In Pygame, how can I get graphical input(e.g. clicking exit button) and also get input from the a terminal window simultaneously?
To give you context, my game has a GUI but gets its game commands from a "input()" command. How can I look for input from the command line while also handling graphics?
I'm not sure if this is possible, but if not, what other options do I have for getting text input from the user?
Thanks in advance.
You can't do that, unless you use the input command in a different thread, but then you have to deal with syncronization (which might be what you want or don't want to do).
The way I'd implement this is to create a kind of in-game console. When a special key (e.g. '\') is pressed you make the console appear, and when your application is in that state you interpreter key pressing not as in-game commands but... well, as text. You can print them in the console (using fonts). When a key (e.g "return") is pressed you can make the console disappear and the keys take back their primary functionality.
I did this for my pet-project and it works as a charm. Plus, since you are developing in python you can accept python instructions and use exec to execute them and edit your game "on fhe fly"
Suppose I have a game and a python script running. In this game, to speak you just type whatever you want and hit enter. This python script has a button on it that I want to output a predefined string into the game, and hit enter automatically (essentially, the button causes the character to speak the string). What would be the easiest way to implement this?
(just the actual 'send string to game and hit enter' thing, not the buttons and stuff)
Assuming your game is not running in the console (in that case you could use stdin), sendkeys might be an option on Windows. It allows you to send keystrokes to a certain window - in this case, the game window.
If the game is scriptable, you should of course use the game's own scripting options if available.
It depends on what hooks the game provides. If it doesn't provide any hooks, you may want to look into whatever your windowing system uses for automation. I've only done this sort of thing in Linux where I could use X's XTest extension (xte).