views:

1175

answers:

2

so, i guess i must do it in c++, anyone know about this problem? i already searching everywhere and i found some articles about keyboard hook on windows ce, windows mobile is windows ce, isn't it? another questions: which free compiler, ide for windows mobile i could use?

+1  A: 

SetWindowsHookEx is not supported on any WindowsCE (read: Mobile) version. Hooks in general are not supported, in fact.

However, if you're willing to use undocument/unsupported APIs you can pull SetWindowsHookEx out of coredll.dll, and call it as you would on proper Windows. You want WH_KEYBOARD_LL, which a little googling says is 20.

You'll actually need to pull out pointers to the following methods: SetWindowsHookEx, CallNextHookEx, and UnhookWindowsHookEx.

Your code will resemble (this is untested):

//myHook.dll
LRESULT myLowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
  //You'll need to pull a reference to CallNextHookEx out of coredll as well
  if(nCode < 0) return CallNextHookEx(nCode, wParam, lParam);

  KBDLLHOOKSTRUCT data = *((PKBDLLHOOKSTRUCT)lParam);

  //Do something with data

  return CallNextHookEx(nCode, wParam, lParam);
}

//Main Code, which ignores all the nasty function pointers you'd ACTUALLY have to use to do this
...
HHOOK hook = SetWindowsHookEx(WH_KEYBOARD_LL, pMyLowLevelKeyboardProc, hMyHookDll, 0);
...
//Some point in the future
UnhookWindowsHookEx(hook);

I would strongly suggest against this however. I doubt very much that this code will keep working for all future versions of Windows Mobile. Consider some other way to achieve whatever it is you're actually after.

I can't say I have any recommendations for free compilers or IDEs. Anything other than Visual Studio for C/C++ always causes me a lot of frustration. I think this is more a reflection of my habits than a commentary on any other tools.

Kevin Montrose
can i use this with c# and native dll call ?
StoneHeart
On the desktop it is possible to install WH_KEYBOARD_LL hooks using pure managed code. You'll have to experiment to see if the same holds true for Windows Mobile. My examples are in C as they're the "most likely" to work; as noted this is an unsupported operation.
Kevin Montrose
i'm doing a tool on windows mobile, it can run in background and capture key which user pressed (in global), so can i do it in c#?
StoneHeart
A: 

http://www.naresh.se/2009/09/08/getkeyboardstate-mousehooks-not-available-in-windows-mobile/

Follow the above URL. It has the required code to work on Windows Mobile and Windows CE and also has a good explanation as well as a forum to discuss further on...

Forgot to say that the code is in C# as required by some other users...