Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

Dicas_xHarbour (user32)
 
.

POSTED BY: REINALDO HENRIQUE - DO GRUPO XHARBOUR.SPANISH.PORTUGUESE

PARA OS AMIGOS (XHARBOUR.ORG E XHARBOUR.COM)

Introduction

The system idle time is the time that has passed since the computer has received its last input from the user. It can be used to detect whether the user is idle or not (like they have done in Yahoo! Messenger, when there is no activity for a certain amount of time the status of the user is shown as idle). This has many other applications as well. The Application idle time (not the system idle time) can be found easily by resetting a timer on every keyboard or mouse event, but in most cases, it is not adequate for our purposes. Unfortunately, the .NET framework class libraries don't provide much support for getting the system idle time directly.

To do this, there are several ways. One way is by hooking into the mouse and keyboard events of the OS. But I believe the easiest way is by using the GetLastInputInfo() Win32 API. I tried using this in C# without success. So, I decided to write a UserControl in C++ that can be used easily in C# or other languages.

DLL Description

The core of the DLL is the two methods GetLastInputTime() and GetIdleTime(). They use the functions GetLastInputInfo() and GetTickCount() defined in windows.h to get the time from system startup to the last user input and the total time passed since system startup, respectively. Please refer the MSDN documentation for more info on GetLastInputInfo() and GetTickCount().

//returns the time from system startup to last input in milliseconds

DWORD RtwIdleDll::RtwIdleDllControl::GetLastInputTime(void)

{

    LASTINPUTINFO lastInput;
    lastInput.cbSize = sizeof(LASTINPUTINFO);

    BOOL success = GetLastInputInfo(&lastInput);

    if(!success)
    {
    DWORD err=GetLastError();
    // report error, throw exception, etc
    }

    DWORD lastInputTime = lastInput.dwTime;
    return lastInputTime;

}

//returns the time since the last input in milliseconds

DWORD RtwIdleDll::RtwIdleDllControl::GetIdleTime(void)

{

    DWORD totalTime = GetTickCount();
    //gets the time from system startup to now in milliseconds

    DWORD lastInputTime = GetLastInputTime();

    DWORD idleTime = totalTime - lastInputTime;

    return idleTime;

}

The following section lists the properties, methods and events exposed by the DLL:

Properties

IdleTriggerTime (get/set) - The time to wait (in milliseconds) before the first Idle and IdleStart events are fired.

IsIdle (get) - Is true when idle, otherwise false.

Methods

ShowAbout() - Shows the About box.

Events

IdleStart - Fired one time when the idle trigger time is passed.

Idle - Fired continuously when idle.

IdleStop - Fired when idle state ends (keyboard or mouse input received).

Limitations

The events are not asynchronous, therefore, using code that would delay the client event handler methods from returning (like modal dialogs or message boxes) inside the event handler methods will sometimes cause unexpected behavior.

Using the DLL

The DLL must be added to another project through the ToolBox. If you are using Visual Studio .NET, right click on the ToolBox and select "Add/Remove Items..." from the popup menu and select the DLL from its location. After that, it will appear in your ToolBox. Then it can be drag'n dropped to any container at design time. After setting the properties and events, it's ready for use.

C# Signature:

[DllImport("user32.dll")]

static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

VB Signature:

<DllImport("user32.dll")> _

Shared Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean

End Function

User-Defined Types:

LASTINPUTINFO

Notes:

Very usefull to detect user-idle state of an application.

Minimum operating systems: Windows 2000

Tips & Tricks:

Please add some!

Sample Code:

This function retrieves the time since last user input

    static int GetLastInputTime()
    {
    int idleTime = 0;
    LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
    lastInputInfo.cbSize = Marshal.SizeOf( lastInputInfo );
    lastInputInfo.dwTime = 0;

    int envTicks = Environment.TickCount;

    if( GetLastInputInfo( ref lastInputInfo ) )
    {
    int lastInputTick = lastInputInfo.dwTime;

    idleTime = envTicks - lastInputTick;
    }

    return (( idleTime > 0 ) ? ( idleTime / 1000 ) : idleTime );
    }

Sample Code vb.net:

This function retrieves the time since last user input

Dim idletime As Integer

Dim lastInputInf As New LASTINPUTINFO()

   Public Function GetLastInputTime() As Integer

    idletime = 0
    lastInputInf.cbSize = Marshal.SizeOf(lastInputInf)
    lastInputInf.dwTime = 0

    If GetLastInputInfo(lastInputInf) Then
    idletime = Environment.TickCount - lastInputInf.dwTime
    End If

    If idletime > 0 Then
    Return idletime / 1000
    Else : Return 0
    End If

   End Function

OBS
FONTES DE DEMONSTRAÇÃO, PODE ME PEDIR POR E-MAIL

reinaldohf@gmail.com

BY REINALDO HENRIQUE

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions