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

/*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

why do you need kbhit? kbhit works more like a detector if key is pressed, it doesn't wait for keypress.
Simply use getch?

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

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

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.