[Tutor] key detection
Jim Mooney Py3.4.3winXP
cybervigilante at gmail.com
Wed May 6 19:41:26 CEST 2015
On 5 May 2015 at 21:51, Mark Lawrence <breamoreboy at yahoo.co.uk> wrote:
> On 06/05/2015 05:30, Jim Mooney Py3.4.3winXP wrote:
>
>> On 5 May 2015 at 18:32, Steven D'Aprano <steve at pearwood.info> wrote:
>>
>> https://code.activestate.com/recipes/577977-get-single-keypress/
>>>
>>
>>
>> That only has a stub for Linux, but I found one that does both. Although,
>> alas, no IOS version:
>>
>>
>> http://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin/
>>
>> Anyway, I set up msvcrt for now until I install linux - I'm not interested
>> in the program I mentioned, per se, just the error message - mainly to
>> know, generally, what sort of thing it's choking on. Errors are useful to
>> know.
>>
>>
> I went a further step from the recipes linked to above and got here
> https://pypi.python.org/pypi/readchar
I think that's the one that failed for me but I found out why. I just wrote
the simple snippet below to try msvcrt out. I can add a Linux try block
when I install Linux and actually find a wifi driver for it ;')
I reproduced the error that puzzled me almost immediately. It was from
hitting a function key. The snippet below worked fine for letters and such,
but died when I hit a function key (although not all of them). I was
decoding since msvcrt sends byte strings, but there was nothing in the
utf-8 map for that key. The 2> redirect is sure handy for dos console
error messages - something I'll have to remember ;')
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
--
Jim
"What a rotten, failed experiment. I'll start over. Maybe dogs instead of
monkeys this time." --God
More information about the Tutor
mailing list