c# - Can I change a user's keyboard input? -


i found keyboard hook code, i'm trying modify purposes: http://blogs.msdn.com/toub/archive/2006/05/03/589423.aspx

as overview, want have user press key, 'e', , have keyboard return different character, 'z', whatever app in focus.

the relevant method changed 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 set character             vkcode = marshal.readint32(lparam);             console.writeline((keys)vkcode);         }         return callnexthookex(_hookid, ncode, wparam, lparam);     } 

the console correctly outputs as:

e z t z g z etc. 

however, in focus app still types 'e' instead of 'z'. why? changed hooked keyboard input contain 'z' instead of 'e', , console lines show changed correctly!

as understand it, calling return callnexthookex(_hookid, ncode, wparam, lparam); sends "print now" command open app. not how works? there that's preventing me typing character want? know apps autohotkey take input key, check it, , return different character. how do same here?

thanks!

i've done before little different.
instead of trying change parameters sent callnexthookex, 'swallowed' key press (you can returning nonzero value hook procedure prevent subsequent procedures being called).

then used sendinput send new key wanted 'inject'.

so works this:

  • hook procedure identifies target key pressed
  • call sendinput, new key
  • return 1 hook procedure ignore original key

be careful of cyclic redirects, i.e. 'a' redirected 'b' redirected 'a', can blow ;)


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -