基于C++怎么编写一个键盘提示音程序

作者:有用网 阅读量:217 发布时间:2023-11-14
关键字 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 = "SOFTWAREMicrosoftWindowsCurrentVersionRun";
    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;
        }
    }
}

#发表评论
提交评论