python msvcrt,Python msvcrt.getwche方法代码示例

# 需要导入模块: import msvcrt [as 别名]

# 或者: from msvcrt import getwche [as 别名]

def getchar(echo):

# The function `getch` will return a bytes object corresponding to

# the pressed character. Since Windows 10 build 1803, it will also

# return \x00 when called a second time after pressing a regular key.

#

# `getwch` does not share this probably-bugged behavior. Moreover, it

# returns a Unicode object by default, which is what we want.

#

# Either of these functions will return \x00 or \xe0 to indicate

# a special key, and you need to call the same function again to get

# the "rest" of the code. The fun part is that \u00e0 is

# "latin small letter a with grave", so if you type that on a French

# keyboard, you _also_ get a \xe0.

# E.g., consider the Up arrow. This returns \xe0 and then \x48. The

# resulting Unicode string reads as "a with grave" + "capital H".

# This is indistinguishable from when the user actually types

# "a with grave" and then "capital H".

#

# When \xe0 is returned, we assume it's part of a special-key sequence

# and call `getwch` again, but that means that when the user types

# the \u00e0 character, `getchar` doesn't return until a second

# character is typed.

# The alternative is returning immediately, but that would mess up

# cross-platform handling of arrow keys and others that start with

# \xe0. Another option is using `getch`, but then we can't reliably

# read non-ASCII characters, because return values of `getch` are

# limited to the current 8-bit codepage.

#

# Anyway, Click doesn't claim to do this Right(tm), and using `getwch`

# is doing the right thing in more situations than with `getch`.

if echo:

func = msvcrt.getwche

else:

func = msvcrt.getwch

rv = func()

if rv in (u'\x00', u'\xe0'):

# \x00 and \xe0 are control characters that indicate special key,

# see above.

rv += func()

_translate_ch_to_exc(rv)

return rv

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值