Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

I have a card reader for read a user card but reading appears that:

For user 7196: %00007196_ç00007196_ - this is a new card

For user 910202: ç00910202_ - this is a old card


Another card reader appears:

For user 7196: 00007196 - this is a new card

For user 910202: 00910202 - this is a old card


The problem is when i pass the card the system sends all codes like a copy-paste but i need control this paste. Eliminate chars, etc...

This code receives all ps/2 codes:

C#
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class InterceptKeys
{
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool UnhookWindowsHookEx(IntPtr hhk);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string lpModuleName);

    public static int _count = 0;
    private const int WH_KEYBOARD_LL = 13;
    private const int WM_KEYDOWN = 0x0100;
    private static LowLevelKeyboardProc _proc = HookCallback;
    private static IntPtr _hookID = IntPtr.Zero;

    public static void Main()
    {
        _hookID = SetHook(_proc);
        Application.Run();
        UnhookWindowsHookEx(_hookID);
    }

    private static IntPtr SetHook(LowLevelKeyboardProc proc)
    {
        using (Process curProcess = Process.GetCurrentProcess())

        using (ProcessModule curModule = curProcess.MainModule)
        {
            return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
        }
    }

    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
        {

            int vkCode = Marshal.ReadInt32(lParam);

            if ((Keys)vkCode == Keys.Return)
            {
                Console.WriteLine("Cartão novo lido com sucesso.");
            }

            Console.WriteLine((Keys)vkCode);
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }
}


I need increase this code, any help?
Posted
Updated 14-Jun-15 6:56am
v3
Comments
Dave Kreskowiak 14-Jun-15 20:17pm    
Why are you using a global keyboard hook to do this? It's a very bad way of doing this because it is not friendly to other applications running on the system.

Have you looked at the KeyPreview property and associated events of the Form?
Jean-Pierre Carvalho (JPCarvalho) 16-Jun-15 13:01pm    
But i need use for another apps.
Dave Kreskowiak 16-Jun-15 13:57pm    
OK, this is odd. So you have a piece of hardware, and a SDK for it, that scans ID cards and either pastes or stuffs the keyboard with data from the card? So which is it? Does it copy data to the clipboard and stuff a Ctrl-V into the keyboard stream or does it stuff the keys for the data on the card?
Sinisa Hajnal 15-Jun-15 4:06am    
Create a list of invalid chars and simply ignore them (replace them) in paste.
Jean-Pierre Carvalho (JPCarvalho) 16-Jun-15 13:02pm    
Thanks, but it's not simple.

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