Solved

SetWindowsHookEx to hook keyboard messages

Posted on 2003-11-11
12
877 Views
Last Modified: 2013-11-20
How can I catch keystrokes on Pocket pc 2002 by calling the SetWindowsHookEx function?
0
Comment
Question by:DarioDeNicola
  • 5
  • 4
12 Comments
 
LVL 5

Expert Comment

by:jrocnuck
ID: 9726060
not sure on a Pocket PC, but on regular old windows this is an example:

http://www.shellfront.org/modules/source/mrjukes/LiteStroker/LiteStroker.c
0
 
LVL 5

Expert Comment

by:jrocnuck
ID: 9726089
0
 

Author Comment

by:DarioDeNicola
ID: 9739764
i tried to load Hook functions from coredll.dll, this is the code:

typedef long (__cdecl *hookprocedure)(int,unsigned int,long);
typedef HHOOK  (__cdecl *sethook)(int,hookprocedure,HINSTANCE,DWORD);
typedef LRESULT  (__cdecl *nexthook)(HHOOK,int,WPARAM,LPARAM);

HHOOK hook;
nexthook nexthookfunction;

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
                           LPWSTR lpCmdLine,int nCmdShow)
{
      HINSTANCE library;      
      sethook sethookfunction;
      
      if((library=LoadLibrary(L"coredll.dll"))==NULL)
            PostQuitMessage(1);

      if((sethookfunction=(sethook) GetProcAddress(library,L"SetWindowsHookEx"))==NULL)
            PostQuitMessage(1);

      if((nexthookfunction=(nexthook) GetProcAddress(library,L"CallNextHookEx"))==NULL)
            PostQuitMessage(1);

      if((hook=sethookfunction(20,KeyLogProc,hInstance,0))== NULL)
            PostQuitMessage(1);
      


but the call to GetProcAddress return a NULL pointer;
is there something wrong in the code?

thank u
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 5

Accepted Solution

by:
jrocnuck earned 100 total points
ID: 9740129
After doing some poking around, it seems that there are no hooks supported in Windows CE (according to what I've seen).


about the only thing you can do is get keys pressed in your own application:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceui/htm/_wcesdk_Receiving_Keyboard_Input.asp?frame=true

0
 

Author Comment

by:DarioDeNicola
ID: 9740409
i've just tried to use GetAsyncKeyState first but it doesn't work:

i've created a loop to test the keystrokes with GetAsyncKeyState but, for example, if i press a 'a', during the loop the call GetAsyncKeyState on the character 'a' returns non-zero for 2-3-4 times depending how many times the system calls the function while the 'a' is down, do u understand?
so...i'm in troubles and if i don't resolve the question i can't terminate my universitary studies!! :-(
if u have a solution you will save me!!

thanks  
0
 
LVL 5

Expert Comment

by:jrocnuck
ID: 9745596
I'm not sure I can help you with that..  

I could suggest reading the documentation on the function:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcesdkr/html/_wcesdk_Win32_GetAsyncKeyState.asp?frame=true

and maybe

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceui40/html/cerefcheckingotherkeys.asp

this function only tells you the state of a particular key.  You have to ask the state of the 'a' key for example..

Return Values
If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down. The least significant bit is not valid in Windows CE, and should be ignored. GetAsyncKeyState returns the current key state even if a window in another thread or process currently has the keyboard focus.
0
 

Author Comment

by:DarioDeNicola
ID: 9747287
oh, i've already read about the GetAsyncKeyState, the problem is that the least significant bit is ignored in windows ce,according to the msdn documentation....
is there a low-level method to catch interrupts generated by pressing keys?

thank u
0
 
LVL 5

Expert Comment

by:jrocnuck
ID: 9749996
Something I found (probably no longer works) at  http://www.cegadgets.com/wincedevfaq.htm#5.17%20How%20can%20I%20reprogram%20the%20buttons%20on%20the%20device?%20Can%20I%20use%20hardware%20buttons%20to%20initiate%20application%20functionality?

10.9 SetWindowsHookEx really does work, sort of.

If you believe the Microsoft documentation, SetWindowsHookEx does not work under Windows CE. However, if you have read this FAQ you know that SetWindowsHookEx can be found in coredll (2.11 PSPC and HPC Pro versions only). The question is, what does it do, and is it a full implementation.

I have been wanting to write an ATL control that passes messages (like WM_HIBERNATE) to VBCE. One way to do this is to create an ATL control, implement a connection point, and install a windows hook that monitors the message queue, and whenever a relevant message appears, fires an event off to VB. Unfortunately, every combination of

gHook = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC) MyWndProc, gInstance, 0);

returned an error 87 (incorrect parameter). Further searching through the PB header files reveals only three hook-related defines:

#define WH_JOURNALRECORD 0
#define WH_JOURNALPLAYBACK 1
#define WH_KEYBOARD_LL 20

So I stopped using WH_CALLWNDPROC and tried WH_KEYBOARD_LL, and found that it worked quite well. I actually implemented a hook callback that watched for VK_OFF, and when detected, waited 2 seconds before returning. Whenever I pressed the power button the device would indeed wait before turning off. I also attempted to abort the power off by returning a non-zero number from the hook callback. While this does prevent the power button from powering off the device, it also causes the device to crash. Besides, VK_OFF is only generated when the off button is pressed, and not when a device time-out occurs, so the code was of marginal use. However, if you need to trap other low-level keyboard commands (ctrl-esc, etc) SetWindowsHookEx might be for you.

While I imagine journal record and playback are implemented as well, I have not tried them.


------------------------------------------------------
It's starting to sound like an extremely difficult task for CE..  I've checked some newsgoups:

look at the comments from these folks:

http://groups.google.com/groups?q=GetAsyncKeyState+windows+ce&hl=en&lr=&ie=UTF-8&selm=7on3rl%24oqs%241%40nnrp1.deja.com&rnum=3

this one sounds like what you said.. like the bit is ignored:
http://groups.google.com/groups?q=GetAsyncKeyState+windows+ce&hl=en&lr=&ie=UTF-8&selm=8h43l4%24spv%241%40nnrp1.deja.com&rnum=5

and this one:
http://search.communities.microsoft.com/newsgroups/previewFrame.asp?ICP=GSS3&sLCID=US&sgroupURL=microsoft.public.windowsce.platbuilder&sMessageID=%253COWujAj6mDHA.3504@TK2MSFTNGP11.phx.gbl%253E


It may benefit you to post this question to that particular newsgroup.


also could check this.... http://www.pocketpcdn.com/sections/input.html

0
 

Author Comment

by:DarioDeNicola
ID: 9802835
I've used GetAsyncKeyState and some test variables to detect pressed keys from an external keyboard and it works fine!!
but i've an another problem:how can I detect inputs coming from the SIP?
is there a way to realize a complete pocket pc keylogger that logs external keybord inputs and sip inputs?
please help me!!
thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 58
format the code in java 6 101
only14 challenge 19 81
matchUp  challenge 9 115
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question