Frage

Ich schreibe eine kleine Tray-Anwendung, die das letzte Mal, wenn ein Benutzer interagiert mit ihrer Maschine erfassen muss, um zu bestimmen, ob sie untätig sind.

Gibt es eine Möglichkeit, die Zeit ein Benutzer das letzte Mal bewegt die Maus abzurufen, drücken Sie eine Taste oder in irgendeiner Weise mit ihrer Maschine in Wechselwirkung?

ich meine, offensichtlich von Windows verfolgt, dies zu bestimmen, wenn Sie einen Bildschirmschoner oder dem Ausschalten angezeigt werden, usw., also gehe ich davon eine Windows-API gibt es diese für die Beschaffung von mir?

War es hilfreich?

Lösung

GetLastInputInfo. Documented at PInvoke.net.

Andere Tipps

include following namespaces

using System;
using System.Runtime.InteropServices;

and then include following

internal struct LASTINPUTINFO 
{
    public uint cbSize;

    public uint dwTime;
}

/// <summary>
/// Helps to find the idle time, (in milliseconds) spent since the last user input
/// </summary>
public class IdleTimeFinder
{
    [DllImport("User32.dll")]
    private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);        

    [DllImport("Kernel32.dll")]
    private static extern uint GetLastError();

    public static uint GetIdleTime()
    {
        LASTINPUTINFO lastInPut = new LASTINPUTINFO();
        lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
        GetLastInputInfo(ref lastInPut);

        return ((uint)Environment.TickCount - lastInPut.dwTime);
    }
/// <summary>
/// Get the Last input time in milliseconds
/// </summary>
/// <returns></returns>
    public static long GetLastInputTime()
    {
        LASTINPUTINFO lastInPut = new LASTINPUTINFO();
        lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
        if (!GetLastInputInfo(ref lastInPut))
        {
            throw new Exception(GetLastError().ToString());
        }       
        return lastInPut.dwTime;
    }
}

To convert the tickcount into time you can use

TimeSpan timespent = TimeSpan.FromMilliseconds(ticks);

Note. This routine uses the term TickCount but the values are in milliseconds and are so not the same as Ticks.

From MSDN article on Environment.TickCount

Gets the number of milliseconds elapsed since the system started.

Code:

 using System;
 using System.Runtime.InteropServices;

 public static int IdleTime() //In seconds
    {
        LASTINPUTINFO lastinputinfo = new LASTINPUTINFO();
        lastinputinfo.cbSize = Marshal.SizeOf(lastinputinfo);
        GetLastInputInfo(ref lastinputinfo);
        return (((Environment.TickCount & int.MaxValue) - (lastinputinfo.dwTime & int.MaxValue)) & int.MaxValue) / 1000;
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top