Using keyboard presses to trigger internal speaker sound

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 2
Reputation: britishemo15 is an unknown quantity at this point 
Solved Threads: 0
britishemo15 britishemo15 is offline Offline
Newbie Poster

Using keyboard presses to trigger internal speaker sound

 
0
  #1
Nov 11th, 2008
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

  1. /*Author:Joseph Ioannou
  2. Description: Simple program which detects input from keyboard and makes sounds :) */
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <windows.h> // WinApi header file
  6.  
  7. //Sleep(500); // 500 ms delay
  8. //Beep(523,500); // 523 hertz (C5) for 500 milliseconds
  9. int keychk[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // simple key check int array
  10.  
  11. int main()
  12. {
  13. puts("");
  14. // 523 hertz (C5) for 500 milliseconds
  15. Beep(500,1000);
  16. Beep(500,1000);
  17. Beep(500,1000);
  18. Beep(400,800);
  19. Beep(500,500);
  20. Beep(600,500);
  21. Beep(400,800);
  22. Beep(500,500);
  23. Beep(600,500);
  24. puts("Waiting for key press to begin");
  25. getchar(); // key wait
  26.  
  27. printf("Please press a key");
  28. if(kbhit()!='a')
  29. {
  30. printf("a key is pressed");
  31. }
  32. else return 0;
  33. }


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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Using keyboard presses to trigger internal speaker sound

 
0
  #2
Nov 11th, 2008
why do you need kbhit? kbhit works more like a detector if key is pressed, it doesn't wait for keypress.
Simply use getch?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: Using keyboard presses to trigger internal speaker sound

 
0
  #3
Nov 13th, 2008
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..

  1. if(kbhit())
  2. {
  3. switch(getch()) //monitor key stroke
  4. {
  5. case 80: //Down arrow
  6. Focus(CFP+1); break;
  7.  
  8. case 72: //Up arrow
  9. Focus(CFP-1); break;
  10.  
  11. case 13: //Enter
  12. RESPOND=CFP; break;
  13.  
  14. case 27: //Esc
  15. RESPOND=27;
  16. }
  17. }

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.....
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: Using keyboard presses to trigger internal speaker sound

 
0
  #4
Nov 13th, 2008
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..

  1. if(kbhit())
  2. {
  3. switch(getch()) //monitor key stroke
  4. {
  5. case 80: //Down arrow
  6. Focus(CFP+1); break;
  7.  
  8. case 72: //Up arrow
  9. Focus(CFP-1); break;
  10.  
  11. case 13: //Enter
  12. RESPOND=CFP; break;
  13.  
  14. case 27: //Esc
  15. RESPOND=27;
  16. }
  17. }

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.....
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 943
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is online now Online
Posting Shark

Re: Using keyboard presses to trigger internal speaker sound

 
0
  #5
Nov 13th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC