在窗口中移动光标 - ncurses
问题描述:
我用ncurse创建了一个窗口,我试图用我的窗口内的箭头键移动光标,并且只能在里面移动光标。对于我的理解我必须使用wmove()
,但显然我没有得到如何使用它。在窗口中移动光标 - ncurses
下面是一些代码片段,让你知道我做了什么:
int main(int argc, char **argv)
{
WINDOW *my_win;
int startx, starty, width, height;
int ch;
int x = 50;
int y = 5;
initscr();
cbreak();
keypad(stdscr, TRUE);
noecho();
height = 10;
width = 100;
starty = (LINES - height)/2;
startx = (COLS - width)/2;
printw("Press F1 to exit");
refresh();
my_win = create_newwin(25, 50, y, x);
wmove(my_win, y, x);
while((ch = getch()) != KEY_F(1))
{
if ((ch = getch()) == KEY_RIGHT)
wmove(my_win, y++, x++);
refresh();
}
endwin();
return 0;
}
WINDOW *create_newwin(int height, int width, int starty, int startx)
{
WINDOW *local_win;
local_win = newwin(height, width, starty, startx);
box(local_win, 0, 0);
wrefresh(local_win);
return (local_win);
}
答
我只是有一点你的代码。
我觉得主要的问题是你做了refresh()
整个屏幕,当刷新窗口wrefresh (my_win)
本来就够了。
问题二:你是等待keystrke两次: 1. while
循环 2. if
声明。
getch()
在while循环中就足够了。我们可以在循环中使用ch来检测按下了哪个键。
我会做一个switch
代替if
选择:
while((ch = getch()) != 'q')
{
switch (ch)
{
case KEY_LEFT:
x--;
break;
case KEY_RIGHT:
x++;
break;
case KEY_UP:
y--;
break;
case KEY_DOWN:
y++;
break;
}
wmove(my_win, y, x);
wrefresh(my_win);
}
我也去掉了一些unnessacery声明。
这里是我做了什么:
#include <stdio.h>
#include <ncurses.h>
WINDOW *create_newwin(int height, int width, int starty, int startx)
{
WINDOW *local_win = newwin(height, width, starty, startx);
box(local_win, 0, 0);
wrefresh(local_win);
return (local_win);
}
int main(int argc, char **argv)
{
WINDOW *my_win;
int ch;
int x = 2;
int y = 2;
initscr();
cbreak();
keypad(stdscr, TRUE);
noecho();
printw("Press q to exit");
refresh();
my_win = create_newwin(10, 20, y, x);
wmove(my_win, y, x);
wrefresh(my_win);
while((ch = getch()) != 'q')
{
switch (ch)
{
case KEY_LEFT:
x--;
break;
case KEY_RIGHT:
x++;
break;
case KEY_UP:
y--;
break;
case KEY_DOWN:
y++;
break;
}
wmove(my_win, y, x);
wrefresh(my_win);
}
endwin();
return 0;
}
如果x或y获得比窗口大小大或变小,光标不移动,但数值仍然量变到质变。因此,当数值在窗口大小内时,请确保只允许减少或增加x和y!
提示:
case KEY_LEFT:
if (x>0) { x--; }
break;
答
其实,一个更好的解决办法是使用wgetch
为my_win
窗口,并消除大多数的调用来refresh
和wrefresh
。 wgetch
(getch
)在正在读取的窗口上进行刷新。到Zehetner的例子中的变化会是这样的:
--- foo3.c.orig 2015-04-02 19:53:18.000000000 -0400
+++ foo3.c 2015-04-02 19:56:31.000000000 -0400
@@ -1,11 +1,12 @@
#include <stdio.h>
#include <ncurses.h>
+static
WINDOW *create_newwin(int height, int width, int starty, int startx)
{
WINDOW *local_win = newwin(height, width, starty, startx);
box(local_win, 0, 0);
- wrefresh(local_win);
+ keypad(local_win, TRUE);
return (local_win);
}
@@ -27,9 +28,8 @@
refresh();
my_win = create_newwin(10, 20, y, x);
wmove(my_win, y, x);
- wrefresh(my_win);
- while((ch = getch()) != 'q')
+ while((ch = wgetch(my_win)) != 'q')
{
switch (ch)
{
@@ -45,10 +45,12 @@
case KEY_DOWN:
y++;
break;
+ default:
+ beep();
+ break;
}
wmove(my_win, y, x);
- wrefresh(my_win);
}
endwin();
或(导致程序):
#include <stdio.h>
#include <ncurses.h>
static
WINDOW *create_newwin(int height, int width, int starty, int startx)
{
WINDOW *local_win = newwin(height, width, starty, startx);
box(local_win, 0, 0);
keypad(local_win, TRUE);
return (local_win);
}
int main(int argc, char **argv)
{
WINDOW *my_win;
int ch;
int x = 2;
int y = 2;
initscr();
cbreak();
keypad(stdscr, TRUE);
noecho();
printw("Press q to exit");
refresh();
my_win = create_newwin(10, 20, y, x);
wmove(my_win, y, x);
while((ch = wgetch(my_win)) != 'q')
{
switch (ch)
{
case KEY_LEFT:
x--;
break;
case KEY_RIGHT:
x++;
break;
case KEY_UP:
y--;
break;
case KEY_DOWN:
y++;
break;
default:
beep();
break;
}
wmove(my_win, y, x);
}
endwin();
return 0;
}
不太熟悉的ncurses,但应该'create_newwin(25,50,Y,X)'代替是'create_newwin(starty,startx,height,width)'?然后,它看起来像用'wmove(my_win,y,x)'将光标放在右下角,当按下'KEY_RIGHT'时,尝试将它向下移动到右侧,当你已经尽可能地朝着这个方向前进。 – Steve 2015-04-02 12:26:07
@Steve我刚编辑我的问题,让你看到我的create_newwin。根据我的功能,create_newwin(25,50,y,x)似乎正在工作。 – Mehdi 2015-04-02 13:04:51