How would I effectively cancel a call for user input if there is no input within a certain time? (I'm programming a game for a terminal/cmd window with Mac OS X).
I have tried turning off canonical buffering and using a timer thread that joins after the call for user input. I also tried implementing the call to pthread_join() within the parameters of the while loop. Still nothing. The problem is that even though canonical buffering is off, the call for user input is still held up when there is no input. It works fine if there is input though.
It would be great if I could do this without fiddling around with downloading and installing ncurses, but I'll do it if I have to.
Edit: Source code:
//Most headers only pertain to my main program.
#include <iostream>
#include <termios.h>
#include <pthread.h>
#include <time.h>
#include <cstring>
#include <stdio.h>
#include <string.h>
using namespace std;
//Timer function.
void *Timer(void*) {
time_t time1, time2;
time1 = time(NULL);
while (time2 - time1 < 1) {
time2 = time(NULL);
}
pthread_exit(NULL);
}
int main() {
//Remove canonical buffering.
struct termios t_old, t_new;
tcgetattr(STDIN_FILENO, &t_old);
t_new = t_old;
t_new.c_lflag &= ~ICANON;
tcsetattr(STDIN_FILENO, TCSANOW, &t_new);
cout << "Press any key to continue." << endl;
string szInput;
int control = 0;
do {
pthread_t inputTimer;
pthread_create(&inputTimer, NULL, Timer, NULL);
szInput = "";
while (szInput == "") {
szInput = cin.get();
//Handle keypresses instantly.
if (szInput == "a") {
cout << endl << "Instant keypress." << endl;
}
}
pthread_join(inputTimer, NULL);
cout << endl << "One second interval." << endl;
control ++;
} while (control < 25);
cout << "Game Over." << endl;
return 0;
}
See if this works!
char ch; //Input character
int time = 0; //Time iterator
int TIMER = 5000; //5 seconds
while(time<TIMER)
{
if(!kbhit())
{
time = 0;
ch = getch();
//Do your processing on keypress
}
time++;
delay(1);
}
kbhit() detects if any keystroke has occurred. If yes, then get the key character in ch.
One way of checking if there is input is to poll the file descriptor STDIN_FILENO using e.g. the select system call. If STDIN_FILENO is readable, then you can read at at least one character. You can also pass a timeout to the select call.
Thanks Shashwat, it works with the below modifications:
1) Changing if(!kbhit()) to if(kbhit())
2) Changing delay(1); to Sleep(1);
I do not have enough rep to post a comment, hence adding as an answer.
Related
I am trying to do an implementation of htop (system monotitoring) in c++.
So I am using ncurses to refresh my terminal.
I need to get new info every 5 seconds for example, im using a loop to do so.
while (42)
{
key = std::cin.get();
std::cout << key;
this->gereEvent(key);
std::cout << i<< std::endl;
if (i == 500000000)
{
std::cout << "test"<< std::endl;
// fputs(tgetstr((char *)"cl", 0), stdout);
this->refresh();
i = 0;
}
i++;
}
But problem is cin.get() stops the loop..
I cannot do a thread eitheir because std::thread needs c++11.
Have you an idea of how I can do that ?
You need to poll the keyboard events. This can be done in ncurses with getch.
#include<stdio.h>
#include<curses.h>
#include<unistd.h>
int main ()
{
int i=0;
initscr(); //in ncurses
timeout(0);
while(!i)
{
usleep(1);
i=getch();
printw("%d ",i);
if(i>0)
i=1;
else
i=0;
}
endwin();
printf("\nhitkb end\n");
return 0;
}
This example is from http://cc.byexamples.com/2007/04/08/non-blocking-user-input-in-loop-without-ncurses/comment-page-1/#comment-2100.
I'm new to C++. Have decided to do my own game. And i want to make a starting screen for it. And the problem is that i havent found the way to make an "Press any key to continue" function while dots continue. I made the loop for the programm to wait till any would be pressed but dots dont want to display in.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <windows.h>
using namespace std;
int pressCheck(){
char c = 0;
c = getchar();
if (c == 0)
return 1;
return 0;
}
int main()
{
cout << "\t\t\t\t Hello" << endl;
Sleep(300);
cout << "\t\t Welcome to my new game BITHCES!" << endl << endl;
Sleep(700);
cout << "\t\t\tPress any key to proceed";
while(!pressCheck()){
Sleep(300);
cout << ".";
Sleep(300);
cout << ".";
Sleep(300);
cout << ".";
}
getchar();
system("cls");
Sleep(100);
return 0;
}
If you are creating a text based game I would recommend using ncurses (or pdcurses for windows):
[...] a toolkit for developing "GUI-like" application software that runs
under a terminal emulator.
Implementing what you have above would be something like
#include <string>
#include <ncurses.h> // This header might be different on windows
#include <unistd.h> // for usleep, replace with Windows.h (?)
void DisplayCentre(int yy, const std::string& str)
{
// Get the screen size
int y, x;
getmaxyx(stdscr, y, x);
// Compute starting location for string (centre)
x = (x - str.size())/2;
// Write the string to the window
mvwprintw(stdscr, yy, x, str.c_str());
// Make sure the screen is updated
refresh();
}
void PromptForKey(void)
{
// Get the screen size
int y, x;
getmaxyx(stdscr, y, x);
// Write a message at the bottom left of the screen
mvwprintw(stdscr, y-1, 0, "Press any key to continue");
// Set a time-out for wgetch
wtimeout(stdscr, 300);
// While the user hasn't entered a character
while (wgetch(stdscr) == ERR)
{
// Add another dot to the screen
waddch(stdscr, '.');
refresh();
}
// Clear time-out
notimeout(stdscr, true);
}
int main(int argc, char** argv)
{
initscr(); // Initialize curses
cbreak(); // Make typed characters immediately available
noecho(); // Don't automatically print typed characters
curs_set(0); // Make the cursor invisible (where supported)
// Display `Hello' (at line 10)
DisplayCentre(10, "Hello");
// Delay (you might want to use Sleep())
sleep(1);
// Display `Welcome to my new game' (at line 15)
DisplayCentre(15, "Welcome to my new game");
sleep(1);
// Prompt user for key
PromptForKey();
// Close down curses
endwin();
return 0;
}
To compile this program on Linux I use g++ test.cpp -lncurses. On windows you will probaly need to replace sleep with the windows Sleep function and use the appropriate header. You may also need to use an alternative to ncurses.
However, if you are just learning to program I would suggest you try using ncurses in Python. Python has the benefit of being an interpreted language so you don't need to worry too much about compiling or linking executables. Python is also mostly cross platform. The above implemented in Python:
#!/usr/bin/python
from curses import *
from time import sleep
def promptForKey(win):
""" Ask the user to press any key to continue. """
# Get screen size
y,x = win.getmaxyx()
# Display prompt
win.addstr(y-1, 0, "Press any key to continue")
win.refresh()
# Set time-out
win.timeout(300)
while (win.getch() == ERR):
win.addch('.')
# Disable time-out
win.notimeout(True)
def dispCentre(win, yy, string, delay):
""" Display string at line yy and wait for delay milliseconds. """
# Get screen size
y,x = win.getmaxyx()
# Display string in centre
x = (x - len(string))/2
win.addstr(yy, x, string)
win.refresh()
# Delay
sleep(delay)
if __name__ == '__main__':
# Initialize curses
win = initscr()
cbreak()
noecho()
curs_set(0)
# Display some stuff
dispCentre(win, 10, "Hello", 0.3)
dispCentre(win, 15, "Welcome to my new game", 0.7)
promptForKey(win)
# Close down curses
endwin()
i know i am late but i think maybe you wanted to do this? (Run the attached code)
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <windows.h>
#include<conio.h>
using namespace std;
bool pressCheck(){
if (getch())
return 1;
return 0;
}
int main()
{
cout << "\t\t\t\t Hello" << endl;
Sleep(300);
cout << "\t\t Welcome to my new game BITHCES!" << endl << endl;
Sleep(700);
cout << "\t\t\tPress any key to proceed";
while(!pressCheck()){
Sleep(300);
cout << ".";
Sleep(300);
cout << ".";
Sleep(300);
cout << ".";
}
system("cls");
Sleep(100);
return 0;
}
I'm trying to make a timer which will count from the amount of time the user commands it, to zero.
Now I'm trying to add a pause faction to it, which will require to my programm to accept and read input while the timer ticks.
This is the code I have so far -
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <dos.h>
#include <windows.h>
using namespace std;
// sleep(5000);
int seconds;
int hoursLeft;
int minutesLeft;
int secondsCount=0;
void timeLeft ()
{
hoursLeft = seconds/3600;
minutesLeft = seconds/60 - hoursLeft*60;
}
void timer ()
{
if (secondsCount == 60)
{
timeLeft();
cout << "The Amount of time left is: " << hoursLeft << " hours and " << minutesLeft << " minutes left." << endl;
secondsCount=0;
}
secondsCount++;
seconds--;
Sleep(1000);
timer();
}
int main()
{
// introduction and time picking
cout << "Welcome to my Timer - Please set the amount of hours and than minutes you want the timer to run" << endl;
double requestedHours, requestedMinutes;
cin >> requestedHours;
cin >> requestedMinutes;
double requestedSeconds = requestedHours*3600 + requestedMinutes*60;
seconds = requestedSeconds;
cout << "Timer Started";
timer();
}
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
int main()
{
char buffer[16];
int flags;
int fd;
int r;
fd = 0; //stdin
flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
while (1)
{
memset(buffer, 0, sizeof(buffer));
r = read(0, buffer, sizeof(buffer)); //return the number of bytes it reads
if (r > 0) //something was read
{
printf("read: %d\n", buffer[0]);
fflush(stdin);
}
else //nothing has been read
{
puts("update timer here");
}
usleep(50000);
}
return (0);
}
using non blocking read on file descriptor can also be cool
sorry i only have this solution in C
PS: You're computer isnt suppose to work recursively infinitly, you should use a loop instead of an infinite recursion (timer() recalls itself), or your stack will overflow
I'm currently programming Tetris in C++. Right now I am at the stage where I have finished writing the program but I still need to fix a few bugs and optimize performance.
That being said, one of the flaws in my program is that it can only handle one key-press per second. I need it to handle at least three. You can see the flaw demonstrated by this code:
//Most headers only pertain to my main program.
#include <iostream>
#include <termios.h>
#include <pthread.h>
#include <time.h>
#include <cstring>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
using namespace std;
//Timer function.
void *Timer(void*) {
time_t time1, time2;
time1 = time(NULL);
while (time2 - time1 < 1) {
time2 = time(NULL);
}
pthread_exit(NULL);
}
int main() {
//Remove canonical buffering.
struct termios t_old, t_new;
tcgetattr(STDIN_FILENO, &t_old);
t_new = t_old;
t_new.c_lflag &= (~ICANON & ~ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &t_new);
const int STDIN = 0;
struct timeval tv, tv1;
fd_set readfds, readfds2, master;
tv.tv_sec = 1;
tv.tv_usec = 0;
FD_ZERO(&readfds);
FD_ZERO(&master);
FD_SET(STDIN, &readfds);
FD_SET(STDIN, &master);
char buffer[1];
while(buffer[0] != 'q') {
pthread_t inputTimer;
pthread_create(&inputTimer, NULL, Timer, NULL);
readfds = master;
memcpy(&tv1, &tv, sizeof(tv));
if (select(STDIN+1, &readfds, NULL, NULL, &tv1) == -1) {
perror("select");
}
if (FD_ISSET(STDIN, &readfds)) {
buffer[0] = cin.get();
cout << "You entered: " << buffer << endl;
}
pthread_join(inputTimer, NULL);
cout << "Timed out.\n" << endl;
}
cout << "Game Over." << endl;
return 0;
}
As you can see, the program operates by setting up a one second interval timer and timeval. Because both timers use ints to determine how much time has passed, they cannot be more precise than one second. How can I modify my program to be more precise?
My thought was to copy the value of tv1 to a third value if a key was pressed and then wait for input again but for whatever value time that tv1 was. For example if I press a key when there is only half a second left, the value 0.5 would be taken from tv1 and copied to another variable. Then the program would only wait for half a second for input, instead of the full second. This didn't work, however, because tv1 only ever equals 1 or 0.
Try to use struct timeval and gettimeofday() in sys/time.h. You can achieve microseconds resolution.
Manpage: http://linux.die.net/man/3/gettimeofday
Further info: http://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html
EDIT:
In Linux (not portable to MinGW under Windows) you could also use poll() (see here), which lets you wait for milliseconds. That would be more efficient because poll suspends the thread execution until time is out.
//Timer function.
void *Timer(void*) {
poll(0, 0, 100); //Suspend thread for 100 ms.
pthread_exit(NULL);
}
The pollfunction is declared in poll.h
The problem here is that pthread_join suspends the main thread until the timer thread completes. You will therefore miss any user input that comes during or after the join. Since you are already using select, you might as well make use of the timeout built into the select statement. If you keep a watch of time elapsed, you can achieve the same effect without the timer thread.
I'm looking for a (multiplatform) way to do non-blocking console input for my C++ program, so I can handle user commands while the program continually runs. The program will also be outputting information at the same time.
What's the best/easiest way to do this? I have no problem using external libraries like boost, as long as they use a permissive license.
Example using C++11:
#include <iostream>
#include <future>
#include <thread>
#include <chrono>
static std::string getAnswer()
{
std::string answer;
std::cin >> answer;
return answer;
}
int main()
{
std::chrono::seconds timeout(5);
std::cout << "Do you even lift?" << std::endl << std::flush;
std::string answer = "maybe"; //default to maybe
std::future<std::string> future = std::async(getAnswer);
if (future.wait_for(timeout) == std::future_status::ready)
answer = future.get();
std::cout << "the answer was: " << answer << std::endl;
exit(0);
}
online compiler: https://rextester.com/GLAZ31262
I would do this by creating separate a thread which calls normal blocking IO functions and pass it a callback function which it would call when it got input. Are you sure you need to do what you said you want to do?
As for outputting information at the same time, what would happen if the user was in the middle of typing some input and you printed something?
I've done this on QNX4.5 that doesn't support threads or Boost by using select. You basically pass select STDIN as the file descriptor to use and select will return when a new line is entered. I've added a simplified example loop below. It's platform independent, at least for Unix like systems. Not sure about Windows though.
while (!g_quit)
{
//we want to receive data from stdin so add these file
//descriptors to the file descriptor set. These also have to be reset
//within the loop since select modifies the sets.
FD_ZERO(&read_fds);
FD_SET(STDIN_FILENO, &read_fds);
result = select(sfd + 1, &read_fds, NULL, NULL, NULL);
if (result == -1 && errno != EINTR)
{
cerr << "Error in select: " << strerror(errno) << "\n";
break;
}
else if (result == -1 && errno == EINTR)
{
//we've received and interrupt - handle this
....
}
else
{
if (FD_ISSET(STDIN_FILENO, &read_fds))
{
process_cmd(sfd);
}
}
}
There is one easy way:
char buffer[512];
int point = 0;
...
while (_kbhit()) {
char cur = _getch();
if (point > 511) point = 511;
std::cout << cur;
if (cur != 13) buffer[point++] = cur;
else{
buffer[point] = '\0';
point = 0;
//Run(buffer);
}
}
No block, all in 1 thread. As for me, this works.
Non-blocking console input C++ ?
Ans: do console IO on a background thread and provide a means of communicating between threads.
Here's a complete (but simplistic) test program that implements async io by deferring the io to a background thread.
the program will wait for you to enter strings (terminate with newline) on the console and then perform a 10-second operation with that string.
you can enter another string while the operation is in progress.
enter 'quit' to get the program to stop on the next cycle.
#include <iostream>
#include <memory>
#include <string>
#include <future>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <deque>
int main()
{
std::mutex m;
std::condition_variable cv;
std::string new_string;
bool error = false;
auto io_thread = std::thread([&]{
std::string s;
while(!error && std::getline(std::cin, s, '\n'))
{
auto lock = std::unique_lock<std::mutex>(m);
new_string = std::move(s);
if (new_string == "quit") {
error = true;
}
lock.unlock();
cv.notify_all();
}
auto lock = std::unique_lock<std::mutex>(m);
error = true;
lock.unlock();
cv.notify_all();
});
auto current_string = std::string();
for ( ;; )
{
auto lock = std::unique_lock<std::mutex>(m);
cv.wait(lock, [&] { return error || (current_string != new_string); });
if (error)
{
break;
}
current_string = new_string;
lock.unlock();
// now use the string that arrived from our non-blocking stream
std::cout << "new string: " << current_string;
std::cout.flush();
for (int i = 0 ; i < 10 ; ++i) {
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << " " << i;
std::cout.flush();
}
std::cout << ". done. next?\n";
std::cout.flush();
}
io_thread.join();
return 0;
}
sample test run:
$ ./async.cpp
first
new string: first 0 1las 2t 3
4 5 6 7 8 9. done. next?
new string: last 0 1 2 3 4 5 6 7 8quit 9. done. next?
ncurses can be a good candidate.
The StdinDataIO class of the BSD-licensed MUSCLE networking library supports non-blocking reads from stdin under Windows, MacOS/X, and Linux/Unix ... you could use that (or just examine the code as an example of how it can be done) if you want.
You can use the tinycon library to do this. Just spawn a tinycon object in a new thread, and you are pretty much done. You can define the trigger method to fire off whatever you'd like when enter is pressed.
You can find it here:
https://sourceforge.net/projects/tinycon/
Also, the license is BSD, so it will be the most permissive for your needs.
libuv is a cross-platform C library for asynchronous I/O. It uses an event loop to do things like read from standard input without blocking the thread. libuv is what powers Node.JS and others.
In a sense, this answer is incomplete. But yet, I think it can be useful even for people who have different platforms or circumstances, giving the idea, what to look for in their platform.
As I just wrote some scripting engine integration into an SDL2 main event loop (which is supposed to read lines from stdin if there are lines to be read), here is how I did it (on linux (debian bullseye 64 bit)). See below.
But even if you are not on linux, but on some other posix system, you can use the equivalent platform APIs of your platform. For example, you can use kqueue on FreeBSD. Or you can consider using libevent for a bit more portable approach (still will not really work on Windows).
This approach might also work on Windows if you do some special fiddling with the rather new-ish ConPTY. In traditional windows console applications, the problem is, that stdin is not a real file handle and as such, passing it to libevent or using IOCP (IO completion ports) on it will not work as expected.
But, this approach should also work on posix systems, if there is redirection at play. As long as there is a file handle available.
So how does it work?
Use epoll_wait() to detect if there is data available on stdin. While consoles can be configured in all sorts of ways, typically, they operate on a line by line basis (should also apply for ssh etc.).
Use your favorite getline() function to read the line from stdin. Which will work, because you know, there is data and it will not block (unless your console is not defaulting to line by line handling).
Rince and repeat.
#include <unistd.h>
#include <sys/epoll.h>
#include <iostream>
#include <string>
#include <array>
using EpollEvent_t = struct epoll_event;
int main(int argc, const char* argv[]) {
//
// create epoll instance
//
int epollfd = epoll_create1(0);
if (epollfd < 0) {
std::cout << "epoll_create1(0) failed!" << std::endl;
return -1;
}
//
// associate stdin with epoll
//
EpollEvent_t ev;
ev.data.ptr = nullptr;
ev.data.fd = STDIN_FILENO; // from unistd.h
ev.data.u32 = UINT32_C(0);
ev.data.u64 = UINT64_C(0);
ev.events = EPOLLIN;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) < 0) {
std::cout
<< "epoll_ctl(epollfd, EPOLL_CTL_ADD, fdin, &ev) failed."
<< std::endl;
return -1;
}
//
// do non-blocking line processing in your free running
// main loop
//
std::array<EpollEvent_t,1> events;
bool running = true;
while (running) {
int waitret = epoll_wait(epollfd,
events.data(),
events.size(),
0); // 0 is the "timeout" we want
if (waitret < 0) {
std::cout << "epoll_wait() failed." << std::endl;
running = false;
}
if (0 < waitret) { // there is data on stdin!
std::string line;
std::getline(std::cin, line);
std::cout
<< "line read: [" << line << "]" << std::endl;
if (line == "quit")
running = false;
}
// ... Do what you usually do in your main loop ...
}
//
// cleanup of epoll etc.
//
close(epollfd);
return 0;
}
You could do:
#include <thread>
#include <chrono>
#include <string>
#include <iostream>
int main() {
std::cout << "Type exit to quit." << std::endl;
// initialize other std::thread handlers here
std::string input;
while (input != "exit") {
std::getline(std::cin, input);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
std::cout << "Cleaning up and quitting" << std::endl;
return 0;
};
A simple answer with thread/future and reading a single char at a time (you can replace getchar with cin as required)
Timeout is set to zero and a new future is created every time the previous call is completed.
Like cin, getchar requires that the user hits the RETURN key to end the function call.
#include <chrono>
#include <cstdio>
#include <future>
#include <iostream>
#include <thread>
static char get_usr_in()
{
return std::getchar();
}
int main()
{
std::chrono::seconds timeout(0);
std::future<char> future = std::async(std::launch::async, get_usr_in);
char ch = '!';
while(ch!='q') {
if(future.wait_for(timeout) == std::future_status::ready) {
ch = future.get();
if(ch!='q') {
future = std::async(std::launch::async, get_usr_in);
}
if(ch >= '!' && ch <'~')
std::cout << "ch:" << ch << std::endl;
}
std::cout << "." << std::endl;
}
exit(0);
}
Why not use promises?
#include <iostream>
#include <istream>
#include <thread>
#include <future>
#include <chrono>
void UIThread(std::chrono::duration<int> timeout) {
std::promise<bool> p;
std::thread uiWorker([&p]() {
bool running = true;
while(running) {
std::string input;
std::cin >> input;
if(input == "quit") {
p.set_value(true);
running = false;
}
}
});
auto future = p.get_future();
if (future.wait_for(timeout) != std::future_status::ready) {
std::cout << "UI thread timed out" << std::endl;
uiWorker.detach();
return;
}
uiWorker.join();
}
int main()
{
std::thread uiThread(UIThread, std::chrono::seconds(3));
std::cout << "Waiting for UI thread to complete" << std::endl;
uiThread.join();
}
online complier