Win32中如何判断多个键同时按下

可以使用下面的两个Win32函数

 

 

 

 

 

GetKeyState

 

The GetKeyState function retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off—alternating each time the key is pressed).

SHORT GetKeyState(
  int nVirtKey   // virtual-key code
); nVirtKey   // virtual-key code
);

Parameters

nVirtKey

[in] Specifies a virtual key. If the desired virtual key is a letter or digit (A through Z, a through z, or 0 through 9),nVirtKey must be set to the ASCII value of that character. For other keys, it must be a virtual-key code.

If a non-English keyboard layout is used, virtual keys with values in the range ASCII A through Z and 0 through 9 are used to specify most of the character keys. For example, for the German keyboard layout, the virtual key of value ASCII O (0x4F) refers to the "o" key, whereas VK_OEM_1 refers to the "o with umlaut" key.

Return Values

The return value specifies the status of the specified virtual key, as follows:

  • If the high-order bit is 1, the key is down; otherwise, it is up.
  • If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled. 

 

//-------------------------或者-----------------------------

 

 

GetAsyncKeyState

 

The GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call toGetAsyncKeyState.

SHORT GetAsyncKeyState(
  int vKey   // virtual-key code
); vKey   // virtual-key code
);

Parameters

vKey

[in] Specifies one of 256 possible virtual-key codes. For more information, seeVirtual-Key Codes.

Windows NT/2000: You can use left- and right-distinguishing constants to specify certain keys. See the Remarks section for further information.

Return Values

If the function succeeds, the return value specifies whether the key was pressed since the last call toGetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call toGetAsyncKeyState. The return value is zero if a window in another thread or process currently has the keyboard focus.

 

//-------------------------------事件函数------------------------------------

 

 

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	RECT rec;
	SHORT res;
	rec.left = 100;
	rec.top = 100;
	rec.right = 700;
	rec.bottom = 200;
	hdc = GetDC(hWnd);
	// TODO:  在此添加任意绘图代码...
	//str += L"_";	
	res = GetAsyncKeyState((int)'A');
	if (res & 0x8000){
		str += L"A";
		::DrawText(hdc, str.c_str(), str.size(), &rec, NULL);
	}
	res = GetAsyncKeyState(0x42);
	if (res & 0x8000){
		str += L"b";
		::DrawText(hdc, str.c_str(), str.size(), &rec, NULL);
	}
	res = GetAsyncKeyState(0x43);
	if (res & 0x8000){
		str += L"c";
		::DrawText(hdc, str.c_str(), str.size(), &rec, NULL);
	}
	switch (message)
	{
	case WM_KEYDOWN:


		break;
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// 分析菜单选择: 
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break; 
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps); EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	
	return 0;
}

 

 

//多多指教--
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值