您好,登錄后才能下訂單哦!
這篇文章主要講解了“基于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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。