4

I am using Python's curses module. In stdscr, whenever I press enter key, curse moves to the first column in the same line. I have couple of questions regarding it.

  1. What is the reason for that?

  2. Is there a way to move the curse to the next line?

  3. If I want to do certain things (execute some function or something) on enter key press, then what will come in 'if' condition? e.g.

    if (condition which will determine if ENTER was pressed or not)
        # somecode
    
Neuron
  • 5,141
  • 5
  • 38
  • 59
Rob
  • 169
  • 2
  • 4
  • 15
  • Please provide the shortest possible complete program that demonstrates your question. See http://stackoverflow.com/help/mcve for more info about how and why to create a short example. See http://stackoverflow.com/help/asking for more info about how to ask a good question. – Robᵩ Aug 27 '15 at 16:08

1 Answers1

7
  1. What is the reason for that?

You need to invoke curses.noecho() as part of your initialization.

  1. Is there a way to move the curse to the next line?

screen.move(y,x) will move to an absolute location. screen.getyx() will tell you your current location.

  1. If I want to do certain things (execute some function or something) on enter key press, then what will come in 'if' condition? e.g.

You'd think that you could call getch() and compare the result with KEY_ENTER. In practice, you need to check for more values than that. Depending upon your terminal settings, which library you use, and phase of the moon, you may need to check for newline (aka \n, ^J, ASCII 10) or carriage return (\r, ^M, ASCII 13).

c = screen.getch()
if c == curses.KEY_ENTER or c == 10 or c == 13:
    # I hit ENTER

Sample program:

import curses

# Thanks, http://www.ipsum-generator.com
ipsum = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer
nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla
quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent
mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum
lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent
per conubia nostra, per inceptos himenaeos.'''

try:
    # Standard startup. Probably don't need to change this
    screen = curses.initscr()
    curses.cbreak()
    curses.noecho()
    screen.keypad(True)

    # Silly program to write to the screen,
    # wait for either <ENTER> or <Q>.
    # On <ENTER>, mess with the screen.
    # On <Q>, exit.
    screen.addstr(0, 0, ipsum)
    screen.move(0, 0)
    screen.refresh()
    i = 0
    j = 0

    while True:
        c = screen.getch()
        if c == ord('q'):
            exit(0)
        if c == curses.KEY_ENTER or c == 10 or c == 13:
            i += 1
            if i % 3 == 0:
                screen.addstr(0, 0, ipsum.lower())
            if i % 3 == 1:
                screen.addstr(0, 0, ipsum.upper())
            if i % 3 == 2:
                screen.addstr(0, 0, ipsum)
            screen.move(0, 0)
        if c == curses.KEY_DOWN:
            y, x = screen.getyx()
            maxy, maxx = screen.getmaxyx()
            screen.move((y+1) % maxy, x)
        screen.refresh()


finally:
    # Standard shutdown. Probably don't need to change this.
    curses.nocbreak()
    screen.keypad(0)
    curses.echo()
    curses.endwin()

Reference:

Neuron
  • 5,141
  • 5
  • 38
  • 59
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • It worked. I wrote exactly the same code expect "or c==10 or c==13". Why we need to check extra values? Can you please elaborate more on this point. – Rob Aug 27 '15 at 17:35
  • I'm sorry, I don't have a clue why sometimes KEY_ENTER works and sometimes it doesn't. I added some more text to the answer, though. – Robᵩ Aug 27 '15 at 18:30
  • 3
    `KEY_ENTER` refers to the *Enter* key on the numeric keypad, and is unrelated to the *Enter* key on the main qwerty-keyboard. Normally the latter will give a newline (10, `\n`) because that is the Unix convention. Checking for carriage return (13, `\r`) does no harm. – Thomas Dickey Aug 28 '15 at 08:36