Witam
poszukuję jakiegoś sposobu na czekanie np. przez 10 na wciśnięcie klawisza - w innym wypadku wykonuje się inna akcja.
pozdrawiam
0
1
Wyłączasz buforowanie, przełączasz tryb stdin na nieblokujący i używasz selecta / polla / kevent / cokolwiek do programowania event-based oczekując przez zadany timeout.
Edit: +kod - ilustracja.
#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
#include <err.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
int
main()
{
int fd, kq, events;
struct termios conf;
struct kevent ev;
struct timespec tv;
fd = fileno(stdin);
if (!isatty(fd))
err(1, "stdin is not a tty");
if (tcgetattr(fd, &conf) < 0)
err(1, "tcgetattr(2) failed");
/*
* Wyłączenie buforowania.
*/
setbuf(stdin, NULL);
/*
* Konfiguracja terminala tak, aby nie blokował się w oczekiwaniu na
* enter. Możesz dodatkowo wyłączyć echo pisząc ~(ICANON | ECHO).
*/
conf.c_lflag &= ~ICANON;
conf.c_cc[VTIME] = 0;
conf.c_cc[VMIN] = 0;
tcsetattr(fd, TCSANOW, &conf);
/*
* W Linuksie pewnie trzeba użyć epoll albo selecta.
*/
kq = kqueue();
if (kq < 0)
err(1, "kqueue(2) failed");
EV_SET(&ev, fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, NULL);
if (kevent(kq, &ev, 1, NULL, 0, NULL) < 0)
err(1, "kevent(2) failed");
/*
* Timeout na 10s.
*/
tv.tv_sec = 10;
tv.tv_nsec = 0;
events = kevent(kq, NULL, 0, &ev, 1, &tv);
if (events < 0)
err(1, "kevent(2) failed");
if (events == 0)
printf("No events.\n");
else
printf("Key pressed: %d\n", fgetc(stdin));
return (0);
}
0
30% tych bibliotek nawet nie mam, chodziło mi o użycie tylko standardowych bibliotek które na pewno mam.
0
A które na pewno masz?
0
Nie ma ich ponieważ używa Windowsa a nie Linuxa