Python curses get keypress

Solution for Python curses get keypress
is Given Below:

I need a way to detect both the arrow keys and the enter key. I don’t have a numpad on my computer, so this statement is looking for a key that doesn’t exist:

char = window.getch()
if char == curses.KEY_ENTER:
    stdscr.addstr("'enter' key pressed")

I would just use this to get the keypress:

char = window.getkey()
if char == "n":
    stdscr.addstr("'enter' key pressed")

but I also have to get the arrow keys, with the getch() function. Is there any way I could use these two functions together, or another way that can get both keys that I haven’t thought of?

You could look at using the keyboard module, installed with pip install keyboard.

Something like the following could get you going, adapted from the modules GitHub example:

import keyboard

def print_pressed_keys(e):
    if e.event_type == "down":
        keys = [keyboard._pressed_events[name].name for name in keyboard._pressed_events]
        if "up" in keys:
            print("do stuff for up pressed")
        elif "enter" in keys:
            print("do stuff for enter pressed")

Black Thunder gives a good detailed description of how to use the module here.