Wszystko pięknie i ładnie jeżeli chodzi o "msvcrt" w Windows
Ale kiedy zaczynam broić w Linuksie, mam problem z pobraniem znaków specjalnych pomijając <Enter> oraz <Bakspace> który w kodzie ASCII jest DELETEM.
Działał mi DEL kiedy to sprawdziłem że jego kod po zwróceniu to "[3~", ale klaiwsze <UP> <DOWN> zwracały czasem "A", "B", czasem "[A", "[B", i instrukcje warunkowe nie mogły ich wychwycić, tak samo mieszały trochę przy odczycie (kasowały mi tekst i przechodziły na początek linijki.
Kod użyłem taki samych jak pod Windowsem... ale obecnie zastąpiłem część linuxową pass
Oto mój kod:
################################################################################
import os
import platform
import sys
system = platform.system();
if system == "Windows": import msvcrt;
if system == "Linux": import sys, tty, termios;
################################################################################
class bufferedIput:
""" """
system = platform.system();
## =========================================================================
def __init__( self ):
""" """
self.bufforHist = [ ];
self.position = 0;
## =========================================================================
def __getch( self ):
""" """
if ( bufferedIput.system == "Linux" ):
fd = sys.stdin.fileno();
old_settings = termios.tcgetattr(fd);
try:
tty.setraw(sys.stdin.fileno());
input0 = sys.stdin.read(1);
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings);
else:
input0 = msvcrt.getch();
return input0;
## =========================================================================
def __getchMore( self ):
""" """
if ( bufferedIput.system == "Linux" ):
fd = sys.stdin.fileno();
old_settings = termios.tcgetattr(fd);
try:
tty.setraw(sys.stdin.fileno());
input0 = sys.stdin.read(3);
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings);
else:
input0 = msvcrt.getch();
return input0;
## =========================================================================
def chinput( self, preInput = "" ):
""" """
buffor = "";
sys.stdout.write( preInput );
while ( True ):
character = '';
character = self.__getch();
# ( 003 - "CTRL+C" ETX end of text )
if ( ord(character) == 3 ):
raise Exception( "'CTRL+C' has been called" );
# ( 008 - backspace ) Windows
# ( 127 - del ) Linux
if ( ord(character) == 8 or ord(character) == 127 ):
if ( len(buffor) > 0 ):
sys.stdout.write( "\b \b" );
buffor = buffor[:-1];
continue;
# ( 013 - carriage return )
elif ( ord(character) == 13 ):
sys.stdout.write( "\n" );
break;
# ( 224 - special character ) Windows
elif ( bufferedIput.system == "Windows" and ord(character) == 224 ):
character = self.__getchMore();
# ( 072 - up arrow )
if ( ord(character) == 72 ):
if ( self.position > 0 ):
sys.stdout.write( "\b \b"*len(buffor) );
self.position -= 1;
buffor = self.bufforHist[ self.position ];
sys.stdout.write( buffor );
else:
pass;
# ( 080 - down arrow )
elif ( ord(character) == 80 ):
if ( self.position < len(self.bufforHist)-1 ):
sys.stdout.write( "\b \b"*len(buffor) );
self.position += 1;
buffor = self.bufforHist[ self.position ];
sys.stdout.write( buffor );
else:
pass;
# ( 083 - delete )
elif ( ord(character) == 83 ):
sys.stdout.write( "\b \b"*len(buffor) );
buffor = "";
# ( 027 - special character as DEL ) Linux
elif ( bufferedIput.system == "Linux" and ord(character) == 27 ):
pass;
else:
sys.stdout.write( character );
buffor += character;
self.bufforHist.append( buffor );
self.position = len( self.bufforHist );
return buffor;