NCURSES - 在 pad 中移动光标
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 箭头键时,有这一行:
else if(cursorY == LINES-1){
导致光标没有动作的结果不在底线
建议:
else if(cursorY < LINES-1){
其他键处理函数需要类似的更正
来自许多 ncurses 和 pad 函数的 returned 代码需要检查。对这些 return 代码进行错误检查将使失败的原因一目了然。
使用此程序时:
prefresh(p,linseCount, 0, 0,0, LINES-1,COLS-1);
强烈建议,在调试问题之前,刷新整个 pad 而不是仅刷新一行
在 p = newpad(WIDE,COLS);
添加此调用后添加以下调用:
keypad(p, TRUE);
这将使您的键盘能够正确处理箭头键产生的转义序列...
我正在用 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 箭头键时,有这一行:
else if(cursorY == LINES-1){
导致光标没有动作的结果不在底线
建议:
else if(cursorY < LINES-1){
其他键处理函数需要类似的更正
来自许多 ncurses 和 pad 函数的 returned 代码需要检查。对这些 return 代码进行错误检查将使失败的原因一目了然。
使用此程序时:
prefresh(p,linseCount, 0, 0,0, LINES-1,COLS-1);
强烈建议,在调试问题之前,刷新整个 pad 而不是仅刷新一行
在 p = newpad(WIDE,COLS);
添加此调用后添加以下调用:
keypad(p, TRUE);
这将使您的键盘能够正确处理箭头键产生的转义序列...