admin管理员组

文章数量:1531696

显而易见的答案是:

someFunction = string.upper

ord('a') != ord('A') # 97 != 65

someFunction('a') == someFunction('A') # a_code == A_code

或者,换句话说:

char_from_user = getch().upper() # read a char converting to uppercase

if char == 'Q':

# quit

exit = True # or something

elif char in ['A', 'K']:

do_something()

等。。。

下面是getch函数的一个实现,它可以在Windows和Linux平台上工作,

based on this recipe

:

class _Getch(object):

"""Gets a single character from standard input.

Does not echo to the screen."""

def __init__(self):

try:

self.impl = _GetchWindows()

except ImportError:

self.impl = _GetchUnix()

def __call__(self):

return self.impl()

class _GetchUnix(object):

def __init__(self):

import tty, sys

def __call__(self):

import sys, tty, termios

fd = sys.stdin.fileno()

old_settings = termios.tcgetattr(fd)

try:

tty.setraw(sys.stdin.fileno())

ch = sys.stdin.read(1)

finally:

termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)

return ch

class _GetchWindows(object):

def __init__(self):

import msvcrt

def __call__(self):

import msvcrt

return msvcrt.getch()

getch = _Getch()

本文标签: 密钥代码Python秘钥