943,890 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1976
  • C++ RSS
Nov 11th, 2008
0

Using keyboard presses to trigger internal speaker sound

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
britishemo15 is offline Offline
2 posts
since Feb 2008
Nov 11th, 2008
0

Re: Using keyboard presses to trigger internal speaker sound

why do you need kbhit? kbhit works more like a detector if key is pressed, it doesn't wait for keypress.
Simply use getch?
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Nov 13th, 2008
0

Re: Using keyboard presses to trigger internal speaker sound

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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
Rhohitman is offline Offline
81 posts
since Dec 2007
Nov 13th, 2008
0

Re: Using keyboard presses to trigger internal speaker sound

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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
Rhohitman is offline Offline
81 posts
since Dec 2007
Nov 13th, 2008
0

Re: Using keyboard presses to trigger internal speaker sound

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().
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: c++ STL LIST function
Next Thread in C++ Forum Timeline: Need help reading from .txt file!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC