How to reveal curses of ALT + keyboard shortcuts in python

New to python here and using curses import. I want to detect keyboard shortcuts like ALT+ F, etc. I am currently using getch()to get the key and then print it in a curses window. The value does not change for For ALT+ F. How to define keyboard shortcuts ALT?

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)
+4
source share
1 answer

Try the following:

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)
+3
source

Source: https://habr.com/ru/post/1531423/


All Articles