Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 08-26-2008, 03:57 PM   #1 (permalink)
 
Newb Techie

Join Date: Jan 2006

Posts: 39

smartydebater

Question C++ Programming Help

Hi. I'm trying to make a program that would make a beep noise (on a computers motherboard) every time a user presses a certain key such as "A" or "B" etc... So far I've come up with the code to make the beep noise, but I'm not sure how to gather the keystrokes. Any suggestions?

Code:
//C++ code -- motherboard beep
//compiled in dev-c++ version 4.9.9.2
#include <iostream>
#include <windows.h> // WinApi header

using namespace std;

int main()
{
Beep(523,500); // 523 hertz for 500 milliseconds

cin.get(); // wait
return 0; //no errors
}
Thanks,
David
smartydebater is offline  
Old 08-26-2008, 09:19 PM   #2 (permalink)
 
Newb Techie

Join Date: Aug 2008

Posts: 16

george_1988 is on a distinguished road

Default Re: C++ Programming Help

As far as I know there is no functions for determining keypresses, however I believe there many be a way around this for detecting ANY key press. Sorry I don't know how to determine a given key press such as A or B.

Although this is not C++ code you should be able to implement it.

Code:
char key;

puts("press any key...");
getch(key);

if(getch != '\0')
{
     code for "not equal to null" (in other words, if a key is pressed)
}
else
{
     code for "is equal to null" (in other words, if a key is NOT presssed)
}
I hope this helps you in some way. If not, then I am clueless.

Last edited by george_1988; 08-26-2008 at 09:21 PM.
george_1988 is offline  
Old 08-26-2008, 10:57 PM   #3 (permalink)
 
Newb Techie

Join Date: Jan 2006

Posts: 39

smartydebater

Default Re: C++ Programming Help

Dang, I'm still stumped! I'm sure there's some easy way to do it that we're overlooking... but basically I want a program that constantly monitors keystrokes and once a certain key is pressed it plays a sound. I'm guessing I'd want an endless loop to monitor keystrokes?

Last edited by smartydebater; 08-26-2008 at 11:00 PM.
smartydebater is offline  
Old 08-26-2008, 11:06 PM   #4 (permalink)
 
Monster Techie

Join Date: May 2004

Location: /usr/root/mn/us

Posts: 1,121

bla!! is on a distinguished road

Default Re: C++ Programming Help

There's many way to read input.

stdio.h would have most of them


scanf
fscanf
fgetc
getc

Here's a list with references
cstdio (stdio.h) - C++ Reference

Then all you need to do is compare the input with the ASCII of each letter
If it matches the letter you want, have it beep, if not skip

Embed this all in a loop and you're set.
__________________
<br>
Its a frigging Laptop, not a Labtop!!!!
bla!! is offline  
Old 08-27-2008, 03:24 PM   #5 (permalink)
 
Newb Techie

Join Date: Aug 2008

Posts: 16

george_1988 is on a distinguished road

Default Re: C++ Programming Help

Try this and see if it's to your liking. You should be able to convert to C++ very easily.

Code:
	char ch;

	puts("Press any key... ");
  	scanf("%c", &ch);
	
	if(ch == 'A' || ch == 'a')
		// beep code goes here
	else
 		// whatever code goes here
One thing you should note is that this will be calling a scanf function, I believe the equivalent is the C IN (not sure of the code) function in C++.

As far as I am aware, to constantly "monitor" the keystrokes would require some sort of input stream to be created. There are no standard C/C++ library functions to do this. There may very well be ways to do this by invoking the Win32 API, but that's a topic I have no knowledge on.

Hope this helped in some way.
george_1988 is offline  
Old 08-27-2008, 03:34 PM   #6 (permalink)
 
Newb Techie

Join Date: Jan 2006

Posts: 39

smartydebater

Default Re: C++ Programming Help

Thanks. It did. I just still can't come up with a way to monitor all keystrokes for the certain character...

Last edited by smartydebater; 08-27-2008 at 03:48 PM.
smartydebater is offline  
Old 08-27-2008, 05:18 PM   #7 (permalink)
 
Newb Techie

Join Date: Aug 2008

Posts: 16

george_1988 is on a distinguished road

Default Re: C++ Programming Help

This is not as simple as it sounds. The scanf and if statement method is about the only simple way you're gonna do this. You would need these in an infinite loop, constantly prompting the user for input. This is not "monitoring" as you put it, simply prompting the user and waiting. I'm sure I understand what you're asking, that is for the code to monitor the keystrokes in the background, so to speak, while the program does other things.

To do what you want is not as easy as it sounds... sorry.
george_1988 is offline  
Old 08-31-2008, 03:52 AM   #8 (permalink)
Baez's Avatar
 

Join Date: Sep 2005

Location: Toronto, Canada

Posts: 5,465

Baez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of light

Default Re: C++ Programming Help

What do you mean by "monitor" though? Do you want all your keystrokes to be written to a text file or do you just want them put out to the screen? Running an infinite loop to log keys to a text file or output to the screen is pretty simple I'm not sure what george is talking about. Let me know what you would like and I'll put a quick function together for you.
__________________

Baez is offline  
Old 08-31-2008, 08:00 AM   #9 (permalink)
S0ULphIRE's Avatar
 
01001100011011110110110

Join Date: Mar 2007

Location: Perth, Australia

Posts: 1,940

S0ULphIRE has a spectacular aura aboutS0ULphIRE has a spectacular aura about

Send a message via MSN to S0ULphIRE
Default Re: C++ Programming Help

^he explains what he wants pretty well in the first post I think.
um, not too sure about character checking and such, but you could try this:

while (getchar()!='A');

you might need to split the statement up a little, compiler might not like that.

that should just wait until getchar equals the letter A, then add what you want to do after that.
__________________
"As a result of all this hardship, dirt, thirst, and wombats, you would expect Australians to be a dour lot. Instead, they are genial, jolly, cheerful, and always willing to share a kind word with a stranger, unless they are an American."
-- Douglas Adams

Click this if I helped you
>>>><<<<
S0ULphIRE is offline  
Old 08-31-2008, 02:32 PM   #10 (permalink)
Baez's Avatar
 

Join Date: Sep 2005

Location: Toronto, Canada

Posts: 5,465

Baez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of light

Default Re: C++ Programming Help

Yeah he doesn't really explain how he wants to "gather" the keystrokes that's basically what I was asking.

To elaborate on what you just suggested, it's a very good idea but most compilers won't accept getchar as a left operand. In order for it to work you would need to do something like:

Code:
char c;

do
{
c=getchar();
}while(c != 'A')

__________________


Last edited by Baez; 09-01-2008 at 01:22 AM.
Baez is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
The A-Z of Programming Languages: JavaScript Osiris Programming Discussions 0 08-04-2008 09:35 AM
Payments Programming Rex100 Programming Discussions 2 07-14-2008 09:30 PM
New to programming completely... wanna make use of the school years vernong1992 Programming Discussions 6 07-31-2007 04:41 PM
New Hack Technique Exploits Common Programming Error Osiris Virus - Spyware Protection / Detection 0 07-23-2007 02:26 PM