基于C++編寫一個(gè)鍵盤提示音程序
首先講一下思路,這次制作的小黑子相當(dāng)于鍵盤提示音,輸入J,N,T,M,會(huì)發(fā)出“雞你太美”的聲音,連續(xù)按下JNTM則會(huì)發(fā)出“你干嘛啊,哎呦”的聲音。
完整的工程和代碼以及成品已經(jīng)上傳至百度網(wǎng)盤,喜歡的同學(xué)可以下載:
鏈接:https://pan.baidu.com/s/1IjPBB1dhz-ZPSP8SpKWq8Q 提取碼: 5smw
準(zhǔn)備資源
首先我們要下載雞你太美的音頻并剪輯好,然后再準(zhǔn)備一個(gè)可愛的圖片當(dāng)作圖標(biāo)。我已經(jīng)準(zhǔn)備好了一份,想要的同學(xué)可以直接用。
將準(zhǔn)備好的資源添加到工程中。
之后不要忘記在主程序引用資源文件
#include "resource.h"
播放聲音
使用PlaySound函數(shù)即可
PlaySound((LPCTSTR)IDR_WAVE1, NULL, SND_RESOURCE | SND_SYNC);
獲取鍵盤按鍵
使用GetAsyncKeyState獲取鍵盤按鍵狀態(tài)。為了方便使用,我使用了宏定義
#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" 函數(shù)。程序執(zhí)行將在此處開始并結(jié)束。 // #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++編寫一個(gè)鍵盤提示音程序的詳細(xì)內(nèi)容,更多關(guān)于C++鍵盤提示音程序的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++的std::vector<bool>轉(zhuǎn)儲(chǔ)文件問(wèn)題
這篇文章主要介紹了C++的std::vector<bool>轉(zhuǎn)儲(chǔ)文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11C語(yǔ)言數(shù)據(jù)(整數(shù)、浮點(diǎn)數(shù))在內(nèi)存中的存儲(chǔ)
之前對(duì)c語(yǔ)言數(shù)據(jù)存儲(chǔ)一直不太明白,最近仔細(xì)研究了一番,所以下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言數(shù)據(jù)(整數(shù)、浮點(diǎn)數(shù))在內(nèi)存中存儲(chǔ)的相關(guān)資料,需要的朋友可以參考下2021-06-06C++ 遞歸遍歷文件并計(jì)算MD5的實(shí)例代碼
在本篇文章里小編給大家整理的是一篇關(guān)于C++ 遞歸遍歷文件并計(jì)算MD5的實(shí)例代碼,有興趣的朋友們可以學(xué)習(xí)參考下。2021-07-07你知道C語(yǔ)言函數(shù)調(diào)用常用的2種方式嗎
本篇博客會(huì)講解C語(yǔ)言函數(shù)調(diào)用的2種方式,分別是:傳值調(diào)用和傳址調(diào)用。這2種函數(shù)調(diào)用方式有什么區(qū)別呢?為什么會(huì)有不同的效果呢?分別有哪些用途呢?下面就來(lái)一一展開2023-04-04C++實(shí)現(xiàn)將數(shù)據(jù)寫入Excel工作表的示例代碼
直觀的界面、出色的計(jì)算功能和圖表工具,使Excel成為最流行的個(gè)人計(jì)算機(jī)數(shù)據(jù)處理軟件。在本文中,您將學(xué)習(xí)如何使用?Spire.XLS?for?C++?創(chuàng)建?Excel?文檔,以及如何將數(shù)據(jù)寫入?Excel?工作表2023-03-03