Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Key capture 1

Status
Not open for further replies.

dominochmiel

Programmer
Jan 28, 2003
40
PL
I need to write programm which can catch every key press or mouse event in windows. Friend told me that I can use something called Hook but I can't find how it works. Please help me?
 
I'm afraid I know nothing about hook, but in BCB you can you the OnKeyDown, OnKeyUp, OnKeyPress, OnMouseDown, OnMouseUp events to trap these.
Or, in a normal window, just look out for WM_KEYPRESS (I think) events in your main switch statement.
The help files have loads of information on these.
Hope I helped,
DJL

Common sense is what tells you the world is flat.
 
But using this I can capture just messages or events which are behaving to my app. I need to capture every key press in whole system not just in my app. In shortening I need to write something like WinSight32 which is included to BCB.
 
To actually have it event driven, check out the keybd_event event. Just catch it in the system messages, and you'll know what key was pressed.

You can also use an MFC function called GetKeyboardState.
You would just keep a history of the states every 100 ms or so, then if a state changes, record it.

I've included the Microsoft help page for it.

The GetKeyboardState function copies the status of the 256 virtual keys to the specified buffer.

BOOL GetKeyboardState(

PBYTE lpKeyState // address of array to receive status data
);


Parameters

lpKeyState

Points to the 256-byte array that will receive the status data for each virtual key.



Return Values

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

An application can call this function to retrieve the current status of all the virtual keys. The status changes as a thread removes keyboard messages from its message queue. The status does not change as keyboard messages are posted to the message queue.
When the function returns, each member of the array pointed to by the lpKeyState parameter contains status data for a virtual key. If the high-order bit is 1, the key is down; otherwise, it is up. If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.

To retrieve status information for an individual key, use the GetKeyState function.
An application can use the virtual-key code constants VK_SHIFT, VK_CONTROL and VK_MENU as indices into the array pointed to by lpKeyState. This gives the status of the SHIFT, CTRL, or ALT keys without distinguishing between left and right. An application can also use the following virtual-key code constants as indices to distinguish between the left and right instances of those keys:

VK_LSHIFT VK_RSHIFT
VK_LCONTROL VK_RCONTROL
VK_LMENU VK_RMENU


These left- and right-distinguishing constants are available to an application only through the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions.

See Also

GetKeyState, GetAsyncKeyState, MapVirtualKey, SetKeyboardState

Hope it helps,
Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top