How to undo keyboard capture using C # to develop key changer software

I am trying to develop software that will enter a keyboard, consume input and return some other character in the exchange of input. For example, if I type: "abcd" and define the exchange rule as any Russian alphabet, then I would expect the output to be: "sky".

The code I use is as follows:

namespace hook_form
{
    public partial class Form1 : Form
    {
        //Keyboard API constants

        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;
        private const int WM_KEYUP = 0x0101;
        private const int WM_SYSKEYUP = 0x0105;
        private const int WM_SYSKEYDOWN = 0x0104;
        private HookHandlerDelegate proc;
        private IntPtr hookID = IntPtr.Zero;
        private delegate IntPtr HookHandlerDelegate(int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam);

        private struct KBDLLHOOKSTRUCT
        {
            public int vkCode;
            int scanCode;
            public int flags;
            int time;
            int dwExtraInfo;
        }

        private IntPtr HookCallback(int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
        {
            bool AllowKey = false;

            switch (lParam.vkCode)
            {
                case (65|97|66|98): // key codes for "a/b/c/d" etc. goes here
                    //Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key
                    AllowKey = true;
                    SendKeys.Send("\u0997"); // code for russian letters here....
                    break;
            }

            MessageBox.Show(lParam.vkCode.ToString());

            if (AllowKey == false)
                return (System.IntPtr)1;

            return CallNextHookEx(hookID, nCode, wParam, ref lParam);
        }
        #region DllImports

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, 
        HookHandlerDelegate 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, ref KBDLLHOOKSTRUCT lParam);

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

        private void buttonStart_Click(object sender, EventArgs e)
        {
            proc = new HookHandlerDelegate(HookCallback);
            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                hookID = SetWindowsHookEx(WH_KEYBOARD_LL, proc,
                GetModuleHandle(curModule.ModuleName), 0);
            }
        }

        private void btnEnd_Click(object sender, EventArgs e)
        {
            UnhookWindowsHookEx(hookID);
        }
    }
}

I get the output as: "asbksid", I mean that the letters entered (English) are not used by the code (I mean that they should not appear on the output), although I returned (System.IntPtr)1.

I think this is a disengagement problem. Can someone help me solve this problem?

, - - , ?

+3
2

CallNextHookEx , .

: , , , WM_KEYDOWN WM_KEYUP. , . , . , .

+1

backspace , ?

AllowKey = true;
SendKeys.Send("{BS}");
SendKeys.Send("\u0997"); // code for russian letters here....
break;
0

Source: https://habr.com/ru/post/1777878/


All Articles