[Tutor] key detection

Steven D'Aprano steve at pearwood.info
Thu May 7 01:52:25 CEST 2015


On Wed, May 06, 2015 at 03:24:20PM -0700, Jim Mooney Py3.4.3winXP wrote:
> On 6 May 2015 at 14:08, Dave Angel <davea at davea.name> wrote:
> 
> > 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
> 
> 
> I just didn't think of accounting for function keys until I hit one -
> experimental learning. The program I'm working on doesn't require F keys,
> but I tried one just to see what would happen ;')  It's worth making the
> error to reinforce unicode in my head. 

I'm not entirely sure how dealing with something that has nothing to do 
with Unicode will reinforce Unicode in your head. That sounds a bit like 
saying "I love to cook, I find chopping vegetables really helps my 
Python programming..."  :-)


> One item - once I dropped decoding
> for special keys, some were printed as hex codes but some as letters. i.e.
> F11 was b'\x85', but F9 and F10 were b'C' and b'D', so I assume the second
> byte of some function keys just happens to map to utf-8 letters. 

It would be more correct to say they happen to map to ASCII letters.

[Aside: I still don't know whether I like or dislike the (mis)feature 
where bytes are displayed by default as if they were ASCII strings.]

It's not quite fair to say that it is an "accident" that it maps to a 
UTF-8 character. After all, UTF-8 was carefully designed to be ASCII 
compatible, and maybe there was some specific reason why the second byte 
of F9 is 0x43 (ASCII 'C'). But perhaps we can say that it is an accident 
of history that it partially matches UTF-8. It certainly isn't intended 
to match UTF-8.

Think of it this way: if you open a JPEG file in binary mode, to get a 
bunch of bytes, and manage by trial and error to find a sequence 
somewhere inside the file that decodes as UTF-8 without error, that 
doesn't mean that the inventor of the JPEG image format had UTF-8 in 
mind when he designed it.



-- 
Steve


More information about the Tutor mailing list