-1

I have this sample code where I write on the screen if the enter key is pressed but when I run the code nothing happens and nothing is written on the screen. I know I can use stdscr.getch(), but for some reason I don't want to use them.

import curses

def main(stdscr):
    stdscr.keypad(True)
    while 1:
        Key = stdscr.getkey()
        if Key == curses.KEY_ENTER:
            stdscr.addstr(0,0,'u pressed enter')
            stdscr.refresh()

curses.wrapper(main)
martineau
  • 119,623
  • 25
  • 170
  • 301
Amey patil
  • 25
  • 1
  • 4
  • maybe first use `print(Key, curses.KEY_ENTER)` to see what you get when you press key and compare it with `curses.KEY_ENTER`. Maybe you have to compare `Key` with different value - maybe some `KEYPAD_ENTER`. OR maybe `getkey()` wasn't created for `ENTER` – furas Apr 24 '21 at 15:55
  • on my computer with Linux I have to use `if Key == '\n':` or `if ord(Key) == 10:` – furas Apr 24 '21 at 16:00
  • "but for some reason I don't want to use them" — why is that? – martineau Apr 24 '21 at 16:31
  • @martineau I tested it and `getch()` always gives key code (integer) so you have to compare with `ord("a")` or `97`. With `getkey()` you can compare directly with `"a"`. But better is `get_wch()` - it also gives `"a"` instead of `97` and only `get_wch` works with native chars. I added testing code in answer. – furas Apr 24 '21 at 17:56
  • Does this answer your question? [How to detect Return or Enter key properly when using Curses](https://stackoverflow.com/questions/45536217/how-to-detect-return-or-enter-key-properly-when-using-curses) – Thomas Dickey Apr 25 '21 at 10:12

1 Answers1

1

On my computer with Linux I have to use

if Key == '\n': 

or

if ord(Key) == 10:

It seems getkey() doesn't treat ENTER as special key and it doesn't return curses.KEY_ENTER. OR maybe it depens on terminal - some of then may have option to define code for ENTER and/or BACKSPACE.

EDIT:

I found out that for special keys getkey() gives me strings like "KEY_LEFT" instead of integer value curses.KEY_LEFT. But get_wch() gives integer value curses.KEY_LEFT (and char for normal keys) - but it still treats ENTER as '\n'


You should simply use print() to check what you get in variables.

print(Key, type(Key))

and when you see it is <class str>

print( ord(Key) )

You could also compare it with constant

print( Key, curses.KEY_ENTER, Key == curses.KEY_ENTER )

EDIT:

I tested it with this code on Linux Mint 20 MATE, Python 3.8, in Mate-Terminal.

import curses

def main(stdscr):

    while True:
        #key = stdscr.getch()     # always integer (keycode), native/Polish char WRONG (two wrong integers)
        #key = stdscr.getkey()    # char or string (keyname), native/Polish char WRONG (two wrong chars)
        key = stdscr.get_wch()   # char or integer (keycode), native/Polish char OK
        
        print('key:', type(key), key, end='\n\r')
        
        if isinstance(key, str):
            print('len:', len(key), end='\n\r')
            if len(key) == 1:            
                print('ord:', ord(key), end='\n\r')
        else:
            print('keyname:', curses.keyname(key), end='\n\r')

        print('---', end='\n\r')

        stdscr.refresh()

# --- main ---

#print('curses.KEY_ENTER:', curses.KEY_ENTER)
#print('curses.KEY_BACKSPACE:', curses.KEY_BACKSPACE)

curses.wrapper(main)

For me the best is get_wch() because it works correctly with native (Polish) chars.

getch()   - always integer (keycode),  native/Polish char WRONG (two wrong integers)
getkey()  - char or string (keyname),  native/Polish char WRONG (two wrong chars)
get_wch() - char or integer (keycode), native/Polish char OK
        

EDIT:

For key F1 it needs

  • string "KEY_F(1)" for getkey()

    getkey() == "KEY_F(1)"
    
  • integer 265 or curses.KEY_F1 for get_wch() and getch()

    get_wch() == 265
    get_wch() == curses.KEY_F1
    
    getch() == 265
    getch() == curses.KEY_F1
    
furas
  • 134,197
  • 12
  • 106
  • 148
  • yup thanks for the great explanation i used '\n' and its works perfectly fine thanks – Amey patil Apr 25 '21 at 01:35
  • btw how can i read the f1 - f12 keys using this method ? – Amey patil Apr 25 '21 at 02:27
  • if you run my code and press `F1` then you see `key: 265 `, and `keyname: KEY_F(1)` . If you use `getkey` (which send special keys as string) then you have to compare with string `getkey() == "KEY_F(1)"`, if you use `getch` or `get_wch` (which send special keys as integer) then you have to compare with integer `get_wch() == 265` but you can use also `get_wch() == curses.KEY_F1` – furas Apr 25 '21 at 03:58
  • yeah its working but i dont know why it doesnt work on my stock terminal when i use other terminal its works perfectly fine – Amey patil Apr 27 '21 at 05:06
  • as I know long time ago there was many different terminals (hardware) and current terminals (software) can emulate different versions. You could use my program to see codes when you press `F1`. Maybe it helps to see what codes you have to use in program. – furas Apr 27 '21 at 05:31
  • yeah no worries i am now using xterm and its works perfectly fine – Amey patil Apr 27 '21 at 07:39