![](/img/trans.png)
[英]Moving the cursor to the end of a line of text in ncurses window?
[英]NCURSES - Moving cursor in pad
我正在用ncurses编写应用程序,但无法在Pad中移动光标。 有办法吗? 我认为wmove(Window w, int y, int x)
应该是正确的方法,但我不能那样做...我做错了吗?
int main(void)
{
WINDOW *p;
FILE *f;
int ch;
initscr();
keypad(stdscr, TRUE);
/* create a new pad */
p = newpad(WIDE,COLS);
if( p == NULL )
bomb("Unable to create new pad\n");
/* open the file */
f = fopen(FILENAME,"r");
if( f == NULL)
bomb("Unable to open file\n");
int i =0;
while( (ch=fgetc(f)) != EOF){
waddch(p,ch);
}
fclose(f);
prefresh(p,0, 0, 0,0, LINES-1,COLS-1);
int c, cursorY=0, cursorX=0, linseCount = 0;
wmove(p,2,2);
while((c=wgetch(p))!='q'){
switch(c){
case KEY_LEFT:
if(cursorX != 0)
cursorX--;
break;
case KEY_RIGHT:
if (cursorX != COLS-1)
cursorX++;
break;
case KEY_UP:
if(cursorY==0 && linseCount==0)
break;
else if(cursorY == 0){
linseCount--;
prefresh(p,linseCount, 0, 0,0, LINES-1,COLS-1);
}
else
cursorY--;
break;
case KEY_DOWN:
if(cursorY==LINES-1 && linseCount==WIDE-1)
break;
else if(cursorY == LINES-1){
linseCount++;
prefresh(p,linseCount, 0, 0,0, LINES-1,COLS-1);
}
else
cursorY--;
break;
}
wmove(p,cursorY,cursorX);
prefresh(p,linseCount, 0, 0,0, LINES-1,COLS-1);
}
endwin();
return 0;
}
||| ---------------------编辑-解决方案----------------------- - |||
int main(void)
{
WINDOW *p;
FILE *f;
int ch;
initscr();
savetty();
noecho();//disable auto-echoing
cbreak();//making getch() work without a buffer I.E. raw characters
keypad(stdscr,TRUE);//allows use of special keys, namely the arrow keys
clear(); // empty the screen
timeout(0);
/* create a new pad */
p = newpad(WIDE,COLS);
keypad(p, TRUE);
if( p == NULL )
bomb("Unable to create new pad\n");
/* open the file */
f = fopen(FILENAME,"r");
if( f == NULL)
bomb("Unable to open file\n");
/* display file’s contents on the pad */
while( (ch=fgetc(f)) != EOF)
waddch(p,ch);
fclose(f);
/* display the pad’s contents on the screen */
prefresh(p,0, 0, 0,0, LINES-1,COLS-1);
int c, cursorY=0, cursorX=0, linseCount = 0;
while((c=wgetch(p))!='q'){
switch(c){
case KEY_LEFT:
if(cursorX != 0)
cursorX--;
break;
case KEY_RIGHT:
if (cursorX != COLS-1)
cursorX++;
break;
case KEY_UP:
if(cursorY==0 && linseCount==0)
break;
else if(cursorY == linseCount){
cursorY--;
linseCount--;
prefresh(p,linseCount, 0, 0,0, LINES-1,COLS-1);
}
else
cursorY--;
break;
case KEY_DOWN:
if(cursorY==LINES-1 && linseCount==WIDE-1)
break;
else if(cursorY == linseCount+LINES-1){
cursorY++;
linseCount++;
prefresh(p,linseCount, 0, 0,0, LINES-1,COLS-1);
}
else
cursorY++;
break;
}
wmove(p,cursorY,cursorX);
prefresh(p,linseCount, 0, 0,0, LINES-1,COLS-1);
}
endwin();
return 0;
}
这两行导致光标与变量curserX和curserY认为光标所在的位置不同。
int c, cursorY=0, cursorX=0, linseCount = 0;
wmove(p,2,2);
此代码,在处理key_down箭头键时
if(cursorY==LINES-1 && linseCount==WIDE-1)
正在针对不正确的x宽度检查'linsecount'。
建议从以下方面开始诅咒:
initscr();
savetty();
noecho();//disable auto-echoing
cbreak();//making getch() work without a buffer I.E. raw characters
keypad(stdscr,TRUE);//allows use of special keys, namely the arrow keys
clear(); // empty the screen
timeout(0); // reads do not block
处理key_down箭头键时,有以下行:
否则if(cursorY == LINES-1){
这导致诅咒没有采取任何行动
建议:
else if(cursorY < LINES-1){
其他键处理功能也需要进行类似的更正
需要检查许多ncurses和pad函数返回的代码。 对这些返回码进行错误检查将使故障显而易见。
使用此过程时:
prefresh(p,linseCount, 0, 0,0, LINES-1,COLS-1);
强烈地,直到问题解决为止,刷新整个焊盘而不是仅刷新一行
在您的p = newpad(WIDE,COLS);
之后添加以下调用p = newpad(WIDE,COLS);
添加此调用:
keypad(p, TRUE);
这将使您的键盘能够正确处理由箭头键产生的转义序列...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.