3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DXライブラリでキーが押された瞬間を判別する

Posted at

掲題の通りです。
質問があったもので

#include "DxLib.h"

// キー入力
int key[256]; // 0:入力されていない 1:入力された瞬間 2:入力されている
void inputKey() {
	static char buf[256];
	GetHitKeyStateAll(buf);
	for (int i = 0; i < 256; i++) {
		if (buf[i]) {
			if (key[i] == 0) key[i] = 1;
			else if (key[i] == 1) key[i] = 2;
		} else key[i] = 0;
	}
}

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
	ChangeWindowMode(TRUE);
	DxLib_Init();
	SetDrawScreen(DX_SCREEN_BACK);
	while (!ProcessMessage()) {
		ClearDrawScreen();
		inputKey();
		if (key[KEY_INPUT_SPACE] == 1) {
			DrawString(0, 0, "SPACE押された瞬間", GetColor(255, 255, 255));
		} else if (key[KEY_INPUT_SPACE] == 2) {
			DrawString(0, 24, "SPACE押されている", GetColor(255, 255, 255));
		}
		ScreenFlip();
	}
	DxLib_End();
	return 0;
}
3
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?