Python Forum
msvcrt.getch prints twice inside loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
msvcrt.getch prints twice inside loop
#1
A recent Windows update (Windows 10 version 1803 feature update) seems to have changed the behaviour of msvcrt.getch(). I confirmed this by testing on a second computer before and after the update.

this code:
import msvcrt

while True:
    character = msvcrt.getch()
    print('Hello World')
prints twice for each keystroke:
Output:
Hello World Hello World
tested with python 3.6.5 and 3.5.3

Am I going crazy, or am I using msvcrt.getch() wrong since the Windows update?

What I really need is to put a keystroke in to a variable and print it to the command line, but it's printing an extra line after each character and my program no longer works.

hitting the 'b' key three times with this code:
import msvcrt

while True:
    character = msvcrt.getch().decode('utf-8')
    print(character)
Gives this output:
Output:
b b b
Any ideas as to what has changed?
Reply


Messages In This Thread
msvcrt.getch prints twice inside loop - by ReallyBigTeeth - Jun-22-2018, 09:49 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 451 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,776 Nov-07-2023, 09:49 AM
Last Post: buran
  zfill prints extra et the end of a var tester_V 4 915 Mar-24-2023, 06:59 PM
Last Post: tester_V
  Help adding a loop inside a loop Extra 31 4,626 Oct-23-2022, 12:16 AM
Last Post: Extra
  variable prints without being declared. ClockPillow 2 1,822 Jul-11-2021, 12:13 AM
Last Post: ClockPillow
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 2,734 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop JulyFire 2 2,544 Jan-10-2021, 01:50 AM
Last Post: JulyFire
  Output prints Account.id at the end? LastStopDEVS 5 2,803 Dec-19-2020, 05:59 AM
Last Post: buran
  Try/Exept prints only ones tester_V 11 3,914 Nov-03-2020, 02:38 AM
Last Post: tester_V
  Creating a variables inside FOR loop zazas321 5 4,113 Sep-16-2020, 04:42 PM
Last Post: Naheed

Forum Jump:

User Panel Messages

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