CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2005
    Posts
    6

    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. :-)

  2. #2
    Join Date
    Jun 2005
    Posts
    1,255

    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.

  3. #3
    Join Date
    May 1999
    Location
    West Sussex, England
    Posts
    1,939

    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
    Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
    Please remember to rate useful answers. It lets us know when a question has been answered.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured