This may be an xy problem, but I'm trying to to build a kernel based text editor, similar to vim
or nano
, and I know how to use the escape chars to clear the screen, then reprint, I can have it accept characters, but I'm not sure how to get it to accept arrow inputs for navigation. I thought there were ASCII values for them, but apparently not. Is there a way to use the arrows, or do I have to make a navigation mode and insert mode like vim
?
I've also briefly played with curses
, but that was prohibitive because, as I understood, a whole new window had to be opened for it and this is not compatible with the vision of a single terminal window that I had.
Please note that curses
does not work because it cleares the window, which I don't want.
curses
is exactly what you want. In fact I believe vim implements its interface with curses.
Try to put the following code into a file called test_curses.py
:
import curses
screen = curses.initscr()
screen.addstr("Hello World!!!")
screen.refresh()
screen.getch()
curses.endwin()
Now open a terminal (not IDLE! a real terminal!) and run it via:
python test_curses.py
You should see that the terminal was cleared and an Hello World!!!
writing appeared. Press any key and the program will stop, restoring the old terminal contents.
Note that the curses
library isn't as easy and "user-friendly" as you may be accustomed to. I suggest reading the tutorial (unfortunately for the C language, but the python interface is mostly the same)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With