dotnet compact framework:
hi, based on a few posts i have read here, i created a dll that allows me to perform low level keyboardhooking from my C# code. this works great and i send to a MessageWindow, certain WM_KEYDOWN messages. What i would now like to do is if i detect certain keys (regardless of what process they eminated from), set focus to a control on a form, and cancel the keypress all together. Is this possible? What i have attempted are two things: 1. don't call CallNextHookEx. when i bypass that call, the same message is sent repeatedly to my MessageWindow (i.e. the last message sent from the dll's HookProc). why? 2. i marhall in the KBDLLHOOKSTRUCT structure in the C# code, change the vkcode and scancode to something like -9999, and marshall that back to a ptr (i.e. the memory block the C code is referencing). The marshalling works (i see the -9999 being processed by the dll), but the keydown is still processed and does something. Why i say this is that in by C# code, i can set focus to the control i want to when i recieve the right arrow key press, but as soon as the HookProc call is completed in the dll, the control looses focus (no longer has that dark border indicating it has the focus) anyone has any idea of how to solve this problem?
here's some of the code ******BEGIN C#****** public class KeyHandlerMW : MessageWindow { ... protected override void WndProc( ref Message msg ) { ... switch ( msg.WParam.ToInt32( ) ) { case ( int ) API.WM_KEYDOWN: API.KBDLLHOOKSTRUCT kbStruct = getKeyStruct( msg.LParam ); if ( kbStruct.vkCode == 39 ) { //********* some code here that sets focus to form control //********* //try to suppress keydown event by changing vkCode and //letting DLL know bypass CallNextHookEx kbStruct.vkCode = -9999; kbStruct.scanCode = -9999; System.Runtime.InteropServices. Marshal.StructureToPtr( kbStruct, msg.LParam, false ); } break; } } public API.KBDLLHOOKSTRUCT getKeyStruct( IntPtr structPtr ) { API.KBDLLHOOKSTRUCT kbStruct = ( API.KBDLLHOOKSTRUCT ) System.Runtime.InteropServices.Marshal. PtrToStructure( structPtr, typeof( API.KBDLLHOOKSTRUCT ) ); Logger.debug("VKCode: " + kbStruct.vkCode); Logger.debug("ScanCode: " + kbStruct.scanCode); Logger.debug("Flags: " + kbStruct.flags); Logger.debug("Time: " + kbStruct.time); Logger.debug("dwExtraInfo: " + kbStruct.dwExtraInfo); return kbStruct; } } ******END C#****** ******BEGIN DLL****** KEYHOOK_DLL_API LRESULT CALLBACK keyboardHookProc(int ncode,WPARAM wparam, LPARAM lparam) { if ( ncode == HC_ACTION ) { if( wparam == WM_KEYDOWN ) { SendMessage( _messageWindow, ncode, wparam, lparam); } } KBDLLHOOKSTRUCT *kbs = (KBDLLHOOKSTRUCT*) lparam; if ( kbs->vkCode == -9999 ) { //send dummy message back to message window to see //if StructToPtr worked SendMessage( _messageWindow, ncode, 9999, lparam); return NULL; } else return ( CallNextHookEx( _keyboardHook, ncode, wparam,lparam ) ); return 0; } ******END DLL******
I think that, if you just bypass calling the next hook and return the right value, the key should effectively be ignored. Paul T. [quoted text, click to view] "farseer" <farseer@optonline.net> wrote in message news:1128060810.083607.296300@z14g2000cwz.googlegroups.com... > here's some of the code > > ******BEGIN C#****** > public class KeyHandlerMW : MessageWindow > { > ... > > protected override void WndProc( ref Message msg ) > { > ... > > switch ( msg.WParam.ToInt32( ) ) > { > case ( int ) API.WM_KEYDOWN: > API.KBDLLHOOKSTRUCT kbStruct = getKeyStruct( msg.LParam ); > > if ( kbStruct.vkCode == 39 ) > { > //********* > some code here that sets focus to form control > //********* > > //try to suppress keydown event by changing vkCode and > //letting DLL know bypass CallNextHookEx > kbStruct.vkCode = -9999; > kbStruct.scanCode = -9999; > System.Runtime.InteropServices. > Marshal.StructureToPtr( > kbStruct, msg.LParam, false ); > } > > break; > > } > } > > > public API.KBDLLHOOKSTRUCT getKeyStruct( IntPtr structPtr ) > { > API.KBDLLHOOKSTRUCT kbStruct = ( API.KBDLLHOOKSTRUCT ) > System.Runtime.InteropServices.Marshal. > PtrToStructure( structPtr, typeof( API.KBDLLHOOKSTRUCT ) ); > > Logger.debug("VKCode: " + kbStruct.vkCode); > Logger.debug("ScanCode: " + kbStruct.scanCode); > Logger.debug("Flags: " + kbStruct.flags); > Logger.debug("Time: " + kbStruct.time); > Logger.debug("dwExtraInfo: " + kbStruct.dwExtraInfo); > > return kbStruct; > } > } > > ******END C#****** > > ******BEGIN DLL****** > > KEYHOOK_DLL_API LRESULT CALLBACK keyboardHookProc(int ncode,WPARAM > wparam, > LPARAM lparam) > { > if ( ncode == HC_ACTION ) > { > if( wparam == WM_KEYDOWN ) > { > SendMessage( _messageWindow, ncode, wparam, lparam); > } > } > > KBDLLHOOKSTRUCT *kbs = (KBDLLHOOKSTRUCT*) lparam; > > if ( kbs->vkCode == -9999 ) > { > //send dummy message back to message window to see > //if StructToPtr worked > SendMessage( _messageWindow, ncode, 9999, lparam); > return NULL; > } > else > return ( CallNextHookEx( _keyboardHook, ncode, wparam,lparam ) ); > > return 0; > > } > ******END DLL****** >
What would be the "right" value to return? perhaps that is what i am missing. right now, bypassing the CallNextHookEx and returning something like NULL does not do it for me. I know NULL is probably not what should be returned, but i'm not sure what should be the return value... This would be an "app-saver" if i can get this to work... thanks much
If you read the docs on LowLevelKeyboardProc function, linked from the documentation of SetWindowsHookEx() in MSDN, it tells you what to return for various situations, including the case where you don't want the key to be sent to the target window procedure. Paul T. [quoted text, click to view] "farseer" <farseer@optonline.net> wrote in message news:1128543576.986376.213420@g47g2000cwa.googlegroups.com... > What would be the "right" value to return? perhaps that is what i am > missing. right now, bypassing the CallNextHookEx and returning > something like NULL does not do it for me. I know NULL is probably not > what should be returned, but i'm not sure what should be the return > value... This would be an "app-saver" if i can get this to work... > > thanks much >
thanks...well, so you don't think i am a lazy bum, i did look, but may have not read it carefully...i thought that not callingnexthookex would simply not pass it on to other hookprocs (i.e. other applications that have installed WH_KEYBOARD_LL). i now see the last four words of the paragraph of interest..."..or the target window procedure". i am hoping that means what i think it does... i'll try tonight... thanks much
Good luck! Paul T. [quoted text, click to view] "farseer" <farseer@optonline.net> wrote in message news:1128549299.216856.56860@f14g2000cwb.googlegroups.com... > thanks...well, so you don't think i am a lazy bum, i did look, but may > have not read it carefully...i thought that not callingnexthookex would > simply not pass it on to other hookprocs (i.e. other applications that > have installed WH_KEYBOARD_LL). > i now see the last four words of the paragraph of interest..."..or the > target window procedure". i am hoping that means what i think it > does... > i'll try tonight... > > thanks much >
Don't see what you're looking for? Try a search.
|