How do I...?

S

SumGie

I need a command, (or function, or whatever) that will look at the keyboard
to determine if any keys are being pressed, without pausing if there are
none. The CIN>> command pauses the program to wait for a keypress.
I want a loop that will keep looping, updating the screen endlessly, until I
press a (particular) key to stop it. I'm trying to program a text example
of the game of life. I want to be able to tell it to go, and have it
running generations until I tell it to stop, and then return to a menu mode
where the user can turn individual cells on or off.

My C++ instructor has not been able to tell me a way to do this. Can any of
you? (adding machine language code to my program has been suggested, but is
not an option. We don't cover how to do that until much later in the
course, not to mention that I don't know machine language programming.)

Thanks all!
 
A

adbarnet

It's off topic in C++, but you need to research keyboard interrupts. Your
main problem is that your application needs to have 'focus' before it can
assume keyboard actions are intended for it - how you do that is definitely
not C++ specific.

HTH

Aiden
 
O

osmium

SumGie said:
I need a command, (or function, or whatever) that will look at the keyboard
to determine if any keys are being pressed, without pausing if there are
none. The CIN>> command pauses the program to wait for a keypress.
I want a loop that will keep looping, updating the screen endlessly, until
I press a (particular) key to stop it. I'm trying to program a text
example of the game of life. I want to be able to tell it to go, and have
it running generations until I tell it to stop, and then return to a menu
mode where the user can turn individual cells on or off.

My C++ instructor has not been able to tell me a way to do this. Can any
of you? (adding machine language code to my program has been suggested,
but is not an option. We don't cover how to do that until much later in
the course, not to mention that I don't know machine language
programming.)

This is in the C FAQ.

http://www.eskimo.com/~scs/C-faq/q19.1.html

It provides guidance, not an answer. It would be easiest to look around on
google groups for an answer, it is a very common question. I tool a look at
the SDL library solution proposed upthread, and it may be fine and do what
you want. But I would expect a steep learning curve with that approach.
 
J

Joe C

SumGie said:
I need a command, (or function, or whatever) that will look at the keyboard
to determine if any keys are being pressed, without pausing if there are
none. The CIN>> command pauses the program to wait for a keypress.
I want a loop that will keep looping, updating the screen endlessly, until
I press a (particular) key to stop it. I'm trying to program a text
example of the game of life. I want to be able to tell it to go, and have
it running generations until I tell it to stop, and then return to a menu
mode where the user can turn individual cells on or off.

My C++ instructor has not been able to tell me a way to do this. Can any
of you? (adding machine language code to my program has been suggested,
but is not an option. We don't cover how to do that until much later in
the course, not to mention that I don't know machine language
programming.)

Thanks all!

[OT] If you are on a windows system...this compilable code may be helpful.

#include <windows.h>
#include <string>
#include <iostream>

using namespace std;

string getPassword();

int main(){

string password;

while(true){

cout << " Input Password: ";
password = getPassword();

cout << "Verify Password: ";
string pwverify = getPassword();

if(password == pwverify) {break;}
cout << "\nPassword mismatch. Try again\n";
}

cout << password << endl << password.size() << endl;

}


string getPassword(){
char inkey;
DWORD w;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE keyboard = GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD input;

string pw;

while(true) {
// Read an input record.
ReadConsoleInput(keyboard, &input, 1, &w);

// Process a key down input event.
if(input.EventType == KEY_EVENT
&& input.Event.KeyEvent.bKeyDown)
{

// Retrieve the character that was pressed.
inkey = input.Event.KeyEvent.uChar.AsciiChar;
if(inkey == 13){ break;} // enter pressed
if(inkey == 8){ // backspace
if(pw.size()){
pw.erase(pw.size()-1, 1);
cout << "\b \b";
}
continue;
}

if(inkey != 0) { // 0 is function keys, shift, ctrl, etc.
cout << '*';
pw += inkey;
}
}
}
cout << endl;
return pw;
}
 
E

evaned

Joe said:
SumGie said:
I need a command, (or function, or whatever) that will look at the keyboard
to determine if any keys are being pressed, without pausing if there are
none. The CIN>> command pauses the program to wait for a keypress.
I want a loop that will keep looping, updating the screen endlessly, until
I press a (particular) key to stop it. I'm trying to program a text
example of the game of life. I want to be able to tell it to go, and have
it running generations until I tell it to stop, and then return to a menu
mode where the user can turn individual cells on or off.

My C++ instructor has not been able to tell me a way to do this. Can any
of you? (adding machine language code to my program has been suggested,
but is not an option. We don't cover how to do that until much later in
the course, not to mention that I don't know machine language
programming.)

Thanks all!

[OT] If you are on a windows system...this compilable code may be helpful.

#include <windows.h>
#include <string>
#include <iostream>

using namespace std;

string getPassword();

int main(){

string password;

while(true){

cout << " Input Password: ";
password = getPassword();

cout << "Verify Password: ";
string pwverify = getPassword();

if(password == pwverify) {break;}
cout << "\nPassword mismatch. Try again\n";
}

cout << password << endl << password.size() << endl;

}


string getPassword(){
char inkey;
DWORD w;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE keyboard = GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD input;

string pw;

while(true) {
// Read an input record.
ReadConsoleInput(keyboard, &input, 1, &w);

// Process a key down input event.
if(input.EventType == KEY_EVENT
&& input.Event.KeyEvent.bKeyDown)
{

// Retrieve the character that was pressed.
inkey = input.Event.KeyEvent.uChar.AsciiChar;
if(inkey == 13){ break;} // enter pressed
if(inkey == 8){ // backspace
if(pw.size()){
pw.erase(pw.size()-1, 1);
cout << "\b \b";
}
continue;
}

if(inkey != 0) { // 0 is function keys, shift, ctrl, etc.
cout << '*';
pw += inkey;
}
}
}
cout << endl;
return pw;
}
 
E

evaned

Hmmm, two things are apparent:

1. Something got screwed up, because I posted a reply before but it
just shows up as a quote of your post.

2. My post was wrong anyway. I had said that that seems like overkill,
because if you're on Windows you probably have access to conio.h and
getch(), but I had forgotten that the benefit of getch() over anything
I know of in the stardard library is not that getch doesn't wait for
input, but doesn't display its output.

Anyway, sorry for the two useless replies then. ;-)
 
S

Stewart Gordon

adbarnet said:
It's off topic in C++, but you need to research keyboard interrupts. Your
main problem is that your application needs to have 'focus' before it can
assume keyboard actions are intended for it - how you do that is definitely
not C++ specific.
<snip top of upside-down reply>

So you're saying the usual methods (be they assembly or library) for
reading the keyboard is to totally hijack keyboard input? Maybe on
whatever OS you're using. We don't know what OS the OP is using, but on
Windows at least, every DOS/console window has its own keyboard buffer.

Stewart.
 
S

Stewart Gordon

SumGie said:
I need a command, (or function, or whatever) that will look at the keyboard
to determine if any keys are being pressed, without pausing if there are
none. The CIN>> command pauses the program to wait for a keypress.
I want a loop that will keep looping, updating the screen endlessly, until I
press a (particular) key to stop it. I'm trying to program a text example
of the game of life. I want to be able to tell it to go, and have it
running generations until I tell it to stop, and then return to a menu mode
where the user can turn individual cells on or off.

Not sure why you'd want to use a menu for this.
My C++ instructor has not been able to tell me a way to do this. Can any of
you? (adding machine language code to my program has been suggested, but is
not an option. We don't cover how to do that until much later in the
course, not to mention that I don't know machine language programming.)

What does your project specification say about being able to do this?
And is it a rule on your course that you may not use language features
you haven't to date been taught on the course?

Maybe your C++ compiler has a library that enables you to read
keystrokes. For example, some DOS/Windows compilers have conio.h; then
the relevant functions are kbhit and getch. OTOH, if no such thing is
to be seen, then you can't do it without a bit of assembly, and so you
might as well stop trying.

Stewart.
 
S

SumGie

Stewart Gordon said:
Not sure why you'd want to use a menu for this.


Um, I haven't thought of a better way. I'm totally new to C++. The class is
still doing console apps. (The output appears in a dos prompt window. No
graphics or such, and no mouse support.)

What does your project specification say about being able to do this? And
is it a rule on your course that you may not use language features you
haven't to date been taught on the course?

He just offered a little extra credit to anyone who can show him an example
of the game before the end of the term (which just began). As for not using
features I haven't been taught, well, no rule against it. As a matter of
fact, that's what I'm trying to do by asking this question here; find out
what is possible so I can go learn it on my own and use it to do this
program. I only said adding machine language wasn't an option because when
the person who suggested it started explaining how, I was very quickly lost.
It is presently above my level of understanding. That's the only block. If
I could come to understand how to do it, it would be perfectly legitimate to
use.
Maybe your C++ compiler has a library that enables you to read keystrokes.
For example, some DOS/Windows compilers have conio.h; then the relevant
functions are kbhit and getch. OTOH, if no such thing is to be seen, then
you can't do it without a bit of assembly, and so you might as well stop
trying.

Last term, we had a linux terminal to program on. This term it's windows xp
with visual C++ 6.0. I don't know how to research what libraries are
available, or what their features are. I'll try including conio.h to see if
it'll compile, and if so, I'll find out what those commands you mentioned
do, and how to use them. Thanks for the suggestions. :)

