How to get input as a Left Arrow key?

前端 未结 3 2005
心在旅途
心在旅途 2020-12-19 14:42

I want to get the left and right arrow key as a one of the input. How can i get it? Is there any way?

3条回答
  •  太阳男子
    2020-12-19 15:19

    That's how you can do it with ncurses. Unlike TkInter, X11 is not required.

    #! /usr/bin/python
    
    import curses
    
    screen = curses.initscr()
    try:
        curses.noecho()
        curses.curs_set(0)
        screen.keypad(1)
        screen.addstr("Press a key")
        event = screen.getch()
    finally:
        curses.endwin()
    
    if event == curses.KEY_LEFT:
        print("Left Arrow Key pressed")
    elif event == curses.KEY_RIGHT:
        print("Right Arrow Key pressed")
    else:
        print(event)
    

提交回复
热议问题