| | |
Using keyboard presses to trigger internal speaker sound
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 2
Reputation:
Solved Threads: 0
Hi everyone, basically i'm writing a simple program in c++ to trigger a beep from the internal speaker much like a musical keyboard except i'm having trouble with capturing key press
how would i be able to trap a key press then initiate a beep, i have tried using kbhit() in a If statement but not able to detect the keyboard press, as i dont know the keyboard button matrix addresses here is my code, tell me what you think
the program starts by a simple text statement then plays a small tune like the first nine notes from star wars as an introduction
the keychk is just an array i was going to use to compare the key press to the number pad as i thought it would be useful
the kbhit if statement is just an example i left in to show you what i ment, hope this helps you help me (the printf will be replaced with a beep)
thanks in advance, Joe
how would i be able to trap a key press then initiate a beep, i have tried using kbhit() in a If statement but not able to detect the keyboard press, as i dont know the keyboard button matrix addresses here is my code, tell me what you think
C++ Syntax (Toggle Plain Text)
/*Author:Joseph Ioannou Description: Simple program which detects input from keyboard and makes sounds :) */ #include <stdio.h> #include <conio.h> #include <windows.h> // WinApi header file //Sleep(500); // 500 ms delay //Beep(523,500); // 523 hertz (C5) for 500 milliseconds int keychk[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // simple key check int array int main() { puts(""); // 523 hertz (C5) for 500 milliseconds Beep(500,1000); Beep(500,1000); Beep(500,1000); Beep(400,800); Beep(500,500); Beep(600,500); Beep(400,800); Beep(500,500); Beep(600,500); puts("Waiting for key press to begin"); getchar(); // key wait printf("Please press a key"); if(kbhit()!='a') { printf("a key is pressed"); } else return 0; }
the program starts by a simple text statement then plays a small tune like the first nine notes from star wars as an introduction
the keychk is just an array i was going to use to compare the key press to the number pad as i thought it would be useful
the kbhit if statement is just an example i left in to show you what i ment, hope this helps you help me (the printf will be replaced with a beep)
thanks in advance, Joe
ya using the getch is the good idea to tap the keyboard..
but i guess its not pretty good idea to use the getch() directly
since it waits for the keystroke.. always and pause the screen...
you can use it like this..
for eg..
I have always wonder how to tap the Function key (F1, F2, F3... F12)... Somebody know then help us out..
but i guess its not pretty good idea to use the getch() directly
since it waits for the keystroke.. always and pause the screen...
you can use it like this..
for eg..
C++ Syntax (Toggle Plain Text)
if(kbhit()) { switch(getch()) //monitor key stroke { case 80: //Down arrow Focus(CFP+1); break; case 72: //Up arrow Focus(CFP-1); break; case 13: //Enter RESPOND=CFP; break; case 27: //Esc RESPOND=27; } }
I have always wonder how to tap the Function key (F1, F2, F3... F12)... Somebody know then help us out..
Last edited by cscgal; Nov 13th, 2008 at 4:24 pm. Reason: Fixed code tags
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
ya using the getch is the good idea to tap the keyboard..
but i guess its not pretty good idea to use the getch() directly
since it waits for the keystroke.. always and pause the screen...
you can use it like this..
for eg..
I have always wonder how to tap the Function key (F1, F2, F3... F12)... Somebody know then help us out..
but i guess its not pretty good idea to use the getch() directly
since it waits for the keystroke.. always and pause the screen...
you can use it like this..
for eg..
C++ Syntax (Toggle Plain Text)
if(kbhit()) { switch(getch()) //monitor key stroke { case 80: //Down arrow Focus(CFP+1); break; case 72: //Up arrow Focus(CFP-1); break; case 13: //Enter RESPOND=CFP; break; case 27: //Esc RESPOND=27; } }
I have always wonder how to tap the Function key (F1, F2, F3... F12)... Somebody know then help us out..
Last edited by cscgal; Nov 13th, 2008 at 4:23 pm. Reason: Fixed code tags
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
If you're going to use windows.h's Beep() instead of the old non-ANSI _beep(), you might as well use Window's GetKeyState(), or GetKeyboardState().
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
![]() |
Other Threads in the C++ Forum
- Previous Thread: c++ STL LIST function
- Next Thread: Need help reading from .txt file!!
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





