Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, i have scanner 2d honeywell xenon 1900, it work like hid-keyboard.
in my program i use hooker for keyboard, which intercept press keys on keybord
and when i press on keyboard in english layout - hooker understand virtual_code and scan_code of keys
but when qr code consist english character, hooker ignored their because functions
ToAsciiEx or ToUnicodeEx can't translate virtual-code to character

for all english chracter my hooker show:
virtual code - 164
scan code - 56
in 'atol driver for scanners' in test read it show so
in qr code "abc абв" programm show "097098099 #32 абв"

but when cursor in notepad all characters correct

i use standart function:
C#
  [StructLayout(LayoutKind.Sequential)]
        private class KeyboardHookStruct
        {
            /// <summary>
            /// Specifies a virtual-key code. The code must be a value in the range 1 to 254. 
            /// </summary>
            public uint vkCode;
            /// <summary>
            /// Specifies a hardware scan code for the key. 
            /// </summary>
            public uint scanCode;
            /// <summary>
            /// Specifies the extended-key flag, event-injected flag, context code, and transition-state flag.
            /// </summary>
            public int flags;
            /// <summary>
            /// Specifies the time stamp for this message.
            /// </summary>
            public int time;
            /// <summary>
            /// Specifies extra information associated with the message. 
            /// </summary>
            public int dwExtraInfo;
        }


... ... ... ...


   private const int WH_KEYBOARD_LL = 13;


... ... ... ...


private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            //indicates if any of underlaing events set e.Handled flag
            bool handled = false;

            //it was ok and someone listens to events
            if ((nCode >= 0) && (KeyDown != null || KeyUp != null || KeyPress != null))
            {
                KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

                //raise KeyDown
                if (KeyDown != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
                {
                    Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
                    KeyEventArgs e = new KeyEventArgs(keyData);
                    KeyDown(this, e);
                    handled = handled || e.Handled;


                }

                if (KeyPress != null && wParam == WM_KEYUP)
                {
                    bool isDownShift = ((GetKeyState(VK_SHIFT) & 0x80) == 0x80 ? true : false);
                    bool isDownCapslock = (GetKeyState(VK_CAPITAL) != 0 ? true : false);

                    byte[] keyState = new byte[256];
                    GetKeyboardState(keyState);
                    byte[] inBuffer = new byte[2];

                    int localeId = GetKeyboardLayoutId();

                    if (ToUnicodeEx(MyKeyboardHookStruct.vkCode,
                          MyKeyboardHookStruct.scanCode,
                          keyState,
                          inBuffer,
                          2,
                          0
                          , localeId
                          ) >= 1)
                    {
                        UnicodeEncoding uEn = new UnicodeEncoding();
                        string outKey = uEn.GetString(inBuffer);

                        foreach (char key in outKey)
                        {

                            KeyPressEventArgs e = new KeyPressEventArgs(key);
                            KeyPress(this, e);
                            handled = handled || e.Handled;

                        }

                    }
                }

                // raise KeyUp
                if (KeyUp != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
                {
                    Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
                    KeyEventArgs e = new KeyEventArgs(keyData);
                    KeyUp(this, e);
                    handled = handled || e.Handled;

                }

            }

            //if event handled in application do not handoff to other listeners
             if (handled)
                return 1;
            else
                return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
        }


may be somebody write for scanners, what's mistake?


sorry for my english...
Posted
Comments
Bernhard Hiller 6-Aug-13 4:37am    
Are you sure it is for the "English" characters when you get the values of 164/56, not the Cyrillic? Can you set the keyboard at the scanner?
in qr code "abc абв" programm show "097098099 #32 абв" => the "English" characters are shown with their decimal ASCII code; strangely the space gets changed to ' #32 ', and 32 is the decimal ASCII code for space.
wladimirbm 6-Aug-13 6:08am    
below =)
wladimirbm 6-Aug-13 6:01am    
i'm sure, when i stay breakpoint on line "if (ToUnicodeEx(MyKeyboardHookStruct.vkCode", i can see what values have MyKeyboardHookStruct.vkCode() and MyKeyboardHookStruct.scanCode
you right, it's ascii code, i think so too, suches ascii code show test-driver 'atol', but i don't know what method it gets
but windows hook can't give me ascii code
Bernhard Hiller 6-Aug-13 6:27am    
Did the drivers for the scanner also install a keyboard layout for the scanner? If so, what happens when you use that layout instaed of a standard layout?
wladimirbm 6-Aug-13 7:07am    
no, driver just can read from device and show it.
when scanner have default setting, it don't send (ignore) cyrillic characters.
when set settings for cyrillic vif spec qr-code, it can read cyrillic, but english character send like ascii, and hooker don't understend virtual and scan codes.
may be i should use WH_GETMESSAGE ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900