9

I'm writing a simple program for Windows using Python, and this program takes advantage of the time module. Specifically, I'm using time.sleep(x) to pause the program for a short moment, generally 0.5-2 seconds. Here's basically what I'm doing:

import time
time.sleep(2)

while True:
    x = input(prompt)
    if x == 'spam':
        break

The problem with this is, if the user presses enter while time.sleep has it paused, then those enters will be counted towards the input in the while loop. This results in prompt being printed several times, which is frustrating.

I want to know if there's a way to temporarily disable keyboard input while time.sleep is going on, and then enable it afterwards. Something like this:

import time
disable_keyboard_input()
time.sleep(2)
enable_keyboard_input()

while True:
    etc.

Does anyone know of a way to do this using Python? Thank you in advance!

RbwNjaFurret
  • 91
  • 1
  • 1
  • 4
  • http://stackoverflow.com/questions/2520893/how-to-flush-the-input-stream-in-python pretty much a duplicate – Maltysen Mar 26 '15 at 22:26
  • @Maltysen That method didn't work for me :( – RbwNjaFurret Mar 26 '15 at 22:47
  • `while msvcrt.kbhit(): msvcrt.getwch()` should work to clear the keyboard buffer after sleeping. The caveat is that this only works when `stdin` is the Windows console, not in development environments that replace `sys.stdin` with a pipe or file-like object. – Eryk Sun Mar 28 '15 at 02:23
  • @eryksun Thank you so much, that worked! I didn't try running it through the console because I didn't know that it changed how `sys.stdin` worked. It works perfectly now, thank you so much! :D – RbwNjaFurret Mar 28 '15 at 20:06
  • Also a duplicate of [disable or lock mouse and keyboard in Python?](https://stackoverflow.com/questions/7529991/disable-or-lock-mouse-and-keyboard-in-python) – Toothpick Anemone Nov 11 '19 at 01:09

3 Answers3

3

I found this worked brilliantly:

import time
class keyboardDisable():

    def start(self):
        self.on = True

    def stop(self):
        self.on = False

    def __call__(self): 
        while self.on:
            msvcrt.getwch()


    def __init__(self):
        self.on = False
        import msvcrt

disable = keyboardDisable()
disable.start()
time.sleep(10)
disable.stop()

It stops the user from typing anything; when you press a key on the keyboard, nothing happens.

flumperious
  • 164
  • 1
  • 8
  • I integrated this code in one of my programs and it is really useful. My question is, exactly how does it work? – unkn0wn.dev Nov 15 '19 at 00:40
  • @pythonier500 first of all, sorry for the late reply. The code works by constantly running msvcrt.getwch(), which returns the key pressed before the character is printed on screen. Generally, this would be used in code where the character is printed after being modified somehow, but in this case the value returned is ignored. If you were to instead print it, you would be able to type normally. TLRD: It intercepts the key before it is displayed – flumperious Nov 20 '19 at 09:38
  • Does the code above generally disables the input from keyboard, or only in a shell or cmd ? Moreover if I understand well the result that we want to get is to disable ANY input from keyboard for 10 seconds. – ppel123 Apr 06 '20 at 22:38
  • It only works within the shell/cmd of python, and only works to stop characters being displayed there. If you want to stop all keyboard input, thats much harder. It just makes python ignore keypresses, which solves OP's problem – flumperious Apr 08 '20 at 12:51
  • Okk thanks @mono , I am trying to suppress not stop the keyboard input if a specific condition is True, but struggling a lot. – ppel123 Apr 08 '20 at 15:50
  • I don't see how `__call__` is called. The object is called `disable` but there is no call to `disable()`. If I do call it, it hangs forever, because it's in the endless while loop – Thomas Weller Oct 21 '21 at 17:46
3

The other 2 answers are perfect if you are using it to make a real software and want to disable keyboard input only in the output screen. However, if it's for personal usage or you want to disable the keyboard for the whole system, you can use this code:

import keyboard
import time
a = int(input("Enter the time (in seconds) for which you want to disable keyboard: "))
for i in range(150):
    keyboard.block_key(i)
time.sleep(a)

Don't forget to run pip install keyboard in the terminal before executing this code otherwise, you will get an error.

Shubham Garg
  • 459
  • 3
  • 10
-1

Try this.

stdout = sys.stdout
sys.stdout = sys.stderr
time.sleep(2)

sys.stdout = stdout

while True:
    pass

Didn't tried this. But I hope all that was typed before sleep is ended will sended to stderr... Don't sure, but maybe Python have access to linux's std/null?

Alexander R.
  • 1,756
  • 12
  • 19