这篇文章主要讲解了“基于c++怎么编写一个键盘提示音程序”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“基于C++怎么编写一个键盘提示音程序”吧!准备资源首先我们要下载鸡你太美的音频并剪辑好,
这篇文章主要讲解了“基于c++怎么编写一个键盘提示音程序”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“基于C++怎么编写一个键盘提示音程序”吧!
首先我们要下载鸡你太美的音频并剪辑好,然后再准备一个可爱的图片当作图标。
将准备好的资源添加到工程中。
之后不要忘记在主程序引用资源文件
#include "resource.h"
使用PlaySound函数即可
PlaySound((LPCTSTR)IDR_WAVE1, NULL, SND_RESOURCE | SND_SYNC);
使用GetAsyncKeyState获取键盘按键状态。为了方便使用,我使用了宏定义
#define KEY_DOWN(key_name) ((GetAsyncKeyState(key_name)& 0x8001)?1:0)#define KEY_UP(key_name) ((GetAsyncKeyState(key_name)& 0x8001)?0:1)
然后就是获取键盘输入了
for (;;) { //鸡 if (KEY_DOWN('J')) { if (hasPress[0] == false) { hasPress[0] = true; niganma = 1; thread t1(PlaySounds,0); t1.detach(); } } if (KEY_UP('J')) { hasPress[0] = false; } //你 if (KEY_DOWN('N')) { if (hasPress[1] == false) { hasPress[1] = true; if (niganma == 1) { niganma = 2; } else { niganma = 0; } thread t1(PlaySounds, 1); t1.detach(); } } if (KEY_UP('N')) { hasPress[1] = false; } //太 if (KEY_DOWN('T')) { if (hasPress[2] == false) { hasPress[2] = true; if (niganma == 2) { niganma = 3; } else { niganma = 0; } thread t1(PlaySounds, 2); t1.detach(); } } if (KEY_UP('T')) { hasPress[2] = false; } //美 if (KEY_DOWN('M')) { if (hasPress[3] == false) { hasPress[3] = true; if (niganma == 3) { thread t1(PlaySounds, 4); t1.detach(); niganma = 0; } else { thread t1(PlaySounds, 3); t1.detach(); } } } if (KEY_UP('M')) { hasPress[3] = false; } }
所以,完整的代码如下:
// XiaoHeiZi.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。//#include "resource.h"#include <iOStream>#include<thread>#include <coNIO.h>#include<stdlib.h>#include<windows.h>#include<iostream>#include <Mmsystem.h> #pragma comment(lib, "Winmm.lib") #define KEY_DOWN(key_name) ((GetAsyncKeyState(key_name)& 0x8001)?1:0)#define KEY_UP(key_name) ((GetAsyncKeyState(key_name)& 0x8001)?0:1)#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) using namespace std; bool ModifyRegedit(bool bAutoRun){ char pFileName[MAX_PATH] = { 0 }; DWord dwRet = GetModuleFileNameA(NULL, (LPSTR)pFileName, MAX_PATH); std::cout << pFileName; HKEY hKey; LPCSTR lpRun = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; long lRet = ReGopenKeyExA(HKEY_LOCAL_MacHINE, lpRun, 0, KEY_WRITE, &hKey); if (lRet != ERROR_SUCCESS) { std::cout << "failed"; return false; } if (bAutoRun) RegSetValueA(hKey, "XiaoHeizi", (DWORD)REG_SZ, (LPCSTR)pFileName, MAX_PATH); else RegDeleteValueA(hKey, "XiaoHeizi"); ReGCloseKey(hKey); return true;} static void PlaySounds(int i){ if (i == 0) { PlaySound((LPCTSTR)IDR_WAVE1, NULL, SND_RESOURCE | SND_SYNC); } else if (i == 1) { PlaySound((LPCTSTR)IDR_WAVE4, NULL, SND_RESOURCE | SND_SYNC); } else if (i == 2) { PlaySound((LPCTSTR)IDR_WAVE5, NULL, SND_RESOURCE | SND_SYNC); } else if (i == 3) { PlaySound((LPCTSTR)IDR_WAVE3, NULL, SND_RESOURCE | SND_SYNC); } else { PlaySound((LPCTSTR)IDR_WAVE2, NULL, SND_RESOURCE | SND_SYNC); } }int main(){ ModifyRegedit(true); bool hasPress[4]; for (int i = 0; i < 4; i++) { hasPress[i] = false; } int niganma = 0; for (;;) { //鸡 if (KEY_DOWN('J')) { if (hasPress[0] == false) { hasPress[0] = true; niganma = 1; thread t1(PlaySounds,0); t1.detach(); } } if (KEY_UP('J')) { hasPress[0] = false; } //你 if (KEY_DOWN('N')) { if (hasPress[1] == false) { hasPress[1] = true; if (niganma == 1) { niganma = 2; } else { niganma = 0; } thread t1(PlaySounds, 1); t1.detach(); } } if (KEY_UP('N')) { hasPress[1] = false; } //太 if (KEY_DOWN('T')) { if (hasPress[2] == false) { hasPress[2] = true; if (niganma == 2) { niganma = 3; } else { niganma = 0; } thread t1(PlaySounds, 2); t1.detach(); } } if (KEY_UP('T')) { hasPress[2] = false; } //美 if (KEY_DOWN('M')) { if (hasPress[3] == false) { hasPress[3] = true; if (niganma == 3) { thread t1(PlaySounds, 4); t1.detach(); niganma = 0; } else { thread t1(PlaySounds, 3); t1.detach(); } } } if (KEY_UP('M')) { hasPress[3] = false; } }}
感谢各位的阅读,以上就是“基于C++怎么编写一个键盘提示音程序”的内容了,经过本文的学习后,相信大家对基于C++怎么编写一个键盘提示音程序这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!
--结束END--
本文标题: 基于C++怎么编写一个键盘提示音程序
本文链接: https://www.lsjlt.com/news/351073.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0