c# - make GlobalKeyboardHook class read string comes from 2D barcode scanner -
i using globalkeyboardhook class make application listen keyboard keydown event , works fine when barcode scanner scan image, return string "dos-720" encodeing "ÃÍãÏ", globalkeyboardhook class return thing "¤aii¤",
how make globalkeyboardhook class return original string??
the globalkeyboardhook class
public class globalkeyboardhook { [dllimport("user32.dll")] private static extern int callnexthookex(intptr hhk, int code, int wparam, ref keyboardhookstruct lparam); [dllimport("user32.dll")] private static extern intptr setwindowshookex(int idhook, llkeyboardhook callback, intptr hinstance, uint theardid); [dllimport("user32.dll")] private static extern bool unhookwindowshookex(intptr hinstance); [dllimport("kernel32.dll")] private static extern intptr loadlibrary(string lpfilename); public delegate int llkeyboardhook(int code, int wparam, ref keyboardhookstruct lparam); public struct keyboardhookstruct { public int vkcode; public int scancode; public int flags; public int time; public int dwextrainfo; } private const int wh_keyboard_ll = 13; private const int wm_keydown = 0x0100; private const int wm_keyup = 0x0101; private const int wm_syskeydown = 0x0104; private const int wm_syskeyup = 0x0105; private llkeyboardhook llkh; public list<keys> hookedkeys = new list<keys>(); private intptr hook = intptr.zero; public event keyeventhandler keydown; public event keyeventhandler keyup; public globalkeyboardhook() { llkh = new llkeyboardhook(hookproc); hook(); } ~globalkeyboardhook() { unhook(); } public void hook() { intptr hinstance = loadlibrary("user32"); hook = setwindowshookex(wh_keyboard_ll, llkh, hinstance, 0); } public void unhook() { unhookwindowshookex(hook); } public int hookproc(int code, int wparam, ref keyboardhookstruct lparam) { if (code >= 0) { keys key = (keys) lparam.vkcode; if (hookedkeys.contains(key)) { keyeventargs karg = new keyeventargs(key); if ((wparam == wm_keydown || wparam == wm_syskeydown) && (keydown != null)) keydown(this, karg); else if ((wparam == wm_keyup || wparam == wm_syskeyup) && (keyup != null)) keyup(this, karg); if (karg.handled) return 1; } } return callnexthookex(hook, code, wparam, ref lparam); } }
and use of in form
globalkeyboardhook ghook; public form1() { initializecomponent(); //new thread(samplefunction).start(); } private void form1_load(object sender, eventargs e) { ghook = new globalkeyboardhook(); // create new globalkeyboardhook // declare keydown event ghook.keydown += new keyeventhandler(ghook_keydown); // add keys want hook hookedkeys list foreach (keys key in enum.getvalues(typeof(keys))) ghook.hookedkeys.add(key); } // handle keydown event public void ghook_keydown(object sender, keyeventargs e) { textbox1.text += ((char)e.keyvalue).tostring(); } private void button1_click(object sender, eventargs e) { ghook.hook(); } private void button2_click(object sender, eventargs e) { ghook.unhook(); } private void form1_formclosing(object sender, formclosingeventargs e) { ghook.unhook(); }
Comments
Post a Comment