[Tutor] key detection

Dave Angel davea at davea.name
Wed May 6 23:08:39 CEST 2015


On 05/06/2015 01:41 PM, Jim Mooney Py3.4.3winXP wrote:

> from msvcrt import *
>
> while True:
>      if kbhit():
>          key = getch()
>          if key == b'\xe0' or key == b'\000':
>              print('special key follows')
>              key = getch()
>              print(str(key, encoding='utf-8')) #got rid of this decode after
> a function key error
>          else:
>              print('The key is: ', str(key, encoding='utf-8'))
>
> Traceback (most recent call last):
>    File "keyget.py", line 9, in <module>
>      print(str(key, encoding='utf-8'))
> UnicodeDecodeError: 'utf-8' codec can't decode byte 0x85 in position 0:
> invalid start byte
>
>
>

I don't know why you would be expecting to get a utf-8 character for the 
second byte of a function key code.  It's an entirely arbitrary byte 
sequence, and not equivalent to anything in Unicode, encoded or not.


-- 
DaveA


More information about the Tutor mailing list