1

アプリケーションがキーボードのキーダウン イベントをリッスンするように GlobalKeyboardHook クラスを使用していますが、正常に動作しますが、バーコード スキャナーが画像をスキャンすると、「ÍãÏ」のような「DOS-720」エンコードの文字列が返されます。GlobalKeyboardHook クラスこの "¤aii¤" のようなものを返します。

GlobalKeyboardHook クラスが元の文字列を返すようにする方法は??

GlobalKeyboardHook クラス

  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);
    }

}

そしてそれをフォームで使用する

 GlobalKeyboardHook gHook;
    public Form1()
    {
        InitializeComponent();
        //new Thread(SampleFunction).Start();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        gHook = new GlobalKeyboardHook(); // Create a new GlobalKeyboardHook
        // Declare a KeyDown Event
        gHook.KeyDown += new KeyEventHandler(gHook_KeyDown);
        // Add the keys you want to hook to the HookedKeys list
        foreach (Keys key in Enum.GetValues(typeof(Keys)))
            gHook.HookedKeys.Add(key);
    }

    // Handle the 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();
    }
4

0 に答える 0