-1

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.

Droid-serif
  • 5
  • 1
  • 2
  • "*after pressing enter, the program tells you that it has received your input*" -- See https://stackoverflow.com/questions/27214759/why-characters-received-in-serial-connection-only-after-pressing-enter – sawdust May 18 '22 at 03:22
  • Not sure if that helps me? I think for some reason termios has taken control of the console and maybe someone who knows termios can help. I'm still happy I could get help, even if nobody hardly ever sees my post. – Droid-serif May 19 '22 at 02:26

0 Answers0