Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » std::cin keypressed?

Credits go to decsonic, Evert, and Specter Phoenix for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
std::cin keypressed?
lucaz
Member #4,194
January 2004

is there a std::cin method similar to allegro's keypressed?.
for example, if I like to make simple loop a exit with key A:

// allegro's key
while(!key[KEY_A]) { /* do something */ }

// std::cin
while(!cin.empty()) { /* do something */ }

Of course, there's not an 'empty()' method for cin.
Thanks in advance!.

Specter Phoenix
Member #1,425
July 2001
avatar

You would need to use unicode or make your own functions to hold the unicode. But if you are just waiting for a keypress(); to close a program then I use something like this:

#include <iostream>

int main(int argc, char *argv[])
{
    char keypress;

    std::cout << "Enter a character and hit enter" << endl;
    std::cin >> keypress;

    return(0);
}

Another method I use is this:

#include <iostream>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    system("PAUSE");
    return(0);
}

lucaz
Member #4,194
January 2004

your code block the program, and I dont want this.

[EDIT:] ...if the exit loop key wasn't pressed the program must continue

decsonic
Member #4,150
December 2003

use readkey then, dont think im getting what your asking either, elaborate?.

Edit
Just read cin into a value and if that value is "esc", break the loop.

meant, ie

if( keypressed() )
{
  readkey();
}

Programmer's paranoia: Don't trust anybody's code, not even your own.

Human Modeling Tutorials

lucaz
Member #4,194
January 2004

in example, I like to call every game loop a function.
I break the loop when the key A was pressed.
Allegro isnt initialized yet, I need to use cin.

I will try:

for(;;) {
 if(cin.rdbuf()->in_avail()) { char c; cin.get(c); if(c=='A') break; }
 function();
}

Quote:

Just read cin into a value and if that value is "esc", break the loop.

Nope, this will block the program.

Specter Phoenix
Member #1,425
July 2001
avatar

If you are using a if(){} to do it then you would need to do something like this:

if(key[KEY_ESC])
{
      // close program or function you are running
}

But there are two other ways to say this

do{
   // continue doing program
}while(!key[KEY_ESC]); // close program once esc is pressed

Or another way of the above do{}while(); loop:

while(!key[KEY_ESC])
{
     // run program until esc is pressed
}
return (0);

decsonic
Member #4,150
December 2003

He wants to avoid using allegro for some reason, cin isnt realy suited for it tho (IMO).

Edit
Disregard the first reply, misread a bit ;)
Any particular reason you cant use allegro to check this input?

Programmer's paranoia: Don't trust anybody's code, not even your own.

Human Modeling Tutorials

lucaz
Member #4,194
January 2004

yes, I have a little program to configure some things, and I dont need allegro while it is running.
If I cant find a way to do something like keypressed() without allegro, Ill init() and exit() it, but of course, if theres a way I will not install it for a simple keypressed().
thanks anyway!.

Evert
Member #794
November 2000
avatar

The standard C and C++ libraries don't have functions that report if a key is pressed, they will only report input after return has been pressed. You'll have to resort to platform-specific functions to detect keypresses, or use a third-party library such as Allegro or curses.

lucaz
Member #4,194
January 2004

Thanks!

CascoOscuro
Member #4,966
August 2004
avatar

You have pure C functions to modify the buffer stream size. You can resize them to a minimal capacity. That's the first thing you must do (at least if you don't want a "queue" of keys pressed). The next thing you must do is find a way to check if the buffer has a key pressed with a non-blocking method. How can you do that? I think you can create a thread that checks if cin is empty or not, and if not what is the character that contains it.

lucaz
Member #4,194
January 2004

Quote:

How can you do that? I think you can create a thread that checks if cin is empty or not, and if not what is the character that contains it.

I dont have any idea about how a thread can help me.
Can you explain a little more.
I it seems that nothing is stored in the cin.rdbuf() until you press enter.
:(

CascoOscuro
Member #4,966
August 2004
avatar

Scanning the input buffer parallel to the game processing. The thread will wait a key pressed, but the game stills running.

Marco Radaelli
Member #3,028
December 2002
avatar

Can you use assembly? I haven't started read about it but I think it will help you here. IIRC you simply have to call an interrupt which returns the keyboard state (or something similar).

lucaz
Member #4,194
January 2004

Quote:

Scanning the input buffer parallel to the game processing. The thread will wait a key pressed, but the game stills running.

Quote:

Can you use assembly? I haven't started read about it but I think it will help you here. IIRC you simply have to call an interrupt which returns the keyboard state (or something similar).

very good ideas!.
anyway, they are platform dependent :(. But still good! :D

Kris Allen
Member #4,639
May 2004
avatar

(Edit: Nevermind, someone already mentioned threads)

- Kris

lucaz
Member #4,194
January 2004

of course, this is a very good idea.
but c/c++ cant handle threads without a lib.

Kris Allen
Member #4,639
May 2004
avatar

Just been doing some hunting for you :) Try this:

#include <conio.h>
#include <stdio.h>

int main() {

  while (1) {
    if (kbhit()) {
      printf("%i\n",getch());
    }
  }

  return 0;
}

(not std:: I know, but works fine!)

- Kris

Carrus85
Member #2,633
August 2002
avatar

Um, isn't conio.h a windows-only header?

Evert
Member #794
November 2000
avatar

Quote:

You have pure C functions to modify the buffer stream size.

I don't think so. Do you have a link?

Quote:

The next thing you must do is find a way to check if the buffer has a key pressed with a non-blocking method. How can you do that? I think you can create a thread that checks if cin is empty or not, and if not what is the character that contains it.

You can't. Your program doesn't get to see the input until return is pressed. At least not on some systems.

Quote:

Um, isn't conio.h a windows-only header?

DOS only, I think. There may be ports to Linux console.

ImLeftFooted
Member #3,935
October 2003
avatar

Hmm... Im sure theres a way to do it. Two ideas:

Play around with the std::cin.peek() method, it might be able to get input without blocking.

If that doesnt work, there has to be a way to insert a new line into the stream using some obscure streaming class. If you can postfix a newline, then std::cin.getline() will return immediately. I'd look into this more, but its not my project. If i get bored enough Ill test my hypothesis. :P

lucaz
Member #4,194
January 2004

Quote:

ANSI/ISO C No
POSIX No

One question, conio.h is only for windows?
Perhaps kbkey() is one of those no-portable functions, that are posted to every system... :D
I'll try with peek() too.

Thank you very much, I'll try this and... I WILL BACK

Specter Phoenix
Member #1,425
July 2001
avatar

conio.h is DOS only not windows. Windows isn't DOS, DOS isn't Windows.

lucaz
Member #4,194
January 2004

then we have problems again!.
I havent tried yet peek() but i dont think i will work, since the rdbuf()->in_avail() returns 0 even if the user is writing.

Specter Phoenix
Member #1,425
July 2001
avatar

Is unicode cross-platform or OS-specific?

 1   2 


Go to: