3

The getch() method returns different values for the main window and pads if the key pressed is non-ASCII. For example, pressing the arrow keys I get the expected KEY_UP, KEY_DOWN etc in the main window, but in the pad I get 65 for the up arrow and 66 for the down arrow. Why is this, and is there a way to get larger than 255 values for special keys in a pad?

I am using Python 2.6.5.

The following code demonstrates the issue:

import curses

def main(stdscr):
    c = None
    while c != curses.KEY_RIGHT:
        c = stdscr.getch()
        stdscr.addstr(0, 0, "%3d" % c)
        stdscr.refresh()
    pad = curses.newpad(20, 20)
    while True:
        c = pad.getch()
        pad.addstr(0, 0, "%3d" % c)
        pad.refresh(0, 0, 1, 0, 20, 20)

if __name__ == '__main__':
    curses.wrapper(main)
Bjorn
  • 1,090
  • 8
  • 12

2 Answers2

7

Did you try pad.keypad(1)

This is the first time I need to deal with curses in Python, and I had the same problem this morning.

Matthew
  • 86
  • 2
  • 3
  • Yep, setting `window.keypad(1)` makes sure curses interprets escape sequences for you. http://docs.python.org/3.4/library/curses.html#curses.window.keypad – chtenb Mar 17 '14 at 22:33
  • 1
    Is there anyway to automatically set this for all windows? – augurar Dec 02 '14 at 06:46
1

I don't have a direct answer to your question, but I do observe that 65 and 66 are the ASCII values of 'A' and 'B', which happen to be the CSI or SS3 commands used by the Up and Down arrow.

LeoNerd
  • 8,344
  • 1
  • 29
  • 36
  • Thanks @LeoNerd, that gets me on the way to do further investigation. I was not familiar with the terms CSI and SS3 but found that "CSI is ESC followed by left bracket ([) on a 7-bit connection or decimal 155 on an 8-bit connection, and SS3 is ESC followed by O (uppercase letter O) on a 7-bit connection and decimal 143 on an 8-bit connection." I would have clicked "This answer is useful", but I don't have enough reputation. – Bjorn Oct 10 '12 at 12:36