Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect curses ALT + key combinations in python

Tags:

python

curses

New to python here and using the curses import. I want to detect key combinations like ALT+F and similar. Currently, I am using getch() to receive a key and then printing it in a curses window. The value does not change for F or ALT+F. How can I detect the ALT key combinations?

import curses

def Main(screen):
   foo = 0
   while foo == 0: 
      ch = screen.getch()
      screen.addstr (5, 5, str(ch), curses.A_REVERSE)
      screen.refresh()
      if ch == ord('q'):
         foo = 1

curses.wrapper(Main)
like image 535
wufoo Avatar asked Mar 12 '14 19:03

wufoo


People also ask

What is the curses module Python?

What is curses? ¶ The curses library supplies a terminal-independent screen-painting and keyboard-handling facility for text-based terminals; such terminals include VT100s, the Linux console, and the simulated terminal provided by various programs.


1 Answers

Try this:

import curses

def Main(screen):
   while True:
      ch = screen.getch()
      if ch == ord('q'):
         break
      elif ch == 27: # ALT was pressed
         screen.nodelay(True)
         ch2 = screen.getch() # get the key pressed after ALT
         if ch2 == -1:
            break
         else:
            screen.addstr(5, 5, 'ALT+'+str(ch2))
            screen.refresh()
         screen.nodelay(False)

curses.wrapper(Main)
like image 182
Wojciech Walczak Avatar answered Oct 23 '22 17:10

Wojciech Walczak