目录
  • 准备资源
  • 播放声音
  • 获取键盘按键
  • 完整代码

首先讲一下思路,这次制作的小黑子相当于键盘提示音,输入J,N,T,M,会发出“鸡你太美”的声音,连续按下JNTM则会发出“你干嘛啊,哎呦”的声音。

完整的工程和代码以及成品已经上传至百度网盘,喜欢的同学可以下载:

链接:https://pan.baidu.com/s/1IjPBB1dhz-ZPSP8SpKWq8Q  提取码: 5smw

准备资源

首先我们要下载鸡你太美的音频并剪辑好,然后再准备一个可爱的图片当作图标。我已经准备好了一份,想要的同学可以直接用。

将准备好的资源添加到工程中。

之后不要忘记在主程序引用资源文件

#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++键盘提示音程序的资料请关注本网站其它相关文章!

您可能感兴趣的文章:

  • C++游戏编程之模拟实现键盘打字程序
  • c++调用windows键盘代码详情
  • C++模拟键盘按键的实例
  • C++检测键盘某键是否按下的方法
  • C/C++获取键盘事件的方法
  • C++键盘记录程序代码
  • C++处理键盘输入的方法