Create a function to check for key press in unix using ncurses
up vote
14
down vote
favorite
I have been looking for an equivalent to kbhit() and I have read several forums on this subject, and the majority seems to suggest using ncurses.
How should I go about checking if a key is pressed in c++ using ncurses.
The function getch() provided by ncurses reads character from the window.
I would like to write a function that only checks if there is a key press and then I want to do getch().
Thanks in advance.
c++ unix command-line ncurses
add a comment |
up vote
14
down vote
favorite
I have been looking for an equivalent to kbhit() and I have read several forums on this subject, and the majority seems to suggest using ncurses.
How should I go about checking if a key is pressed in c++ using ncurses.
The function getch() provided by ncurses reads character from the window.
I would like to write a function that only checks if there is a key press and then I want to do getch().
Thanks in advance.
c++ unix command-line ncurses
add a comment |
up vote
14
down vote
favorite
up vote
14
down vote
favorite
I have been looking for an equivalent to kbhit() and I have read several forums on this subject, and the majority seems to suggest using ncurses.
How should I go about checking if a key is pressed in c++ using ncurses.
The function getch() provided by ncurses reads character from the window.
I would like to write a function that only checks if there is a key press and then I want to do getch().
Thanks in advance.
c++ unix command-line ncurses
I have been looking for an equivalent to kbhit() and I have read several forums on this subject, and the majority seems to suggest using ncurses.
How should I go about checking if a key is pressed in c++ using ncurses.
The function getch() provided by ncurses reads character from the window.
I would like to write a function that only checks if there is a key press and then I want to do getch().
Thanks in advance.
c++ unix command-line ncurses
c++ unix command-line ncurses
edited Oct 26 '10 at 17:08
ergosys
34.7k43162
34.7k43162
asked Oct 26 '10 at 16:48
Mimsy Jack
83116
83116
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
16
down vote
accepted
You can use the nodelay()
function to turn getch()
into a non-blocking call, which returns ERR
if no key-press is available. If a key-press is available, it is pulled from the input queue, but you can push it back onto the queue if you like with ungetch()
.
#include <ncurses.h>
#include <unistd.h> /* only for sleep() */
int kbhit(void)
int ch = getch();
if (ch != ERR)
ungetch(ch);
return 1;
else
return 0;
int main(void)
initscr();
cbreak();
noecho();
nodelay(stdscr, TRUE);
scrollok(stdscr, TRUE);
while (1)
if (kbhit())
printw("Key pressed! It was: %dn", getch());
refresh();
else
printw("No key pressed yet...n");
refresh();
sleep(1);
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
16
down vote
accepted
You can use the nodelay()
function to turn getch()
into a non-blocking call, which returns ERR
if no key-press is available. If a key-press is available, it is pulled from the input queue, but you can push it back onto the queue if you like with ungetch()
.
#include <ncurses.h>
#include <unistd.h> /* only for sleep() */
int kbhit(void)
int ch = getch();
if (ch != ERR)
ungetch(ch);
return 1;
else
return 0;
int main(void)
initscr();
cbreak();
noecho();
nodelay(stdscr, TRUE);
scrollok(stdscr, TRUE);
while (1)
if (kbhit())
printw("Key pressed! It was: %dn", getch());
refresh();
else
printw("No key pressed yet...n");
refresh();
sleep(1);
add a comment |
up vote
16
down vote
accepted
You can use the nodelay()
function to turn getch()
into a non-blocking call, which returns ERR
if no key-press is available. If a key-press is available, it is pulled from the input queue, but you can push it back onto the queue if you like with ungetch()
.
#include <ncurses.h>
#include <unistd.h> /* only for sleep() */
int kbhit(void)
int ch = getch();
if (ch != ERR)
ungetch(ch);
return 1;
else
return 0;
int main(void)
initscr();
cbreak();
noecho();
nodelay(stdscr, TRUE);
scrollok(stdscr, TRUE);
while (1)
if (kbhit())
printw("Key pressed! It was: %dn", getch());
refresh();
else
printw("No key pressed yet...n");
refresh();
sleep(1);
add a comment |
up vote
16
down vote
accepted
up vote
16
down vote
accepted
You can use the nodelay()
function to turn getch()
into a non-blocking call, which returns ERR
if no key-press is available. If a key-press is available, it is pulled from the input queue, but you can push it back onto the queue if you like with ungetch()
.
#include <ncurses.h>
#include <unistd.h> /* only for sleep() */
int kbhit(void)
int ch = getch();
if (ch != ERR)
ungetch(ch);
return 1;
else
return 0;
int main(void)
initscr();
cbreak();
noecho();
nodelay(stdscr, TRUE);
scrollok(stdscr, TRUE);
while (1)
if (kbhit())
printw("Key pressed! It was: %dn", getch());
refresh();
else
printw("No key pressed yet...n");
refresh();
sleep(1);
You can use the nodelay()
function to turn getch()
into a non-blocking call, which returns ERR
if no key-press is available. If a key-press is available, it is pulled from the input queue, but you can push it back onto the queue if you like with ungetch()
.
#include <ncurses.h>
#include <unistd.h> /* only for sleep() */
int kbhit(void)
int ch = getch();
if (ch != ERR)
ungetch(ch);
return 1;
else
return 0;
int main(void)
initscr();
cbreak();
noecho();
nodelay(stdscr, TRUE);
scrollok(stdscr, TRUE);
while (1)
if (kbhit())
printw("Key pressed! It was: %dn", getch());
refresh();
else
printw("No key pressed yet...n");
refresh();
sleep(1);
answered Oct 26 '10 at 23:58
Matthew Slattery
35.3k381105
35.3k381105
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4025891%2fcreate-a-function-to-check-for-key-press-in-unix-using-ncurses%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown