このエントリーをはてなブックマークに追加


ユーザーがキーボードやマウスなど無操作の状態を取得する・GetLastInputInfo

ユーザーがマウスやキーボードなどの操作をしていない状態の時間を取得するGetLastInputInfo APIを使ったサンプルコードになります。
サンプルコードは C#, Visual Basic(VB)になります。

関連サイト

動作環境

  • Windows 10 ver.1909
  • Visual Studio 2019
  • .NET Framework 4.7.2
  • コンソールアプリケーション

サンプルコード

ユーザーがコンピュータを操作していない状態を取得したい場合は、GetLastInputInfo APIで状態を取得できます。
本サンプルコードは、無操作状態の監視のみになるので、実際に無操作時間をカウントする部分のプログラミングは別途必要になります。

動作

以下のサンプルコードを実行すると、以下のような動作になります。
キーボードやマウスを触る(操作する)と終了するだけです。

A01.gif

C# サンプルコード

using System;

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

class Program
{
    [System.Runtime.InteropServices.DllImport("User32.dll")]
    private static extern bool
            GetLastInputInfo(ref LASTINPUTINFO plii);

    static void Main(string[] args)
    {
        Console.WriteLine("Start : GetLastInputInfo API sample");

        LASTINPUTINFO lastInPut = new LASTINPUTINFO();
        lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);

        uint operation_time = 0;
        bool operation = false;
        while (!operation)
        {
            try
            {
                if (!GetLastInputInfo(ref lastInPut))
                {
                    throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
                } 
                if (operation_time == 0) { operation_time = lastInPut.dwTime; }
                if (operation_time != lastInPut.dwTime) { operation = true; }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception:{0}", ex.Message);
            }
            System.Threading.Thread.Sleep(100);
        }
        Console.WriteLine("Stop: ** You touched the computer.(^^)/ **");
    }
}

Visual Basic(VB) サンプルコード

Module Module1

    Friend Structure LASTINPUTINFO
        Public cbSize As UInteger
        Public dwTime As UInteger
    End Structure

    <Runtime.InteropServices.DllImport("User32.dll")>
    Private Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean
    End Function

    Sub Main()

        Console.WriteLine("Start : GetLastInputInfo API sample")
        Dim lastInPut As LASTINPUTINFO = New LASTINPUTINFO()
        lastInPut.cbSize = CUInt(System.Runtime.InteropServices.Marshal.SizeOf(lastInPut))
        Dim operation_time As UInteger = 0
        Dim operation As Boolean = False

        While Not operation
            Try
                If Not GetLastInputInfo(lastInPut) Then
                    Throw New System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error())
                End If
                If operation_time = 0 Then
                    operation_time = lastInPut.dwTime
                End If
                If operation_time <> lastInPut.dwTime Then
                    operation = True
                End If
            Catch ex As Exception
                Console.WriteLine("Exception:{0}", ex.Message)
            End Try
            Threading.Thread.Sleep(100)
        End While
        Console.WriteLine("Stop: ** You touched the computer.(^^)/ **")
    End Sub

End Module

以上、ユーザーがコンピュータを操作していない状態を取得するサンプルコードでした。


添付ファイル: fileA01.gif 181件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2020-01-19 (日) 16:33:28