ok . . . here is the C++ portion of the code. I have excluded the .DEF file.
#include "stdafx.h"
HANDLE m_hModule;
HWND m_hWnd;
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
//Keep track of the DLL handle.
m_hModule = hModule;
return TRUE;
}
LRESULT CALLBACK KeyboardProc( int nCode, WPARAM wParam, LPARAM lParam)
{
/*
This Routine is call whenever a key is pressed in any
window. The following params are returned -
nCode:
wParam: The virtual keycode of the key that
generated the message.
lParam: Bit Mask as follows -
Bit 00-15 :: The Repeat Count.
Bit 16-23 :: The Scan Code.
Bit 24 :: Extended key. Set to 1 if the
key is an extended key;
otherwise, it is 0.
Bit 25-28 :: Reserved
Bit 29 :: The Context code. Set to 1 if
the ALT key is down;
otherwise, it is 0.
Bit 30 :: The Previous Key State. Set to
1 if the key was down before
the message is sent;
otherwise, it is 0 if the key
is up.
Bit 31 :: The transistion state. Set to
0 if the key is being pressed
and 1 if it is being released.
*/
MessageBox (m_hWnd ,"Key Pressed","KeyBoardHook", 1);
SendMessage (m_hWnd,nCode,wParam,lParam);
return 0;
}
LONG APIENTRY MySetWindowHook(HWND hWnd)
{
HHOOK hHook;
m_hWnd = hWnd;
hHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)
KeyboardProc, (HINSTANCE)m_hModule, 0 );
return (LONG)hHook;
}
This is compiled into a dll and MySetWindowsHook is called from a VB application. After that, any keypress, in any window, will cause KeyBoardProc to fire.
Now I know that this is not complete (I will post the rest later - I have to go to work now), but it should give you the basic idea of what I am doing. This code still needs to be able to eventually release its hook and I need to implement a callback into the VB code so that the VB client knows about the keypress (still fine tuning the VB callback process).
If you are interested, you can implement the KeyboardProc callback in VB as well by placing the following code in a module . . .
Private Sub KeyboardProc(ByVal intCode As Integer, ByVal WPARAM As Long, ByVal LPARAM As Long)
'** Do something with the data
End Sub
but since it is not in a DLL, it will only monitor the current process. I have tried moving the mod to an ActiveX DLL (which I kept in memory by hanging onto a reference) and while it will receive messages about keystrokes in the current process, it does not know about keystrokes from other processes . . . to do that, I had to use the C++ code/DLL listed above.
Hope this helps you out with your problem! And, if you have any idea regarding how to implement the callback in pure VB let me know . . .
its not that I don't like using C++ (actually, I like to use it) . . . I am just curious to know if this could be done with PURE VB.
- Jeff Marler B-)