Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting User Activity

Tags:

c#

windows

I need to create a program that monitors a computer for activity. Such as a mouse move, mouse click or keyboard input. I don't need to record what has happened just that the computer is in use. If their computer has not been in use for a certain period of time, i.e. 15 mins, I need to fire off an event.

Is there a way that I can get notified of these events?

like image 339
Sally Avatar asked Mar 09 '11 11:03

Sally


People also ask

What is Activity Recognition API?

The Activity Recognition API automatically detects activities by periodically reading short bursts of sensor data and processing them using machine learning models.

What is activity recognition in computer vision?

It means that a human should place wearable sensors on their body for the system to collect data. The second approach is vision-based, i.e. human activity recognition from video or images. In this case, the system gathers data with a camera to identify activities.

Why is human activity recognition important?

Device sensors provide insights into what persons are doing in real-time (walking, running, driving, etc.). Knowing users' activity allows, for instance, to interact with them through an app.


1 Answers

Thank you LordCover. This code is from here. This class takes control of the keyboard and mouse controls for you. You can use in a timer like this:

private void timer1_Tick(object sender, EventArgs e)
{
    listBox1.Items.Add(Win32.GetIdleTime().ToString());
    if (Win32.GetIdleTime() > 60000) // 1 minute
    {
        textBox1.Text = "SLEEPING NOW";
    }
}

Main code for control. Paste to your form code.

internal struct LASTINPUTINFO
{
    public uint cbSize;
    public uint dwTime;
}

public class Win32
{
    [DllImport("User32.dll")]
    public static extern bool LockWorkStation();

    [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);
    }

    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;
    }
}
like image 167
Murat Atasoy Avatar answered Sep 17 '22 20:09

Murat Atasoy