I'm so used to BASIC, it's hard for me to comprehend that an advanced level
language like C++ could have no provision for sampling input devices without
pausing. That's just... bizarre. I had assumed that, asking my question, I
would have been pointed to a command we hadn't discussed in class. (and
maybe with this post, I have, in the conio.h, kbhit, and getch commands. )
I suppose since my teacher couldn't tell me a way, I shouldn't have assumed
that. Heh.
 
K

Karl Heinz Buchegger

SumGie said:
Last term, we had a linux terminal to program on. This term it's windows xp
with visual C++ 6.0. I don't know how to research what libraries are
available, or what their features are. I'll try including conio.h to see if
it'll compile, and if so, I'll find out what those commands you mentioned
do, and how to use them.

Or make it simpler:
Just type the text _kbhit in some edit window,
position the caret somwhere in this text and hit F1
The IDE will search for the help text for that function.
Thanks for the suggestions. :)

I'm so used to BASIC, it's hard for me to comprehend that an advanced level
language like C++ could have no provision for sampling input devices without
pausing.

Moment.
That's "Standard C++". You would be surprised what 'Standard C++' cannot do.
It has no knowledge about accessing any hardware devices at all. It has
no idea that there is a screen in front of you. It doesn't know that you enter
input with a keyboard or a mouse. It has no idea that there is a file system
with a directory structure. It has no idea of 'Internet' in general and 'WWW'
in special, ....
In short: Standard C++ limits itself to things (with some small exceptions, such
as 'files'), that can be found on any computer. And with 'any', I mean 'any'. Be
it a desktop PC as in your case, or some embedded computer controlling your
microwave oven, or a supercomuter used at NASA to do finite element calculations.

Having said this: All of this does not mean, that it cannot be done. It just
means that 'Standard C++' as defined by ISO does not talk about it. But nobody
is hindering you to use 'non-standard extensions', provided by whoever has
the knowledge (because he actually knows how to talk to the controller of
the embedded computer controlling the ignition system of your car) to
implement them.
 
S

Stewart Gordon

SumGie said:
Um, I haven't thought of a better way.

Actually, it does depend on what you can do. With the aid of a means of
manipulating the cursor position, you could use the cursorkeys and a few
others to edit the field.
I'm totally new to C++. The class is still doing console apps. (The
output appears in a dos prompt window. No graphics or such, and no
mouse support.)

Yes, I expect that's how most people learn to program in any language,
with few exceptions.

Last term, we had a linux terminal to program on. This term it's
windows xp with visual C++ 6.0. I don't know how to research what
libraries are available, or what their features are.
<snip>

Look in the help.

You can also look in the directory (folder, whatever you call it) where
your compiler is installed and see what .h files are there.

For that matter, there's also a Win32 console API that you might like to
check out:

http://msdn.microsoft.com/library/en-us/dllproc/base/console_functions.asp

But this is getting more and more OT for c.l.c++....

Stewart.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
FloridaHan

Latest Threads

Top