C++ Programming Help

Status
Not open for further replies.

smartydebater

Baseband Member
Messages
39
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
 
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.
 
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?
 
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.
 
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.
 
Thanks. It did. I just still can't come up with a way to monitor all keystrokes for the certain character...
 
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.
 
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.
 
^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.
 
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')
 
Status
Not open for further replies.
Back
Top Bottom