How to unhook keyboard-hook using c# to develop a keystroke changing software -


i trying develop software take keyboard input, consume input , return other character inexchange of typed input. example, if type: "abcd" , define exchange rule russian alphabet expect output as: "ский".

the code using 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 "a/b/c/d" etc. goes here                     //alt+tab, alt+esc, ctrl+esc, windows key                     allowkey = true;                     sendkeys.send("\u0997"); // code 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 output as: "aсbкcиdй", mean typed letters (english) not consumed code(i mean should not appear in output) though returned (system.intptr)1.

i think problem of unhooking. can me solve problem ?

if not possible, can refer me open source software uses keyboard hook?

try not calling callnexthookex events want swallow.

edit: 1 problem see key press generates 2 messages wm_keydown , wm_keyup. injecting new keyboard message both cases, duplicating it. example @ tim barrass' link show how better. not explain behavior seeing, though.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -