Search code examples
pythoncursespython-curses

Cannot stop arrows keys from echoing to the console in Python with the curses library


Below is my code. I'm trying to make a simple text editor and I tried to make sure that arrows keys wouldn't print to the console but it still does. Any help pls?

import curses

text = []


def main(screen):
    curses.curs_set(1)
    curses.noecho()

    while True:
        newChar = screen.getkey()

        if newChar != curses.KEY_LEFT or curses.KEY_RIGHT or curses.KEY_UP or curses.KEY_DOWN:
            text.append(newChar)

        screen.addstr(0, 0, "".join(text))
        screen.refresh()


curses.wrapper(main)```

Solution

  • Oof, I figured out the problem. Issue with comparing values in the if statement. Resolved my own problem. yippee

    import curses
    
    text = []
    
    
    def main(screen):
            curses.curs_set(1)
    
            while True:
                newChar = screen.getch()
    
                if newChar not in [curses.KEY_LEFT, curses.KEY_RIGHT, curses.KEY_UP, curses.KEY_DOWN]:
                    screen.addstr(0, 0, curses.keyname(newChar))
                    screen.refresh()
    
    
    curses.wrapper(main)