原谅我今天才学会使用win32的按键消息处理!

之前一直写directX的程序,用到的win32消息并不多,而且大部分的按键捕获都是有directInput完成的,因为能捕获到手柄那样的按键(而且据说还是实时的消息捕获)。。。


今天当我想要自己写一个win32的按键消息处理时就晕了。。


各种google、baidu出来的结果都要么是mfc的要么是写到windows的回调函数里面,比如这样的:


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_KEYDOWN:
		 	switch (wParam)
		 	{
		 	case VK_LEFT:
		 		PlayerManger::GetInstance()->SetWriteMessage("$s10");
		 		break;
		 	case VK_ESCAPE:
		 		PlayerManger::GetInstance()->SetPlayFlag(FALSE);
		 		WaitForSingleObject(PlayerManger::GetInstance()->ChildProcess(), INFINITE);
		 		PostQuitMessage(0);
		 		break;
		 	}
		break;
	case WM_DESTROY:
		PlayerManger::GetInstance()->SetPlayFlag(FALSE);
		WaitForSingleObject(PlayerManger::GetInstance()->ChildProcess(), INFINITE);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

也不说不好,就是感觉不和自己之前有游戏开发里面那样的风格(期间有各种google、baidu了如何做出来VK_LEFT系统按键之外的按键监听),最终修改的win32框架如下:

// test.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "test.h"

#define MAX_STRING  100
#define VK_A		0x41
#define VK_Z		0x5A

// 全局变量:
HWND	  g_hWnd;
HINSTANCE hInst;					// 当前实例
TCHAR	  szTitle[MAX_STRING]		= L"test";	// 标题栏文本
TCHAR	  szWindowClass[MAX_STRING] = L"test";		// 主窗口类名

// 此代码模块中包含的函数的前向声明:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
void				KeyDown(HWND hWnd);

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                       _In_opt_ HINSTANCE hPrevInstance,
                       _In_ LPTSTR    lpCmdLine,
                       _In_ int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: 在此放置代码。
	MSG msg = {0};
	MyRegisterClass(hInstance);

	// 执行应用程序初始化:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	// 主消息循环:
	while (msg.message != WM_QUIT)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			KeyDown(g_hWnd);
		}
	}

	return (int) msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TEST));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= 0;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // 将实例句柄存储在全局变量中

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   g_hWnd = hWnd;

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

/*键盘消息处理*/
void KeyDown(HWND hWnd)
{
	if(GetAsyncKeyState(VK_A)<0 )		//判断上方向键是否按下
	{
		MessageBox(0, L"A", L"GetAsyncKeyState", MB_OK);
	}
	else if(GetAsyncKeyState(VK_Z)<0 )		//判断上方向键是否按下
	{
		MessageBox(0, L"Z", L"GetAsyncKeyState", MB_OK);
	}
	else if(GetAsyncKeyState(VK_ESCAPE)<0)
	{
		SendMessage(hWnd, WM_CLOSE, 0, 0);
	}
}


你可能感兴趣的:(原谅我今天才学会使用win32的按键消息处理!)