OSX iTerm2可以将鼠标用于python ncurses,但不能用于C语言中的诅咒
我正在考虑为一个用C编写的小编辑器提供鼠标支持。该编辑器是围绕ncurses构建的,所以我认为这应该是可能的。我从另一篇文章中获得了这个Python源代码,它运行:OSX iTerm2可以将鼠标用于python ncurses,但不能用于C语言中的诅咒,python,c,ncurses,Python,C,Ncurses,我正在考虑为一个用C编写的小编辑器提供鼠标支持。该编辑器是围绕ncurses构建的,所以我认为这应该是可能的。我从另一篇文章中获得了这个Python源代码,它运行: import curses from contextlib import contextmanager @contextmanager def open_screen(): scr = curses.initscr() curses.noecho() curses.cbreak() scr.keyp
import curses
from contextlib import contextmanager
@contextmanager
def open_screen():
scr = curses.initscr()
curses.noecho()
curses.cbreak()
scr.keypad(1)
try:
yield scr
finally:
curses.nocbreak()
scr.keypad(0)
curses.echo()
curses.endwin()
curses_mouse_states = {
curses.BUTTON1_PRESSED: 'Button 1 Pressed',
curses.BUTTON1_RELEASED: 'Button 1 Released',
curses.BUTTON1_CLICKED: 'Button 1 Clicked',
curses.BUTTON1_DOUBLE_CLICKED: 'Button 1 Double-Clicked',
curses.BUTTON1_TRIPLE_CLICKED: 'Button 1 Triple-Clicked',
curses.BUTTON2_PRESSED: 'Button 2 Pressed',
curses.BUTTON2_RELEASED: 'Button 2 Released',
curses.BUTTON2_CLICKED: 'Button 2 Clicked',
curses.BUTTON2_DOUBLE_CLICKED: 'Button 2 Double-Clicked',
curses.BUTTON2_TRIPLE_CLICKED: 'Button 2 Triple-Clicked',
curses.BUTTON3_PRESSED: 'Button 3 Pressed',
curses.BUTTON3_RELEASED: 'Button 3 Released',
curses.BUTTON3_CLICKED: 'Button 3 Clicked',
curses.BUTTON3_DOUBLE_CLICKED: 'Button 3 Double-Clicked',
curses.BUTTON3_TRIPLE_CLICKED: 'Button 3 Triple-Clicked',
curses.BUTTON4_PRESSED: 'Button 4 Pressed',
curses.BUTTON4_RELEASED: 'Button 4 Released',
curses.BUTTON4_CLICKED: 'Button 4 Clicked',
curses.BUTTON4_DOUBLE_CLICKED: 'Button 4 Double-Clicked',
curses.BUTTON4_TRIPLE_CLICKED: 'Button 4 Triple-Clicked',
curses.BUTTON_SHIFT: 'Button Shift',
curses.BUTTON_CTRL: 'Button Ctrl',
curses.BUTTON_ALT: 'Button Alt'
}
with open_screen() as scr:
curses.mousemask(-1)
while True:
c = scr.getch()
if c == curses.KEY_MOUSE:
gmouse = curses.getmouse()
mouse_state = gmouse[4]
states = '; '.join(state_string for state, state_string
in curses_mouse_states.viewitems()
if mouse_state & state)
scr.addstr(0, 0, states)
scr.clrtoeol()
scr.addstr(1, 0, "x = %3d" % gmouse[1] )
scr.addstr(2, 0, "y = %3d" % gmouse[2] )
scr.refresh()
elif c == ord('q'):
break
但是,我有C语言的源代码:
#include <ncurses.h>
#include <string.h>
#define WIDTH 30
#define HEIGHT 10
int startx = 0;
int starty = 0;
char *choices[] = { "Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
};
int n_choices = sizeof(choices) / sizeof(char *);
void print_menu(WINDOW *menu_win, int highlight);
void report_choice(int mouse_x, int mouse_y, int *p_choice);
int main()
{ int c, choice = 0;
WINDOW *myscreen, *menu_win;
MEVENT event;
mmask_t oldmask;
/* Initialize curses */
myscreen = initscr();
clear();
noecho();
cbreak(); //Line buffering disabled. pass on everything
keypad(myscreen,TRUE);
/* Try to put the window in the middle of screen */
startx = (80 - WIDTH) / 2;
starty = (24 - HEIGHT) / 2;
attron(A_REVERSE);
mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)");
refresh();
attroff(A_REVERSE);
/* Print the menu for the first time */
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
print_menu(menu_win, 1);
/* Get all the mouse events */
mousemask(ALL_MOUSE_EVENTS, &oldmask);
while(1)
{
c = wgetch(menu_win);
switch(c) {
case KEY_MOUSE:
if(getmouse(&event) == OK)
{ /* When the user clicks left mouse button */
mvprintw(21,1,"Event: x = %3d y = %3d",event.x,event.y);
if(event.bstate & BUTTON1_PRESSED)
{ report_choice(event.x + 1, event.y + 1, &choice);
if(choice == -1) //Exit chosen
goto end;
mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]);
refresh();
}
} else {
mvprintw(21,1,"Couldn't get event!");
}
print_menu(menu_win, choice);
break;
case 'Q':
case 'q':
goto end;
default:
break;
}
}
end:
mousemask(oldmask,&oldmask);
nocbreak();
keypad(myscreen,FALSE);
echo();
endwin();
return 0;
}
void print_menu(WINDOW *menu_win, int highlight)
{
int x, y, i;
x = 2;
y = 2;
box(menu_win, 0, 0);
for(i = 0; i < n_choices; ++i)
{ if(highlight == i + 1)
{ wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", choices[i]);
wattroff(menu_win, A_REVERSE);
}
else
mvwprintw(menu_win, y, x, "%s", choices[i]);
++y;
}
wrefresh(menu_win);
}
/* Report the choice according to mouse position */
void report_choice(int mouse_x, int mouse_y, int *p_choice)
{ int i,j, choice;
i = startx + 2;
j = starty + 3;
for(choice = 0; choice < n_choices; ++choice)
if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice]))
{ if(choice == n_choices - 1)
*p_choice = -1;
else
*p_choice = choice + 1;
break;
}
}
#包括
#包括
#定义宽度30
#定义高度10
int startx=0;
int starty=0;
字符*选项[]={“选项1”,
“选择2”,
“选择3”,
“选择4”,
“退出”,
};
int n_choices=sizeof(choices)/sizeof(char*);
无效打印菜单(窗口*菜单,整数高亮显示);
无效报告选项(int mouse\u x、int mouse\u y、int*p\u选项);
int main()
{int c,choice=0;
窗口*myscreen,*菜单\u win;
MEVENT事件;
mmask_t旧面具;
/*初始化诅咒*/
myscreen=initscr();
清除();
noecho();
cbreak();//禁用行缓冲。传递所有内容
键盘(myscreen,TRUE);
*试着把窗口放在屏幕中间*
startx=(80-宽度)/2;
星形=(24-高度)/2;
attron(A_反向);
mvprintw(23,1,“单击退出退出(在虚拟控制台中效果最好)”;
刷新();
阿特洛夫(A_反向);
/*第一次打印菜单*/
菜单_win=newwin(高度、宽度、起点、起点);
打印菜单(菜单赢,1);
/*获取所有鼠标事件*/
鼠标掩码(所有鼠标事件和旧掩码);
而(1)
{
c=wgetch(菜单\赢);
开关(c){
大小写键\鼠标:
if(getmouse(&event)==OK)
{/*当用户单击鼠标左键时*/
mvprintw(21,1,“事件:x=%3d y=%3d”,事件.x,事件.y);
如果(event.bstate和按钮1_按下)
{report_choice(event.x+1、event.y+1和choice);
如果(选项==-1)//选择退出
转到终点;
mvprintw(22,1,“所做的选择是:%d所选字符串是\%10s\”,选择,选择[Choice-1]);
刷新();
}
}否则{
mvprintw(21,1,“无法获取事件!”);
}
打印菜单(菜单赢,选择);
打破
案例‘Q’:
案例‘q’:
转到终点;
违约:
打破
}
}
完:
鼠标掩码(oldmask和oldmask);
nocbreak();
键盘(myscreen,FALSE);
回声();
endwin();
返回0;
}
无效打印菜单(窗口*菜单,整数高亮显示)
{
int x,y,i;
x=2;
y=2;
框(菜单,0,0);
对于(i=0;i 如果(mouse_y==j+choice&&mouse_x>=i&&mouse_xC中的示例是从菜单中读取输入(调用wgetch(menu_win)
),但您没有调用
keypad(menu_win, TRUE);
之前,启用特殊键。在ncurses中,如果不启用此模式,则不会返回键\鼠标
主页上说
在
中定义的以下功能键可能
如果已启用键盘,则由getch返回。注意
并非所有这些都必须在任何
特定终端
“这不会运行”没有帮助。确切的问题是什么?代码是否编译?编译过程中是否存在任何错误/警告?如果编译,则运行二进制文件时会发生什么?是否显示任何内容?是否尝试过调试?请发布并尽可能多地添加有关此问题的信息。提供的信息远远不够d、 非常感谢,我在第一步混合了太多东西:-)