Код: Выделить всё
using System;
using System.Runtime.InteropServices;
public class WindowsFunctions
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
public static int TicksSinceLastInput()
{
var info = new LASTINPUTINFO();
GetLastInputInfo(ref info);
var lastInputTickCount = info.dwTime;
return (int)lastInputTickCount;
}
}
[StructLayout(LayoutKind.Sequential)]
struct LASTINPUTINFO
{
public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));
[MarshalAs(UnmanagedType.U4)]
public UInt32 cbSize;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dwTime;
}
< /code>
Однако при запуске, info.dwtime < /code> равен нулю. < /p>
Запуск в vs2019 ide < /p>
обновление < /strong>:
я пытался сделать TickssincelastInput < /code> не STATIC. Теперь: < /p>
[TestMethod]
public void TestTicksSinceLastInput()
{
var funcs = new WindowsFunctions();
var ticks = funcs.TicksSinceLastInput();
Assert.IsTrue( ticks > 0);
}
< /code>
обновление < /strong>:
Мой код сейчас: < /p>
public class WindowsFunctions
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
public int TicksSinceLastInput()
{
var info = new LASTINPUTINFO();
var result = GetLastInputInfo(ref info);
var lastInputTickCount = info.dwTime;
return (int)lastInputTickCount;
}
}
Подробнее здесь: https://stackoverflow.com/questions/677 ... ing-dwtime