Mega Code Archive

 
Categories / Delphi / API
 

Timed beeps from the pc speaker

Question: How can I beep the PC Speaker several times in a row, and have a small delay between beeps that is reliable across different machines with different clock speeds? Answer: The following demonstrates using a delay function that can be used to beep the PC speaker. Example: procedure Delay(ms : longint); {$IFNDEF WIN32} var TheTime : LongInt; {$ENDIF} begin {$IFDEF WIN32} Sleep(ms); {$ELSE} TheTime := GetTickCount + ms; while GetTickCount < TheTime do Application.ProcessMessages; {$ENDIF} end; procedure TForm1.Button1Click(Sender: TObject); begin MessageBeep(word(-1)); Delay(200); MessageBeep(word(-1)); Delay(200); MessageBeep(word(-1)); end;