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.)
Related
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
I was wondering if there's a way to detect onkeyboardevents, but just to count them. I don't want to know exactly what the user has typed, but more like if he typed anything.
To be more specific:
If user presses key "a" on keyboard, I am interested to know that he pressed it (and at what time), not what the letter is.
Scenario:
User types in "hello stackoverflow" in the span of 2.5 seconds.
I want to know that he was typing something, don't care about what he typed.
Why do I want to do this? Some programs detect "keyloggers" as viruses (and they should) and I don't want my users to think it's something fishy.
Cheers.
in python keylogger itself, just keep a count in the place of recording the key stroke,
while(event.Ascii):
c++
refer: https://github.com/VigneshPrasadV
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"
I am writing an editor which has lot of parameters that could be easily interacted with through text. I find it inconvenient to implement a separate text-editor or lots of UI code for every little parameter. Usual buttons, boxes and gadgets would be burdensome and clumsy. I'd much rather let user interact with those parameters through vim.
The preferable way for me would be to get editor open vim with my text buffer. Then, when one would save the text buffer in vim, my editor would get notified from that and update it's view.
Write your intermediate results (what you want the user to edit) to a temp file. Then use the $EDITOR environment variable in a system call to make the user edit the temp file, and read the results when the process finishes.
This lets users configure which editor they want to use in a pseudo-standard fashion.
Check out It's All Text!. It's a Firefox add-in that does something similar for textareas on web pages, except the editor in question is configurable.
You can also think about integrating VIM in to your app. Pida does this