C语言按键声光报警系统
时间: 2024-05-01 10:16:07 浏览: 18
以下是一个简单的C语言按键声光报警系统的代码示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
int key;
printf("Enter the key (0-9): ");
scanf("%d", &key);
if(key >= 0 && key <= 9)
{
printf("Key pressed: %d\n", key);
printf("Alarm activated!\n");
Beep(1000, 2000); // 1000 Hz tone for 2 seconds
}
else
{
printf("Invalid key entered!\n");
}
return 0;
}
```
这个程序会要求用户输入一个数字键(0-9),如果输入的键在这个范围内,程序将打印出“Key pressed: [输入的键]”,然后发出连续2秒的1000 Hz蜂鸣声。如果输入的键不在这个范围内,程序将打印出“Invalid key entered!”。请注意,Beep函数只在Windows操作系统上可用。如果你在其他操作系统上运行这个程序,需要使用其他方法来产生声音。
如果你想要加入闪光灯报警功能,可以使用Windows API中的FlashWindowEx函数来实现。具体方法可参考以下代码示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
int key;
printf("Enter the key (0-9): ");
scanf("%d", &key);
if(key >= 0 && key <= 9)
{
printf("Key pressed: %d\n", key);
printf("Alarm activated!\n");
Beep(1000, 2000); // 1000 Hz tone for 2 seconds
// flash the window
FLASHWINFO fi;
fi.cbSize = sizeof(FLASHWINFO);
fi.hwnd = GetConsoleWindow();
fi.dwFlags = FLASHW_ALL | FLASHW_TIMERNOFG;
fi.uCount = 5;
fi.dwTimeout = 0;
FlashWindowEx(&fi);
}
else
{
printf("Invalid key entered!\n");
}
return 0;
}
```
这个程序在蜂鸣声之后,将使用FlashWindowEx函数闪烁控制台窗口5次,以提醒用户注意。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)