开发者

How to filter keyboard inputs using C# or VB.Net, Everywhere when application is running?

开发者 https://www.devze.com 2023-02-06 15:32 出处:网络
How to filter inputs from keyboard in all windows applications while our written code is running and lives in notification area

How to filter inputs from keyboard in all windows applications while our written code is running and lives in notification area

it's like: user opens our application, now when typing in for example MS Wo开发者_JAVA技巧rd, when he types "s" it is said in our code to filter "s" and change it to "M" so if he press "s" on keyboard, "M" is typed in MS Word document.

is it possible at all?


You should use low-level hook:

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

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

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

And do what you want in your own hook implementation.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号