How to detect curses ALT + key combinations in python

喜你入骨 提交于 2019-12-22 07:55:14

问题


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)

回答1:


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)


来源:https://stackoverflow.com/questions/22362076/how-to-detect-curses-alt-key-combinations-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!