我可以改变一个用户的键盘输入? [英] Can I change a user's keyboard input?

查看:129
本文介绍了我可以改变一个用户的键盘输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个键盘钩子的代码,我想稍微修改一下我的目的:的 http://blogs.msdn.com/toub/archive/2006/05/03/589423.aspx

I found this keyboard hook code, which I'm trying to slightly modify for my purposes: http://blogs.msdn.com/toub/archive/2006/05/03/589423.aspx

作为概述,我想让用户按一个键,说'E',并有键盘返回不同的性格,'Z',到任何应用程序处于焦点。

As an overview, I want to have the user press a key, say 'E', and have the keyboard return a different character, 'Z', to whatever app is in focus.

有关方法我改变现在看起来像:

The relevant method I changed now looks like:

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
        {
            //The truely typed character:
            int vkCode = Marshal.ReadInt32(lParam);
            Console.WriteLine((Keys)vkCode);

            KBDLLHOOKSTRUCT replacementKey = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
            replacementKey.vkCode = 90; // char 'Z'
            Marshal.StructureToPtr(replacementKey, lParam, false);

            //Now changed to my set character
            vkCode = Marshal.ReadInt32(lParam);
            Console.WriteLine((Keys)vkCode);
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }



控制台正确输出该为:

The console correctly outputs this as:

E
Z
T
Z
G
Z
etc.

不过,焦点还是应用型'E'而不是'Z'。为什么?我改变了上瘾键盘输入遏制,而不是'E'Z',并在控制台线显示,这是正确的改变!

HOWEVER, the in focus app still types 'E' instead of 'Z'. Why? I changed the hooked keyboard input to contain 'Z' instead of 'E', and the console lines show that it was changed correctly!

据我了解,在调用返回CallNextHookEx方法(_hookID,nCode,wParam中,lParam的); 是发送现在打印此命令来打开的应用程序。是不是它是如何工作的?有没有东西是阻止我打字我想要的人物?我知道像AutoHotkey的应用需要一个输入键,检查,并返回一个不同的角色。我该怎么做了同样在这里?

As I understand it, calling the return CallNextHookEx(_hookID, nCode, wParam, lParam); is what sends the "print this now" command to the open app. Is that not how it works? Is there something that's preventing me from typing the character I want? I know apps like AutoHotkey take an input key, check it, and return a different character. How do I do the same here?

谢谢!

推荐答案

我这样做过,但有一点不同。结果
而不是试图改变发送到 CallNextHookEx方法的参数,我'吞'的按键(你可以从钩子程序返回非零值,以防止所谓的后续步骤)做到这一点。

I've done this before but a little different.
Instead of trying to change the parameters sent to CallNextHookEx, I 'swallowed' the key press (you can do this by returning a nonzero value from the hook procedure to prevent subsequent procedures from being called).

然后我用的 SendInput 送,我想注入新的密钥。

Then I used SendInput to send the new key that I wanted to 'inject'.

所以基本上它的工作原理是这样的:

So basically it works like this:


  • 钩子程序标识目标的关键按

  • 召唤SendInput,用新密钥

  • 从钩子程序返回1忽略原始密钥

小心循环重定向,即一重定向到重定向到Ab,它可以轻松地炸毁;)

Be careful of cyclic redirects, i.e. 'a' redirected to 'b' redirected to 'a', it can easily blow up ;)

这篇关于我可以改变一个用户的键盘输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