色々と意見を頂きましたので、新しく組みなおしてみました。
開発はVC++6.0で行いました。
まずはヘッダを…
#pragma once
#include <vector>
#include <limits>
#include "DxLib.h"
class Keyboard
{
private:
static std::vector<bool> m_vbOnOff;
static std::vector<bool> m_vbRelease;
static std::vector<bool> m_vbRepeatOnOff;
static std::vector<bool> m_vbRepeatEnable;
static std::vector<unsigned int> m_vuRepeatCount;
static std::vector<unsigned int> m_vuRepeatInterval;
static std::vector<unsigned int> m_vuPressCount;
private:
Keyboard(void);
Keyboard(const Keyboard& cKeyboard);
public:
static bool Refresh(void);
static const bool GetOnOFF(int nKeyCode);
static const bool GetPress(int nKeyCode);
static const bool GetRelease(int nKeyCode);
static const unsigned int GetPressCount(int nKeyCode);
static void SetRepeat(int nKeyCode, bool bEnable, unsigned int uInterval = 1);
};
次に実装部を…
#include "Keyboard.h"
// スタティックメンバの初期化
std::vector<bool> Keyboard::m_vbOnOff(256);
std::vector<bool> Keyboard::m_vbRelease(256);
std::vector<bool> Keyboard::m_vbRepeatOnOff(256);
std::vector<bool> Keyboard::m_vbRepeatEnable(256);
std::vector<unsigned int> Keyboard::m_vuRepeatCount(256);
std::vector<unsigned int> Keyboard::m_vuRepeatInterval(256);
std::vector<unsigned int> Keyboard::m_vuPressCount(256);
// コンストラクタ
Keyboard::Keyboard(void)
{
// ベクタクラスの初期化
std::fill_n(m_vbOnOff.begin(), m_vbOnOff.size(), false);
std::fill_n(m_vbRelease.begin(), m_vbRelease.size(), false);
std::fill_n(m_vbRepeatOnOff.begin(), m_vbRepeatOnOff.size(), false);
std::fill_n(m_vbRepeatEnable.begin(), m_vbRepeatEnable.size(), false);
std::fill_n(m_vuRepeatCount.begin(), m_vuRepeatCount.size(), 0);
std::fill_n(m_vuRepeatInterval.begin(), m_vuRepeatInterval.size(), 0);
std::fill_n(m_vuPressCount.begin(), m_vuPressCount.size(), 0);
}
// キーボード情報の更新
bool Keyboard::Refresh(void)
{
// キーボードの押下状態の更新
char chKeybuf[256] = {0, };
if (GetHitKeyStateAll(chKeybuf) != 0)
{
// GetHitKeyStateAll関数が失敗したのでfalseを返す
return false;
}
else
{
for (int i = 0; i < 256; ++i)
{
m_vbOnOff = chKeybuf == 1 ? true : false;
}
}
for (int i = 0; i < 256; ++i)
{
if (m_vbRepeatEnable == true)
{
if (++m_vuRepeatCount > m_vuRepeatInterval)
{
m_vuRepeatCount = 0;
m_vbRepeatOnOff = m_vbRepeatOnOff == true ? false : true;
}
m_vbOnOff = m_vbRepeatOnOff;
}
if (m_vbOnOff[i] == true)
{
m_vuPressCount[i] += m_vuPressCount[i] < (unsigned int)(std::numeric_limits<int>::max)() ? 1 : 0;
}
else
{
m_vbRelease[i] = m_vuPressCount[i] > 0 ? true : false;
m_vuPressCount[i] = 0;
}
}
return true;
}
// 単純なオン・オフの取得
// int nKeyCode = DXライブラリで設定されてあるキーコード
const bool Keyboard::GetOnOFF(int nKeyCode)
{
return m_vbOnOff[nKeyCode];
}
// 今押されたかどうかの取得
// int nKeyCode = DXライブラリで設定されてあるキーコード
const bool Keyboard::GetPress(int nKeyCode)
{
return m_vuPressCount[nKeyCode] == 1 ? true : false;
}
// 今離されたかどうかの取得
// int nKeyCode = DXライブラリで設定されてあるキーコード
const bool Keyboard::GetRelease(int nKeyCode)
{
return m_vbRelease[nKeyCode];
}
// 押しっ放しのカウント数の取得
// int nKeyCode = DXライブラリで設定されてあるキーコード
const unsigned int Keyboard::GetPressCount(int nKeyCode)
{
return m_vuPressCount[nKeyCode];
}
// リピート機能の設定
// int nKeyCode = DXライブラリで設定されてあるキーコード
// bool bEnable = true : リピートのオン false : リピートのオフ
// unsigned int uInterval = オン・オフの間隔
void Keyboard::SetRepeat(int nKeyCode, bool bEnable, unsigned int uInterval)
{
m_vbRepeatOnOff[nKeyCode] = true;
m_vbRepeatEnable[nKeyCode] = bEnable;
m_vuRepeatInterval[nKeyCode] = uInterval;
m_vuRepeatCount[nKeyCode] = 0;
}
最後に使用例を…
#include "DxLib.h"
#include "Keyboard.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
ChangeWindowMode(TRUE);
if (DxLib_Init() == -1)
{
return -1;
}
// エンターキーのオートリピートを有効にする
Keyboard::SetRepeat(KEY_INPUT_RETURN, true, 25);
SetDrawScreen(DX_SCREEN_BACK);
while (ProcessMessage() == 0 && Keyboard::Refresh() == true && ClearDrawScreen() == 0)
{
// エンターキーの情報を表示
DrawFormatString(0, 0, GetColor(0xFF, 0xFF, 0xFF), "OnOff = %d\n", (int)(Keyboard::GetOnOFF(KEY_INPUT_RETURN)));
DrawFormatString(0, 32, GetColor(0xFF, 0xFF, 0xFF), "Press = %d\n", (int)(Keyboard::GetPress(KEY_INPUT_RETURN)));
DrawFormatString(0, 64, GetColor(0xFF, 0xFF, 0xFF), "Release = %d\n", (int)(Keyboard::GetRelease(KEY_INPUT_RETURN)));
DrawFormatString(0, 96, GetColor(0xFF, 0xFF, 0xFF), "Count = %u\n", (int)(Keyboard::GetPressCount(KEY_INPUT_RETURN)));
ScreenFlip();
// Aが押されたら終了させる
if (Keyboard::GetPress(KEY_INPUT_A) == true)
{
break;
}
Sleep(20);
}
DxLib_End();
return 0;
}