我在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;
}
答案 0 :(得分:0)
这两行导致光标与变量curserX和curserY认为光标所在的位置不同。
int c, cursorY=0, cursorX=0, linseCount = 0;
wmove(p,2,2);
此代码,处理key_down箭头键
if(cursorY==LINES-1 && linseCount==WIDE-1)
正在检查' linsecount'反对x宽度不正确。
建议用:
开始诅咒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);
强烈地,直到问题被调试,刷新整个pad而不是单行
答案 1 :(得分:0)
在p = newpad(WIDE,COLS);
添加此电话后添加以下电话:
keypad(p, TRUE);
这将使您的打击垫能够正确处理箭头键产生的转义序列...