![]() |
|
This thread is locked; no one can reply to it.
![]() ![]() |
1
2
|
std::cin keypressed? |
lucaz
Member #4,194
January 2004
|
is there a std::cin method similar to allegro's keypressed?. // 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. |
Specter Phoenix
Member #1,425
July 2001
![]() |
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 meant, ie if( keypressed() ) { readkey(); }
Programmer's paranoia: Don't trust anybody's code, not even your own. |
lucaz
Member #4,194
January 2004
|
in example, I like to call every game loop a function. 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
![]() |
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 Programmer's paranoia: Don't trust anybody's code, not even your own. |
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. |
Evert
Member #794
November 2000
![]() |
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
![]() |
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. |
CascoOscuro
Member #4,966
August 2004
![]() |
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
![]() |
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!. |
Kris Allen
Member #4,639
May 2004
![]() |
(Edit: Nevermind, someone already mentioned threads) - Kris |
lucaz
Member #4,194
January 2004
|
of course, this is a very good idea. |
Kris Allen
Member #4,639
May 2004
![]() |
Carrus85
Member #2,633
August 2002
![]() |
Um, isn't conio.h a windows-only header?
|
Evert
Member #794
November 2000
![]() |
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
![]() |
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. |
lucaz
Member #4,194
January 2004
|
Quote:
ANSI/ISO C No
One question, conio.h is only for windows? Thank you very much, I'll try this and... I WILL BACK |
Specter Phoenix
Member #1,425
July 2001
![]() |
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!. |
Specter Phoenix
Member #1,425
July 2001
![]() |
Is unicode cross-platform or OS-specific?
|
|
1
2
|