怎么判断按键是系统的左右哪一个键

------解决方案--------------------------------------------------------
可以从LPARAM参数中提取出虚拟键值(virtual key),代码片段如下:
C/C++ code
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);
    if (wParam == WM_KEYDOWN) 
   {
      char* buf;
      char tl[256];
      buf = tl;
      buf = itoa(p->vkCode,buf,10);//这句是关键
      int tmp;
      tmp = atoi(buf);
      switch (tmp) 
      {
     //......
          case VK_LMENU:      buf =  "<Left MENU>";       break;
    //......
       }
    }

------解决方案--------------------------------------------------------
可以从LPARAM参数中提取出虚拟键值(virtual key),代码片段如下:

   
   
   
   
C/C++ code
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);
    if (wParam == WM_KEYDOWN) 
   {
      char* buf;
      char tl[256];
      buf = tl;
      buf = itoa(p->vkCode,buf,10);//这句是关键
      int tmp;
      tmp = atoi(buf);
      switch (tmp) 
      {
     //......
          case VK_LMENU:      buf =  "<Left MENU>";       break;
    //......
       }
    }

------解决方案--------------------------------------------------------
我在PreTranslateMessage中截获消息,并处理Key值,可以区分开始左边还是右边的按下。
例如左shift还是右shift,但是令我感到奇怪的一点是alt键不是在WM_KEYDOWN中响应,而是在WM_SYSKEYDOWN中响应,而且每按两次才会相应一次,但是区分左右还是没问题的,直接上代码;

     
     
     
     
C/C++ code
在PreTranslateMessage中处理如下代码

BYTE vKeyArrow[256];

switch(pMsg->message)
{    
case WM_KEYDOWN:
    memset(vKeyArrow,0,256);
    GetKeyboardState(vKeyArrow);
    if (vKeyArrow[0xa0] == 0x80 || vKeyArrow[0xa0] == 0x81)
        TRACE(L"Left shift is down\n");//左Shift键按下
    if (vKeyArrow[0xa1] == 0x80 || vKeyArrow[0xa1] == 0x81)
        TRACE(L"Right shift is down\n");//右Shift键按下
    break;
case WM_SYSKEYDOWN:
    memset(vKeyArrow,0,256);
    GetKeyboardState(vKeyArrow);
    if (vKeyArrow[0xa4] == 0x80 || vKeyArrow[0xa4] == 0x81)
        TRACE(L"Left alt is down\n");//左alt键按下
    if (vKeyArrow[0xa5] == 0x80 || vKeyArrow[0xa5] == 0x81)
        TRACE(L"Right alt is down\n");//右alt键按下
    break;
}

------解决方案--------------------------------------------------------


       
       
       
       
C/C++ code
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if(HC_ACTION == nCode)
    {
        KBDLLHOOKSTRUCT* pSturct = (KBDLLHOOKSTRUCT*)lParam;
        
        switch(wParam)
        {
        case WM_SYSKEYDOWN:
            switch(pSturct->vkCode)
            {
            case VK_LMENU:
                AfxMessageBox(_T("Left ALT"));
                break;

            case VK_RMENU:
                AfxMessageBox(_T("Right ALT"));
                break;
            }
            break;

        default:
            break;
        }
    }
    return CallNextHookEx(hOldHook, nCode, wParam, lParam);
}
				
转自:http://www.myexception.cn/vc-mfc/191000.html

你可能感兴趣的:(怎么判断按键是系统的左右哪一个键)