So basically I'm trying to make a reaction test in Python. The implementation is unimportant, but I'll leave it below:
def reaction():
print("3",end="\r")
time.sleep(1)
print("2",end="\r")
time.sleep(1)
print("1",end="\r")
time.sleep(1)
print("---------------------")
time.sleep(random.randint(1,4))
reaction_start=int(round(time.time() * 1000))
input("PRESS!")
reaction_end=int(round(time.time() * 1000))
return (reaction_end - reaction_start)
So basically, it gives a countdown before showing "PRESS!", and then it records the time it takes for the user to press enter. I plan to repeat this test several times so that the user's performance can be averaged over.
However, you could cheat this by pressing "Enter" before the "Press!" sign shows up. The "enter" command seems to stack and gets executed the next time the program asks for user input. In fact this seems to be the case in general not only for this function but for every input related thing in Python.
That is, if you make any input when running Python, those inputs gets inserted into the next possible point when the Windows console next asks for inputs. So If I run a Python script, and then while the python script is running, I press "up" and "enter", the program will immediately press up and enter after the script is done running and rerun the last command (i.e. repeat the function). How do you stop commands from stacking in this way?
Related
Win32API commands seem not to work with PowerShell (I'm running 7, but I assume unimportant), only with the inferior IMHO Command Prompt.
I am building a program (a glorified HOSTS site blocker) that runs on the simple structure of "query command, execute command; repeat until user prompts to exit" and one of the things the user can query the program with is UNBLOCK category>site_nickname FOR minutes. This runs the program in one thread, starts a timer in another thread so the user can keep using the program as they desire. After the timer, the UNBLOCK command the program ran at start is undone so the user can get back to work. What I want to avoid is the user being able to cheat fate by saying UNBLOCK, then closing the program via the 'close' button, terminating the PowerShell instance, my Python program, and all its threads and thus ensuring the UNBLOCK command is never undone with a partner BLOCK command.
The user can exit the program by typing EXIT or CANCEL. This has the program wrap up, and then run sys.exit(). As I discovered, using the atexit library just basically does that but runs the function you want afterwards, does not intercept a close-button click. And setting the console control handler only seems to work in Command Prompt (whose support for ANSI escapes/emojis/Unicode probably too, seems to be spotty), also, the window_exit() function, or I guess the proper term is "method" --
def window_exit(signal_type):
sure = ask_input("Wait, wait! Are you sure you want to exit?") # input plus ANSI escape sequence, highlights the input prompt in blue and adds ">>" on a new line
if sure == "Yes":
evaluate("EXIT") # runs this command as if the user typed it in to be evaluated by my program
-- does not accept input properly. Instead, it displays the message and then for some reason decides I am trying to pull some "Press any key to continue ..."-type stuff, when I want the user to be able to type whatever and then have it be evaluated.
What can I do so any attempt to "cheat" and exit the program early is acknowledged and dealt with?
I am trying to use python to take in a string from a barcode scanner and use it to select a laser engraver file to execute. I am able to get the Max Marking (laser software) to open with the correct file, but am getting lost after that. I want to press "f2", which is the hotkey to run the laser, then wait on the "etching" prompt that the Max Marking software displays on the screen, then close Max Marking. I suppose I could test each of the engravings for their respective lengths of time and just use time.sleep(SomeAmountOfTime), but would like to make closing the program literally contingent on the engraving finishing. Is there a way to make python wait on the "currently etching" prompt that displays while the laser is running? This is within the Max Marking application and not a windows prompt. Here is what I have so far...
def notepad():
os.startfile('....filepath....')
time.sleep(2)
pyautogui.press('f2')
#Where I need to wait on etching prompt
os.system('taskkill /f /im maxmarking.exe')
A very simple solution could be _ = input("press ENTER when etching is finished"), it is not automatic but reliable.
If you want something completely automatic, it will be much more difficult. To detect that the prompt in another process has been displayed, either it provides an API to do that (which I doubt) or it will be very hacky (see the whole topic of "window automation", for exemple this question).
If having to return to the Python executing script is bothersome, you could use a hotkey to message it, see for example this question.
How do i rerun a program in IDLE shell without having to go back to the saved code and pressing F5 over and over every time I want to execute it? is there any shortcuts or hotkeys that could be pressed while in the shell?
for example i have a simple program that prints a typed message:
message = input('Enter message: ')
print(message)
once i run this simple program, i give the input, the program prints input and the program ends. Now, say i want to give a different input, i'd have to leave the shell and go back to the saved code and press F5 again. but if there was a hotkey that could be pressed while in the shell that would rerun the program, that would be awesome.
maybe add input() in the end of your code so you can see the results in console before it disappear
I have a simple python script that listens to my keypress and executes a command upon keypress. In my code, I have an infinite while loop which is pretty harsh on the performance of my computer. I'm wondering what's a better way to achieve the same behavior with less impact on the performance. The objective is to print a text which says
You Pressed A Key!
Every time I press a certain key, in this case, 'h', I want to be able to press h multiple times not just once hence the while loop.
import keyboard
while True:
if keyboard.is_pressed('h'):
print('You Pressed A Key!')
You may want to make the process sleep for a while using:
import time
time.sleep(number_of_seconds)
i write some python program.
i usually execute my program by CMD
but this time, i tried to execute program by double click
it works well until interpreter meet input code.
when i input some texts, it shut down
my input code is
for i in (input('range input => ')).split(' '):
range_list.append(int(i));
it works totally well when i execute by path(py ~.py) through the CMD
can you help me?
The interpreter is running in an endless loop. Executing your program from windows or via the command line using python will run and exit the program immediately.
At the end of your program just add
input()
This will keep it open so you can see your results.
Yeah when the program is done, it closes.
You can add something like x = input() at the end if you want to keep it open, or just run it in cmd.
Your program opens in a windowed mode when you double click it. The window will disappear / close down when the program finishes, which happens a lot fast after you have entered all the required inputs.
If you wish to see the output before the program exit, expect an input at the end of your program.