CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2006
    Posts
    162

    ::GetLastInputInfo (..)

    I'm having trouble understanding how to properly use this function, most likely since my course hasn't covered data structures or pointers yet, but I'm very interested in learning.

    The Function reads as:
    Code:
    BOOL GetLastInputInfo(      
    
        PLASTINPUTINFO plii
    );
    Parameters: plii, Pointer to a LASTINPUTINFO structure that receives the time of the last input event

    Data Structure reads as:
    Code:
    typedef struct tagLASTINPUTINFO {
        UINT cbSize;
        DWORD dwTime;
    } LASTINPUTINFO, *PLASTINPUTINFO;
    Members: cbSize, Must be set to sizeof(LASTINPUTINFO).
    dwTime, Tick count when the last input event was received.

    As for the tick count... I checked MSDN and got
    Code:
    DWORD GetTickCount(void);
    Though, this retrieves the time in milliseconds since the system was started, not from last input event. Anywho, I am thoroughly confused!
    And would REAAAAALLY appreciate some help

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: ::GetLastInputInfo (..)

    You can do something like this:
    Code:
    LASTINPUTINFO lpi;
    lpi.cbSize = sizeof(LASTINPUTINFO);
    
    if (!GetLastInputInfo(&lpi))
    {
        // failed, use GetLastError to get error code
    }
    
    // lpi.dwTime now holds the tick count when last input was made
    - petter

  3. #3
    Join Date
    Feb 2006
    Posts
    162

    Re: ::GetLastInputInfo (..)

    Alright thanks, wildfrog.
    This is my code as is, now (not very complex as im just trying to get the understanding of the getlastinputinfo function)

    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    typedef struct tagLASTINPUTINFO {
        UINT cbSize;
        DWORD dwTime;
    } LASTINPUTINFO, *PLASTINPUTINFO;
    
    int main()
    {
    
    	int enter;
    	LASTINPUTINFO lpi;
    	lpi.cbSize = sizeof(LASTINPUTINFO);
    
    	if (!GetLastInputInfo(&lpi))
    	{
        // failed, use GetLastError to get error code
    	}
    
    	// lpi.dwTime now holds the tick count when last input was made
    
    	cout << "enter 1: ";
    	cin >> enter;
    	if (enter==1){
    		cout << lpi.dwTime << endl;
    	}
    	return 0;
    }
    I get 1 error when compiling though:
    error C2065: 'GetLastInputInfo' : undeclared identifier

    :/

  4. #4
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: ::GetLastInputInfo (..)

    Read GetLastInputInfo.

    You need to set up target platform (GetLastInputInfo needs Win2K):
    Code:
    #define _WIN32_WINNT   0x0500
    #define WINVER	       0x0500
    
    #include <windows.h>
    For more read Using the Windows Headers.

    Then you also need to tell the linker to link User32.lib, you can either do that through the project setting or add this line to your code:
    Code:
    #pragma comment(lib, "user32.lib")
    And, the LASTINPUTINFO structure is already defined (in winuser.h included in windows.h) so you shouldn't define it yourself.

    - petter

  5. #5
    Join Date
    Feb 2006
    Posts
    162

    Re: ::GetLastInputInfo (..)

    Thanks for clearing that up for me, I REALLY appreciate it.
    Question though, since you said GetLastInputInfo needs win2k, which operating systems would that make this obsolete for?

  6. #6
    Join Date
    Feb 2006
    Posts
    162

    Re: ::GetLastInputInfo (..)

    alright...sorry...another question :-/
    I implemented the changes you pointed out, and it compiled and ran fine
    heres the code
    Code:
    #define _WIN32_WINNT   0x0500
    #define WINVER	       0x0500
    
    #include <windows.h>
    #include <iostream>
    #pragma comment(lib, "user32.lib")
    using namespace std;
    
    int main()
    {
    
    	int enter;
    	LASTINPUTINFO lpi;
    	lpi.cbSize = sizeof(LASTINPUTINFO);
    
    	if (!GetLastInputInfo(&lpi))
    	{
        // failed, use GetLastError to get error code
    	}
    
    	// lpi.dwTime now holds the tick count when last input was made
    
    	cout << "enter 1: ";
    	cin >> enter;
    	if (enter==1){
    		cout << lpi.dwTime << endl;
    	}
    	return 0;
    }
    except lpi.dwTime returned the value of 609977562
    which really puzzles me

  7. #7
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: ::GetLastInputInfo (..)

    Question though, since you said GetLastInputInfo needs win2k, which operating systems would that make this obsolete for?
    You should be able to figure that out by looking at the 'Using the Windows Headers' link in my previous post.

    Anyway, as far as I can see it won't work on these platforms:
    Windows NT 4.0 (and previous)
    Windows 98
    Windows 95

    But should work on :
    Windows 2000
    Windows XP
    Windows Server 2003
    Windows Vista
    Windows Server "Longhorn"

    except lpi.dwTime returned the value of 609977562
    which really puzzles me
    Compare that value to what you get from GetTickCount(). Their difference is the timespan (in ms) since last the input.

    - petter

  8. #8
    Join Date
    Feb 2006
    Posts
    162

    Re: ::GetLastInputInfo (..)

    Yes, sorry about my question reguarding the obsolete operating systems, it was uncalled for, anywho.
    updated code reads as:
    Code:
    #define _WIN32_WINNT   0x0500
    #define WINVER	       0x0500
    
    #include <windows.h>
    #include <iostream>
    #pragma comment(lib, "user32.lib")
    using namespace std;
    
    int main()
    {
    
    	double tickCount;
    	double idleCount;
    	LASTINPUTINFO lpi;
    	lpi.cbSize = sizeof(LASTINPUTINFO);
    
    	if (!GetLastInputInfo(&lpi))
    	{
        // failed, use GetLastError to get error code
    	}
    
    	// lpi.dwTime now holds the tick count when last input was made
    
    	cout << lpi.dwTime << endl;
    	tickCount = GetTickCount();
    	idleCount = ( lpi.dwTime - tickCount );
    	cout << idleCount << endl;
    	return 0;
    }
    first displays the lpi.dwTime, which returns 611282093
    then displays idleCount, which is the difference between lpi.dwTime and GetTickCount().
    Seems weird to me though, the results that it has returned for idleCount have ranged from -234 to 0.
    Is this because I was last idle zero seconds ago, if that's the case the negatives confuse me ;o

    *Edit*
    Also, you put a comment that says "// lpi.dwTime now holds the tick count when last input was made" but then said I had to get the difference, so what value is lpi.dwTime holding?
    Last edited by 80Degrees; March 5th, 2006 at 09:14 PM.

  9. #9
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: ::GetLastInputInfo (..)

    so what value is lpi.dwTime holding?
    It hold the number of ticks (ms) since the computer was last restarted until the last input was made. And GetTickCount return the number of ticks from the computer was last restarted and until now.

    It seems that you've swapped the two around. You call GetTickCount after GetLastInputInfo so GetTickCount will produce a higher tick count, as a result you'll get a number <= 0.

    - petter

  10. #10
    Join Date
    Feb 2006
    Posts
    162

    Re: ::GetLastInputInfo (..)

    It seems that you've swapped the two around. You call GetTickCount after GetLastInputInfo so GetTickCount will produce a higher tick count, as a result you'll get a number <= 0.
    Hmm.. I'm looking through my code and I'm not sure where I have them misordered, do you mean in the subtraction? Cause otherwise it seems that I have GetTickCount after GetLastInputInfo...atleast I think, ugh, I'm sorry for all the questions

  11. #11
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: ::GetLastInputInfo (..)

    The subtraction, yes.

    - petter

  12. #12
    Join Date
    Feb 2006
    Posts
    162

    Re: ::GetLastInputInfo (..)


    Ok after swapping them, it came back with 1538 which is perfect since its in milliseconds / 1000 so I last made some input 1.5 seconds ago, which seems about right.

    Last last last last last last, LAST question
    Does the GetLastInputInfo check for any sort of input, including mouse click, mouse movement, keyboard click?

  13. #13
    Join Date
    May 2005
    Posts
    4,954

    Re: ::GetLastInputInfo (..)

    @80Degrees please don’t post the same question on different threads you already asked it here.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

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