gpt4 book ai didi

python - OSX iTerm2 可以将鼠标与 python-ncurses 一起使用,但不能与 C 中的curses一起使用

转载 作者:行者123 更新时间:2023-11-30 20:13:36 27 4
gpt4 key购买 nike

我正在考虑为用 C 编写的小型编辑器提供鼠标支持。该编辑器是围绕 ncurses 构建的,所以我认为这应该是可能的。我从另一篇文章中获得了这个 Python 源代码,它运行:

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;
}
}

这不会运行。显然,初始化的东西是正确的,但我开始不理解这个世界了:(有什么帮助吗?

最佳答案

C 语言的示例正在从 menu_win 读取输入(调用 wgetch(menu_win) ),但您没有调用

keypad(menu_win, TRUE);

之前,启用特殊键。在 ncurses 中,KEY_MOUSE如果您不启用此模式,则不会返回。

wgetch 手册页说

The following function keys, defined in <curses.h>, might be returned by getch if keypad has been enabled. Note that not all of these are necessarily supported on any particular terminal.

关于python - OSX iTerm2 可以将鼠标与 python-ncurses 一起使用,但不能与 C 中的curses一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29521053/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com