0

i have an HID usb rfid reader that act like a keyboard, i don't want to put a textbox on my form (WPF) to recieve the text from it because i have other requirements . instead i am trying to capture the key pressed events and process them . for that i have tried three differents methods :

Windows Hook (c++) this is the most simple testcase example that illustrate the issue

#include <iostream>
#include <fstream>
#include <Windows.h>
#pragma comment(lib, "user32.lib")

HHOOK keyboardHook{ NULL };
DWORD lastkey = 0;

LRESULT CALLBACK MyLowLevelKeyBoardProc(const int nCode, const WPARAM wParam, const LPARAM lParam)
{

    KBDLLHOOKSTRUCT *kb = (KBDLLHOOKSTRUCT *)lParam;

    switch (wParam)
    {

    case WM_KEYUP:
        if (lastkey == 13)
           system("cls");
        std::cout << "KeyUp event : " << kb->vkCode   << std::endl;
        lastkey = kb->vkCode;
        break;
    }

    return CallNextHookEx(keyboardHook, nCode, wParam, lParam);
}


int main(int argc, char* argv[])
{
    keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, MyLowLevelKeyBoardProc, NULL, 0);


    if (keyboardHook == NULL) {
        std::cout << "Keyboard hook failed!" << std::endl;
    }


    while (GetMessage(NULL, NULL, 0, 0));
    return 0;
}

Raw input API

by handling the WM_INPUT message (too many code to show)

oblita interception library

the most interesting solution that i will use if i fix the problem

#include "stdafx.h"
#include "C:\Dev\WPF\Interception\library\interception.h"

#include "C:\Dev\WPF\Interception\Interception-1.0.0\samples\utils.h"
#include <string>
#include <iostream>

enum ScanCode
{
    SCANCODE_X = 0x2D,
    SCANCODE_Y = 0x15,
    SCANCODE_ESC = 0x01
};

int main()
{

    using namespace std;

    InterceptionContext context;
    InterceptionDevice device;
    InterceptionStroke stroke;

    wchar_t hardware_id[500];
    string buffer = "";
    //raise_process_priority();

    context = interception_create_context();


    interception_set_filter(context, interception_is_keyboard, INTERCEPTION_FILTER_KEY_UP | INTERCEPTION_FILTER_KEY_UP);



    while (interception_receive(context, device = interception_wait(context), &stroke, 1) > 0)
    {
        if (interception_is_keyboard(device))
        {
            InterceptionKeyStroke &keystroke = *(InterceptionKeyStroke *)&stroke;

            if (keystroke.code == SCANCODE_ESC) break;


        size_t length = interception_get_hardware_id(context, device, hardware_id, sizeof(hardware_id));


        if (wcsstr(hardware_id, L"04F3&PID_0009") == 0)
           interception_send(context, device, &stroke, 1); // Real Keyboard
        else
        {
            // RFID reader
            if (keystroke.code == 28)
            {
                std::cout << buffer << endl;
                  buffer = "";
            }
            else
                buffer = buffer + std::to_string(keystroke.code);


        }
        }
    }

    interception_destroy_context(context);

    return 0;
}

all the three methods gave me the same problem :

some keys are randomly lost during the read, instead of having the 10 characters i my have 8/9 only .

if i use the rfid reader on blocnote no character is lost so there is no problem with the reader .

so my question is : how to ensure that no message/key is lost by windows on the hooks , even if the read operation may take 10 seconds .

thanks and good day .

user2475096
  • 350
  • 3
  • 19
  • I can't see any problem with Windows hook method. – Barmak Shemirani Oct 22 '16 at 09:28
  • me too :) but like i said i have random lost character from the rfid reader, i am on win 8.1 btw – user2475096 Oct 22 '16 at 09:43
  • Why are you hooking keyboard input anyway, instead of just handling the keyboard messages? – IInspectable Oct 22 '16 at 11:19
  • i have to differentiate between the keys that come from the rfid reader and that come from the real keyboard, process the first ones and prevent them to propagate (using the interception library) – user2475096 Oct 22 '16 at 11:43
  • So then, use Raw Input. Since you didn't show that code, we cannot help you fix it. – IInspectable Oct 22 '16 at 18:02
  • even if i didn't post the raw input because actually it's a mini library from here http://www.codeproject.com/Articles/17123/Using-Raw-Input-from-C-to-handle-multiple-keyboard?msg=5313921#xx5313921xx, but the two other methods i have tried illustrate the issue . – user2475096 Oct 22 '16 at 18:34

0 Answers0