Connecting Tech Pros Worldwide Forums | Help | Site Map

How do I...?

SumGie
Guest
 
Posts: n/a
#1: Jul 23 '05
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!



adbarnet
Guest
 
Posts: n/a
#2: Jul 23 '05

re: How do I...?


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



"SumGie" <sumgie@comcast.net> wrote in message
news:Vo6dnR4sRo6Bg6_fRVn-uw@comcast.com...[color=blue]
>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!
>
>[/color]



Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Arne Schmitz
Guest
 
Posts: n/a
#3: Jul 23 '05

re: How do I...?


SumGie schrieb:
[color=blue]
> I want a loop that will keep looping, updating the screen endlessly, until
> I press a (particular) key to stop it.[/color]

Definitely not C++ specific, but go and have a look at the SDL library:

http://www.libsdl.org/

Arne

--
[--- PGP key FD05BED7 --- http://www.root42.de/ ---]
osmium
Guest
 
Posts: n/a
#4: Jul 23 '05

re: How do I...?


"SumGie" writes:
[color=blue]
>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.)[/color]

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.


Joe C
Guest
 
Posts: n/a
#5: Jul 23 '05

re: How do I...?



"SumGie" <sumgie@comcast.net> wrote in message
news:Vo6dnR4sRo6Bg6_fRVn-uw@comcast.com...[color=blue]
>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!
>[/color]

[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;
}


evaned@gmail.com
Guest
 
Posts: n/a
#6: Jul 23 '05

re: How do I...?



Joe C wrote:[color=blue]
> "SumGie" <sumgie@comcast.net> wrote in message
> news:Vo6dnR4sRo6Bg6_fRVn-uw@comcast.com...[color=green]
> >I need a command, (or function, or whatever) that will look at the[/color][/color]
keyboard[color=blue][color=green]
> >to determine if any keys are being pressed, without pausing if there[/color][/color]
are[color=blue][color=green]
> >none. The CIN>> command pauses the program to wait for a keypress.
> > I want a loop that will keep looping, updating the screen[/color][/color]
endlessly, until[color=blue][color=green]
> > I press a (particular) key to stop it. I'm trying to program a[/color][/color]
text[color=blue][color=green]
> > example of the game of life. I want to be able to tell it to go,[/color][/color]
and have[color=blue][color=green]
> > it running generations until I tell it to stop, and then return to[/color][/color]
a menu[color=blue][color=green]
> > 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.[/color][/color]
Can any[color=blue][color=green]
> > of you? (adding machine language code to my program has been[/color][/color]
suggested,[color=blue][color=green]
> > but is not an option. We don't cover how to do that until much[/color][/color]
later in[color=blue][color=green]
> > the course, not to mention that I don't know machine language
> > programming.)
> >
> > Thanks all!
> >[/color]
>
> [OT] If you are on a windows system...this compilable code may be[/color]
helpful.[color=blue]
>
> #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,[/color]
etc.[color=blue]
> cout << '*';
> pw += inkey;
> }
> }
> }
> cout << endl;
> return pw;
> }[/color]

evaned@gmail.com
Guest
 
Posts: n/a
#7: Jul 23 '05

re: How do I...?


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. ;-)

Stewart Gordon
Guest
 
Posts: n/a
#8: Jul 23 '05

re: How do I...?


adbarnet wrote:[color=blue]
> 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.[/color]
<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.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
the 'group where everyone may benefit.
Stewart Gordon
Guest
 
Posts: n/a
#9: Jul 23 '05

re: How do I...?


SumGie wrote:[color=blue]
> 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.[/color]

Not sure why you'd want to use a menu for this.
[color=blue]
> 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.)[/color]

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.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
the 'group where everyone may benefit.
SumGie
Guest
 
Posts: n/a
#10: Jul 23 '05

re: How do I...?



"Stewart Gordon" <smjg_1998@yahoo.com> wrote in message
news:d16tkl$le$1@sun-cc204.lut.ac.uk...[color=blue]
> SumGie wrote:[color=green]
>> 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.[/color]
>
> Not sure why you'd want to use a menu for this.[/color]


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.)

[color=blue]
>[color=green]
>> 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.)[/color]
>
> 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?
>[/color]

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.
[color=blue]
> 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.
>[/color]

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.
[color=blue]
> Stewart.
>
> --
> My e-mail is valid but not my primary mailbox. Please keep replies on the
> 'group where everyone may benefit.[/color]


Karl Heinz Buchegger
Guest
 
Posts: n/a
#11: Jul 23 '05

re: How do I...?


SumGie wrote:[color=blue]
>
> 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.[/color]

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.
[color=blue]
> 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.[/color]

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.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Stewart Gordon
Guest
 
Posts: n/a
#12: Jul 23 '05

re: How do I...?


SumGie wrote:[color=blue]
> "Stewart Gordon" <smjg_1998@yahoo.com> wrote in message
> news:d16tkl$le$1@sun-cc204.lut.ac.uk...
>[color=green]
>> SumGie wrote:
>>[color=darkred]
>>> 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.[/color]
>>
>> Not sure why you'd want to use a menu for this.[/color]
>
> Um, I haven't thought of a better way.[/color]

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.
[color=blue]
> 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.)[/color]

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

<snip>[color=blue]
> 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.[/color]
<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..._functions.asp

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

Stewart.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
the 'group where everyone may benefit.
Closed Thread