键盘挂钩问题,用于扫描hypen(";-";)和等号(";=";) < >我使用Windows C++键盘钩子使用USB扫描仪。如果二维码/条形码包含“=”和“-”,扫描过程中会出现问题。键盘挂钩获取错误的hexa数据(即?(FFFFFF BB))对于这些特殊字符。所有其他字符都得到正确扫描。如果您能为这个问题提出一些可能的解决方案,那就太好了。感谢您查看这个问题,如果需要任何其他信息,请告诉我

键盘挂钩问题,用于扫描hypen(";-";)和等号(";=";) < >我使用Windows C++键盘钩子使用USB扫描仪。如果二维码/条形码包含“=”和“-”,扫描过程中会出现问题。键盘挂钩获取错误的hexa数据(即?(FFFFFF BB))对于这些特殊字符。所有其他字符都得到正确扫描。如果您能为这个问题提出一些可能的解决方案,那就太好了。感谢您查看这个问题,如果需要任何其他信息,请告诉我,c++,visual-c++,C++,Visual C++,键盘钩子代码示例 clspec(dllexport)LRESULT __stdcall HookCallback(int iCode, WPARAM wParam, LPARAM lParam) { CString str, strBuffer; str.Format( _T("lParam =%c(%0x),wParam = %c(%0x)"), (char)lParam, (char)lParam, (char)wParam, (char)

键盘钩子代码示例

clspec(dllexport)LRESULT __stdcall HookCallback(int iCode, WPARAM wParam, LPARAM lParam)
    {   
        CString str, strBuffer;

        str.Format( _T("lParam =%c(%0x),wParam = %c(%0x)"), (char)lParam,  (char)lParam, (char)wParam,  (char)wParam  );
        CT2CA pszConvertedAnsiString (str);

        string str1(pszConvertedAnsiString);// = temp.GetBuffer(temp.GetLength());
        ofstream myfile;
        myfile.open ("ScanData.txt",ios::out | ios::app | ios::binary);
        myfile<<str1<<endl;         

      //  Translation table from base characters to shifted characters.
      char    From[] = "`1234567890-=\\[];',./";
      char    To[]   = "~!@#$%^&*()_+|{}:\"<>?";

      // if not an action event or MSR is not active, go to the next hook
      if (iCode < 0 || iCode != HC_ACTION )
          return CallNextHookEx(_hook, iCode, wParam, lParam);

      // Check if shift key is pressed.
      if (wParam == 0x10 && (lParam & 0xc0000000) == 0)
      {
          myfile<<"Inside  TRUE"<<endl;
          m_bShift = TRUE;
      }
      if (wParam == 0x10 && (lParam & 0xc0000000) != 0)
      {
          myfile<<"Inside  FALSE"<<endl;
          m_bShift = FALSE;
      }      

      // clear existing buffer and post message
      if ( wParam == 0x0d )
      {
          myfile<<"End of scanning"<<endl;
          CharStringToCString( m_Buffer, strBuffer, strlen( m_Buffer ) );              
          myfile<<m_Buffer<<endl;

          m_Buffer[0] = 0;
          m_nBufferIndex = 0;
          return 1;
      }
      else
      {          
          if (m_bShift) //converting the character if shift pressed
          {
              myfile<<"Inside shift"<<endl;
              int i = 0;
              while (From[i] != 0 && From[i] != (char)wParam) i++;
              if (From[i] == (char)wParam)        // Translate to shifted
              {
                  myfile<<"Inside shift2"<<endl;
                  wParam = (int)To[i];            //   character
              }
          }       


 if ((lParam & 0xc0000000) == 0 && wParam != 0x10)
    {      
        myfile<<"Append to buffer"<<endl;
        m_Buffer[m_nBufferIndex++] = (char)wParam;       // Append character to buffer
        m_Buffer[m_nBufferIndex] = 0;
    }
    return 1;
  }

  // If not Scanner data, chain to next hook procedure
  return CallNextHookEx(_hook, iCode, wParam, lParam);
}


void SetHook()
{
        HOOKPROC hkprc;
        hhookDLL = LoadLibrary(TEXT("..\\Debug\\KeyboardDLL.dll")); 
        if(!hhookDLL)
            MessageBox(NULL, _T("Failed to get hhookDLL!"), _T("Error"), MB_ICONERROR);
        hkprc = (HOOKPROC)GetProcAddress(hhookDLL, "_HookCallback@12"); 
        if(!hkprc)
            MessageBox(NULL, _T("Failed to get hkprc!"), _T("Error"), MB_ICONERROR);
        else
            MessageBox(NULL, _T("Scan QR/Bar code and check 'ScanData.txt'!"), _T("INFO"), MB_ICONINFORMATION);
        _hook = SetWindowsHookEx( 
                            WH_KEYBOARD,
                            hkprc,
                            hhookDLL,
                            0); 
        if(!_hook)
            MessageBox(NULL, _T("Failed to get _hook!"), _T("Error"), MB_ICONERROR);
}

output:

    Inside else shift
    Inside else shift 2
    **lParam =(1),wParam = ?(ffffffbb) (******Wrong****** for =)**
    Inside else shift
    Append to buffer
    lParam =(1),wParam = ?(ffffffbb)
    Inside else shift
    lParam =(1),wParam = 
    (d)
    End of scanning
clspec(dllexport)LRESULT\uu stdcall HookCallback(int-iCode、WPARAM-WPARAM、LPARAM-LPARAM)
{   
CString-str,strBuffer;
str.Format(_T(“lParam=%c(%0x),wParam=%c(%0x)”),(char)lParam,(char)lParam,(char)wParam,(char)wParam);
CT2CA pszConvertedAnsiString(str);
字符串str1(pszConvertedAnsiString);/=temp.GetBuffer(temp.GetLength());
流文件;
myfile.open(“ScanData.txt”,ios::out | ios::app | ios::binary);

MyFile您没有得到字符,您得到的是虚拟键代码(有时,但并非总是与打印在键上的字符的ASCII代码匹配)。0xBB恰好是
VK\u OEM\u PLUS
(数字键盘上的加号键)。此答案适用于此问题。感谢您的帮助!