2014-03-12 19 views
4

這裏是python的新手,並使用curses導入。我想檢測像ALT + F和類似的組合鍵。目前,我使用getch()接收密鑰,然後將其打印在curses窗口中。該值不會更改爲FALT + F。如何檢測ALT組合鍵?如何在Python中檢測curses 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) 

回答

3

試試這個:

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) 
+2

OP,^這個是我見過的最好的方法。否則,你可能不喜歡他們說什麼,但結帳http://stackoverflow.com/questions/9750588/how-to-get-ctrl-shift-or-alt-with-getch-ncurses 祝你好運! – wbt11a

+0

也許getch()不是要使用的正確函數調用。我記得它對於C語言中的某些東西來說相當有限。也許更好的問題是如何閱讀原始掃描代碼...此外,該示例似乎並沒有粘貼。嗯.. – wufoo

+0

其實代碼工作正常粘貼英寸我的錯誤。我期待它能夠打印所有按鍵,而不僅僅是ALT +組合。謝謝! – wufoo

相關問題