I have a function, and it breaks the built-in input() function in some way.
getKeyCode() returns the keycode of the key being pressed. It functions well, both for blocking=True and blocking=False.
def getKeyCode(blocking=True):
fd = sys.stdin.fileno()
oldterm = newattr = termios.tcgetattr(fd)# I need this line unchanged, please!
if blocking:
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
if not blocking:
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
try:
return ord(sys.stdin.read(1))
except:
return len(sys.stdin.read(1))
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
if not blocking:
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
termios.tcsetattr(fd, termios.TCSADRAIN, oldterm)
now I try calling the getKeyCode() function, and then I need to use the input() function:
print(getKeyCode(False)) # this line works, printing the keycode of the key pressed at that instant
a = input('Input something here: ')
print('\nYou inputted: '+a)
If you run the code I just wrote, you would find out that while you're inputting your input, nothing seems to happen. However, after pressing enter, the program tells you that it has received your input correctly.
I'll admit that I have no idea how most of the getKeyCode() function works.
But I really need help on how to make it stop affecting the input() function.