📄 第十一章:键盘管理——如何读取功能键和方向键等等.htm
字号:
style='font-family:Tahoma'><span style='mso-tab-count:1'> </span>noecho();<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>cbreak();<spanstyle='mso-tab-count:1'> </span>/*</span></b><bstyle='mso-bidi-font-weight:normal'><span style='font-family:宋体;mso-ascii-font-family:Tahoma;mso-hansi-font-family:"Times New Roman";mso-bidi-font-family:Tahoma'>禁用行缓冲,传递所有输入</span></b><bstyle='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'>*/<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>startx= (80 - WIDTH) / 2;<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>starty= (24 - HEIGHT) / 2;<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:2'> </span><o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>menu_win= newwin(HEIGHT, WIDTH, starty, startx);<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>keypad(menu_win,TRUE);<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>mvprintw(0,0,"</span></b><span lang=EN-US style='font-family:Tahoma'>Use arrow keysto go up and down, Press enter to select a choice<b style='mso-bidi-font-weight:normal'>");<o:p></o:p></b></span></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>refresh();<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>print_menu(menu_win,highlight);<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>while(1)<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>{<spanstyle='mso-tab-count:1'> </span>c = wgetch(menu_win);<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:2'> </span>switch(c)<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:2'> </span>{<spanstyle='mso-tab-count:1'> </span>case KEY_UP:<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:3'> </span>if(highlight== 1)<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:5'> </span>highlight= n_choices;<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:4'> </span>else<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:5'> </span>--highlight;<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:4'> </span>break;<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:3'> </span>caseKEY_DOWN:<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:4'> </span>if(highlight== n_choices)<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:5'> </span>highlight= 1;<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:4'> </span>else<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:5'> </span>++highlight;<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:4'> </span>break;<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:3'> </span>case10:<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:4'> </span>choice= highlight;<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:4'> </span>break;<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:3'> </span>default:<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:3'> </span>mvprintw(24,0, "</span></b><span lang=EN-US style='font-family:Tahoma'>Charcterpressed is = %3d Hopefully it can be printed as '%c<b style='mso-bidi-font-weight:normal'>'", c, c);<o:p></o:p></b></span></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:4'> </span>refresh();<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:4'> </span>break;<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:2'> </span>}<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:2'> </span>print_menu(menu_win,highlight);<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:2'> </span>if(choice!= 0)<span style='mso-tab-count:1'> </span>/*</span></b><bstyle='mso-bidi-font-weight:normal'><span style='font-family:宋体;mso-ascii-font-family:Tahoma;mso-hansi-font-family:"Times New Roman";mso-bidi-font-family:Tahoma'>用户必须要做一个选择</span></b><bstyle='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'>*/<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:3'> </span>break;<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>}<spanstyle='mso-tab-count:1'> </span><o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>mvprintw(23,0, "You chose choice %d with choice string %s\n", choice,choices[choice - 1]);<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>clrtoeol();<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>refresh();<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>endwin();<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>return0;<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'>}<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'>void print_menu(WINDOW *menu_win, int highlight)<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'>{<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>intx, y, i;<span style='mso-tab-count:1'> </span><o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>x= 2;<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>y= 2;<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>box(menu_win,0, 0);<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>for(i= 0; i < n_choices; ++i)<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>{<spanstyle='mso-tab-count:1'> </span>if(highlight == i + 1)/*</span></b><span style='font-family:宋体;mso-ascii-font-family:Tahoma;mso-hansi-font-family:Tahoma;mso-bidi-font-family:Tahoma'>使选择的字符串高亮</span><bstyle='mso-bidi-font-weight:normal'><span lang=EN-US style='font-family:Tahoma'>*/<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:2'> </span>{<spanstyle='mso-tab-count:1'> </span>wattron(menu_win,A_REVERSE); <o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:3'> </span>mvwprintw(menu_win,y, x, "</span></b><span lang=EN-US style='font-family:Tahoma'>%s<bstyle='mso-bidi-font-weight:normal'>", choices[i]);<o:p></o:p></b></span></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:3'> </span>wattroff(menu_win,A_REVERSE);<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:2'> </span>}<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:2'> </span>else<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:3'> </span>mvwprintw(menu_win,y, x, "%s", choices[i]);<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:2'> </span>++y;<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>}<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'><span style='mso-tab-count:1'> </span>wrefresh(menu_win);<o:p></o:p></span></b></p><p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span lang=EN-USstyle='font-family:Tahoma'>}<o:p></o:p></span></b></p></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -