c++ - Low Level keyboard hook never catches WM_KEYDOWN -


i'm trying create macro application start running operations when key pressed (system wide shortcut). did created windows form application visual studio 2012. when form loaded keyboard hook installed:

hookhandle = setwindowshookex( wh_keyboard_ll, (hookproc)keyboardhookhandler, getmodulehandle(null), null); if( hookhandle == 0){     messagebox::show("error setting hook!"); } 

my hook callback function is:

public: static lresult callback keyboardhookhandler( int code, wparam wparam, lparam lparam ) {     if(code>=0 && wparam == wm_keydown){         messagebox::show("key down");     }     return callnexthookex( hookhandle, code, wparam, lparam);  } 

when compile application , run message box never shown. more know call function fired wparam contains value 45 (i did checked , none of wm constants should returned has value 45). after few key events application crashes.

what reason why code doesn't work should to?

update: did removed cast hookproc , changed delegated procedure:

private:      delegate lresult callback hookproc( int code, wparam wparam, lparam lparam );     hookproc^ keyboardhookprocedure; 

and hook setting to:

keyboardhookprocedure = gcnew hookproc(this, &myform::keyboardhookhandler); hookhandle = setwindowshookex( wh_keyboard_ll, keyboardhookprocedure, getmodulehandle(null), null); 

but have problem:

error c2664: 'setwindowshookexw' : cannot convert parameter 2 'windowsformtest::myform::hookproc ^' 'hookproc'

if you're trying catch unique key combination globally, might simpler use registerhotkey. defines system-wide hot key no need hooks or special. going override foreground process handling, won't ideal if you're trying register common key combo.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -