473,382 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How do I...?

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!
Jul 23 '05 #1
11 1677
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" <su****@comcast.net> wrote in message
news:Vo********************@comcast.com...
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!


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 23 '05 #2
SumGie schrieb:
I want a loop that will keep looping, updating the screen endlessly, until
I press a (particular) key to stop it.


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/ ---]
Jul 23 '05 #3
"SumGie" writes:
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.
Jul 23 '05 #4

"SumGie" <su****@comcast.net> wrote in message
news:Vo********************@comcast.com...
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;
}
Jul 23 '05 #5

Joe C wrote:
"SumGie" <su****@comcast.net> wrote in message
news:Vo********************@comcast.com...
I need a command, (or function, or whatever) that will look at the keyboardto determine if any keys are being pressed, without pausing if there arenone. 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;
}


Jul 23 '05 #6
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. ;-)

Jul 23 '05 #7
adbarnet wrote:
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.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
the 'group where everyone may benefit.
Jul 23 '05 #8
SumGie wrote:
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.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
the 'group where everyone may benefit.
Jul 23 '05 #9

"Stewart Gordon" <sm*******@yahoo.com> wrote in message
news:d1*********@sun-cc204.lut.ac.uk...
SumGie wrote:
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.

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

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?


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

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

Jul 23 '05 #10
SumGie wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #11
SumGie wrote:
"Stewart Gordon" <sm*******@yahoo.com> wrote in message
news:d1*********@sun-cc204.lut.ac.uk...
SumGie wrote:
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.


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.

<snip> 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..._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.
Jul 23 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.