2

I want to make a game with Python 3.6 and I have a problem. When I try to set conrols(like WASD), the Python Shell detect that I constantly press ÿ (unicode: b'\xff'). I have not ÿ on my keyboard. I wrote:print(getch()) and print(chr(ord(getch()))).

I have 2 questions:

  1. What is the solution to my problem?
  2. What is the best method to set controls in game made with Python?

Thank you in advance.

Piszcze24
  • 21
  • 1
  • 4
  • 1
    Could it be an `EOF` return code `-1` (`0xFF` if you try to make it into a char), signaling the end of input, that you're interpreting as a unicode sequence? This is certainly a control signal, not actual keyboard input. If you're using Curses, take a lookt at this http://stackoverflow.com/questions/4241366/getch-returns-1 – salezica Feb 21 '17 at 19:05

1 Answers1

0

I had similar problem when I was using IPython console included in Spyder. Here are my suggestions:

First, try to use cmd console to check if the problem continues.

Second, getch() does not wait for you to press a key and continuously reads. If you need to capture a few specific inputs you may need to use:

while True:
    if msvcrt.kbhit():
        ch = msvcrt.getch()
        print(ch)

Third, since you got bytes and you may want to check against few characters in your code, my suggestion for Windows is to use getwch() instead of getch(). Here is my code that logs characters with the time they have been pressed:

import msvcrt, sys, datetime
while True:
    if msvcrt.kbhit():
        ch = msvcrt.getwch()
        if ch == 'q':
           sys.exit()
        else:
           print (ch, " Pressed at : ", datetime.datetime.now().time())
Ali Rokni
  • 168
  • 7