您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要讲解了“基于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++怎么编写一个键盘提示音程序这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。