Capturing the length of a key press
I'm currently writing a piece of code which captures a key press and determines the length of that press, and I'm having trouble deciding how precisely I should go about calculating the lenght of the press. Would a timer be the best idea?
My current setup has an "OnMessage" function which kicks-in whenever a message (key press, alarm activation etc.) is received by the system. I'm not sure if the functionality is such that a continued key press will result in multiples "key pressed" messages being sent to the system, in which case it may be simpler.
If a timer is required, what's the most simple/appropriate method of implementing it?
I'm looking to have say:
1 second press sets i value = 1
5 second press sets i value = 5
10 second press sets i value = 10
And so on...
I need something which regularly checks whether the key is still pressed, and increases the value accordingly.
Many thanks in advance. :-)
Re: Capturing the length of a key press
You can avoid a timer, since Windows messages contain the time when they were posted.
Retrieve the message structure with GetMessage(). See http://msdn.microsoft.com/library/de...ctures/msg.asp.
Re: Capturing the length of a key press
You may find that setting a hook using
Code:
// setup the keyboard hook
m_keyboardHookHandle = ::SetWindowsHookEx(
WH_KEYBOARD,
KeyboardHookFunction,
GetInstanceHandle(),
GetCurrentThreadId());
// finish with the hook
if (m_keyboardHookHandle != NULL)
{
::UnhookWindowsHookEx(m_keyboardHookHandle);
m_keyboardHookHandle = NULL;
}
LRESULT CALLBACK KeyboardHookFunction(int code, WPARAM wParam, LPARAM lParam)
{
if ((HIWORD(lParam) & KF_UP) == 0)
{
if ((HIWORD(lParam) & KF_REPEAT) == 0)
{
// its not a repeat key message
// its a key down message
// start a timer here for this key
}
}
else
{
// its a key-up message, switch back to correct cursor
// end correct timer here
}
return ::CallNextHookEx(m_keyboardHookHandle, code, wParam, lParam);
}
will allow you to get every key press/release in the hook function and determine times there