keyboard - semi-unmanaged code with c# -
public delegate void keyboardhookcapturehandler(keyboardhookeventargs keyboardevents); public class keyboardhookeventargs : eventargs { private keys _pressedkey; private int _pressedkeycode; public keys pressedkey { { return _pressedkey; } } public int pressedkeycode { { return _pressedkeycode; } } public keyboardhookeventargs(int vkcode) { _pressedkey = (keys)vkcode; _pressedkeycode = vkcode; } } public class keyboardhook { private delegate intptr lowlevelkeyboardproc(int ncode, intptr wparam, intptr lparam); public event keyboardhookcapturehandler keyintercepted; private const int wh_keyboard_ll = 13; private const int wm_keydown = 0x0100; private lowlevelkeyboardproc _proc; private intptr _hookid = intptr.zero; public keyboardhook() { _proc = hookcallback; _hookid = sethook(_proc); } public bool unhookkey() { return unhookwindowshookex(_hookid); } private intptr sethook(lowlevelkeyboardproc proc) { using (process curprocess = process.getcurrentprocess()) using (processmodule curmodule = curprocess.mainmodule) { return setwindowshookex(wh_keyboard_ll, proc, getmodulehandle(curmodule.modulename), 0); } } private intptr hookcallback( int ncode, intptr wparam, intptr lparam) { if (ncode >= 0 && wparam == (intptr)wm_keydown) { int vkcode = marshal.readint32(lparam); keyboardhookeventargs keyhookargs = new keyboardhookeventargs(vkcode); keyintercepted(keyhookargs); } return callnexthookex(_hookid, ncode, wparam, lparam); } [dllimport("user32.dll", charset = charset.auto, setlasterror = true)] private static extern intptr setwindowshookex(int idhook, lowlevelkeyboardproc 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, intptr lparam); [dllimport("kernel32.dll", charset = charset.auto, setlasterror = true)] private static extern intptr getmodulehandle(string lpmodulename); }
so have no idea code means though core of program. hooks keyboard press event , sends program. can take there precious time , explain few things me. understand args class can skip that. interested in delegate is, intptr , 2 methods , line line.
thanks if has time
a delegate type specifies signature of function or method: it's way of capturing function or method object, can call method later. delegate instance therefore reference function or method.
an intptr operating system native pointer -- opaque reference piece of unmanaged memory.
the sethook method installing hook procedure windows, hook procedure called every keyboard event in system. hook procedure? proc, instance of lowlevelkeyboardproc
delegate type. in case, proc being set refer hookcallback
function. sethook
ends doing telling windows call hookcallback every time keyboard event happens.
hookcallback unpacking native operating system information associated keyboard event, , raising keyintercepted event unpacked data. it's passing control next hook in chain, in case else wanting hook keyboard events.
so final result of every time keyboard event happens, class raises keyintercepted event. users of class can provide keyintercepted event handlers useful things, example sending bank password crime syndicate of choice... *grin*
Comments
Post a Comment