Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Release kbhit
#1
I there, is possible to release the msvcrt.kbhit()

I have this,

import msvcrt
while True:
	command = msvcrt.kbhit()
	if command == 1:
		print("You Press!!")
But When I press the button in all other cycles of the while kbhit still in 1, how can I do to "release" and set to 0 again the kbhit?

Thnks
Reply
#2
you need to write command == differently (need binary 'cast')
import msvcrt


def getchar():
    while True:
        print("Press '1' to quit")
        command = msvcrt.getch()
        print(f'command: {command}')
        # If using antique python, write above print statement like:
        # print('command: {}'.format(command))
        # f-string requires python 3.6 or newer
        if command == b'1':
            break

getchar()
Reply
#3
Can use msvcrt.kbhit() with msvcrt.getch() then call should be done only when key is pressed.
Always run from command line(cmd) when doing stuff like this,or can get unwanted result.
import msvcrt
import time

def stop_me(timeout=15):
   '''None Blocking version'''
   start_time = time.time()
   while True:
       if msvcrt.kbhit():
           key = msvcrt.getch()
           if key == chr(27).encode():
               return 'Esc key pushed'
       if time.time() - start_time > timeout:
           return f'Did not manage to stop me in {timeout} sec'

if __name__ == '__main__':
    print(stop_me())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get mouse coordinates on click and release OhNoSegFaultAgain 1 2,987 May-17-2019, 06:56 PM
Last Post: buran
  Python 3.4: the only release to create .EXE standalone without .dll samsonite 7 5,784 Feb-28-2019, 09:20 AM
Last Post: samsonite
  How to release control of file jmair 2 2,775 Oct-03-2018, 08:22 PM
Last Post: jmair
  How to release Memory of GPU! majinbuu 1 2,604 May-11-2018, 05:06 AM
Last Post: j.crater
  3.6.2 is latest stable release right? And "path length" Fran_3 3 3,316 Aug-01-2017, 02:04 PM
Last Post: Fran_3
  How to I make my program release memory? Plecto 11 10,680 Dec-18-2016, 10:03 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020