在win32程序设计中,如何判断一个键的当前状态,可以使用GetkeyState()函数,这个函数是用来获取当前键盘上键的状态,也就是说当前你那些键是DOWN的,那些键是UP的。不能不能把GetKeyState 用於尚未按下的键,您也不能为尚未按下的滑鼠按键呼叫
GetKeyState。请不要这样做:
while (GetKeyState (VK_LBUTTON) >= 0) ; // WRONG !!!
只有在您呼叫GetKeyState 期间处理讯息时,而左键已经按下,才会报告键已经按下的讯息。
例如,我想获得当前capslock键的状态,那么我可以这样做:、
if(GetkeyState(VK_CAPITAL)&0X01)// 这里主要是取得capslock键的最低位,因这个位中保存了硬件信息。MSDN上是这么说的
//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.
{
MessageBox(hWnd,_T("caps down"),_T("cap"),MB_OK);
}
MessageBox(hWnd,_T("caps up"),_T("cap"),MB_OK);
这里的最低位和键盘上的灯的亮灭是一致的!