none
Issue with Keyboard hook for scanning for hypen(“-”) and equal sign(“=”) RRS feed

  • Question

  • I use windows C++ Keyboard hook to use USB scanner. Problem happens during scanning if QR code/barcode contains “=” and “-“. Key board hook gets wrong hexa data( i.e. ?(ffffffbb)) for these special characters. All other character gets scan correctly It would be great if you can suggest some possible solution for this problem. Thanks for taking a looking at this and please let me know if any additional information is required.

    Sample key board hook code

    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

    Friday, February 3, 2017 2:47 AM

All replies

  • Hi,

    Our forum is used for discussing issues related to Office Object model, I think your issue is out of scope.

    I suggest you post on C++ forum:

    https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=vcgeneral

    Thanks for your understanding.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 6, 2017 7:20 AM
    Moderator