msvcrt.getch()方法中的\xe0是什么? [英] What is \xe0 in the msvcrt.getch() method?
问题描述
在msvcrt.getch()方法中,当我输入ascii表中没有的任何值时,我总是得到 \xe0。我不知道这是什么意思
In the msvcrt.getch() method, when I enter any value that isn't in the ascii table, I get '\xe0' all the time. I don't know what this means
>>>import msvcrt
>>>up_arrow = msvcrt.getch()
>>> # this is where I have inputed the up arrow
>>>print up_arrow
'\xe0'
>>>down_arrow = msvcrt.getch()
>>>
>>>print down_arrow
'\xe0'
推荐答案
这在文档中进行了解释:
如果按下的键是特殊功能键,则将返回 \000或 \xe0 ';
If the pressed key was a special function key, this will return '\000' or '\xe0'; the next call will return the keycode.
如您所见,您可以从源,Python只是调用了MSVCRT函数 _getch
,有点像POSIX函数 getch
,但在一个按键方式上有所不同:
Under the covers, as you can see from the source, Python is just calling the MSVCRT function _getch
, which is sort of like the POSIX function getch
, but different in one key way:
读取功能键或箭头键时,每个功能必须被两次叫过第一次调用返回0或0xE0,第二次调用返回实际的密钥代码。
When reading a function key or an arrow key, each function must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.
其历史原因可追溯到DOS Windows NT 3.x中的兼容性(或者,对于Windows NT,Microsoft C ++中的DOS兼容性,是Turbo C)。
The historical reason for this goes back to DOS compatibility in Windows NT 3.x (or, rather, Turbo C for DOS compatibility in Microsoft C++ for Windows NT).
IIRC(我可能已经弄错了一些详细信息……),基本思想是:您将通过BIOS调用来获取hi和lo键盘的值,这意味着每个键都有一个16位值。为了使事情变得更简单,Turbo C提供了一个不错的调用,该调用会将键映射到当前(8位)代码页中的字符,因此 a
会给您一个回音。个字节, 0x61
。但是没有足够的空间来映射所有内容,因此在两个单独的调用中,未映射的特殊键如 VK_UP
将被返回,首先是hi字节 0xE0
,然后是lo字节 0x50
。微软从Borland复制了该代码,以便更轻松地将代码移植到其编译器,然后再将代码从DOS / Win3.1移植到NT,这就是MSVCRT如今仍在做的事情,而Python只是包装了执行该功能的函数。
IIRC (and I've probably got a few of the details wrong…), the basic idea was this: You'd make a BIOS call to get hi and lo values for the keyboard, meaning every key had a 16-bit value. Turbo C, to make things easier, provided a nice call that would map keys to their character in the current (8-bit) codepage, so a
would give you back a single byte, 0x61
. But there wasn't enough room to map everything, so special keys like VK_UP
would be returned unmapped, across two separate calls, first the hi byte 0xE0
and then the lo byte 0x50
. Microsoft copied that from Borland to make it easier to port code to their compiler, and then to port code from DOS/Win3.1 to NT, and so that's what MSVCRT still does today, and Python is just wrapping the function that does that.
此答案提供了更多详细信息。
This answer gives some more details.
当然,在大多数Unix终端上,向上箭头也会发送一个字符序列,通常与该字符的光标向上显示控制序列相同,通常为 ESC
,然后 [
,然后是 A
。但是人们不会期望像
Of course on most Unix terminals, the up arrow also send a sequence of characters, usually the same as the cursor-up display control sequence for that character, typically ESC
, then [
, then A
. But people don't expect a single-character getch
-like function to return a single value for every key on Unix, so nobody gets confused.
这篇关于msvcrt.getch()方法中的\xe0是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!