Detecting that the user is away from the PC with .NET

前端 未结 4 952
借酒劲吻你
借酒劲吻你 2020-12-20 04:10

I have a desktop application in which I would like to know two things:

  1. Is the user currently on the PC (more specifically, is he giving any input to the PC), s
相关标签:
4条回答
  • 2020-12-20 04:25

    Here is the code to detect if a screen saver is running. See this for more details

    const int SPI_GETSCREENSAVERRUNNING = 114;
    
    [DllImport( "user32.dll", CharSet = CharSet.Auto )]
    private static extern bool SystemParametersInfo( 
       int uAction, int uParam, ref bool lpvParam, 
       int flags );
    
    
    // Returns TRUE if the screen saver is actually running
    public static bool GetScreenSaverRunning( )
    {
       bool isRunning = false;
    
       SystemParametersInfo( SPI_GETSCREENSAVERRUNNING, 0, 
          ref isRunning, 0 );
       return isRunning;
    }
    
    0 讨论(0)
  • 2020-12-20 04:31

    http://dataerror.blogspot.com/2005/02/detect-windows-idle-time.html

    ^ Detect Windows Idle Time. :)

    The enabler for this feature is the GetLastInputInfo() Win32 API and the LASTINPUTINFO Win32 structure.

    0 讨论(0)
  • 2020-12-20 04:40

    You could use a global keyboard/mouse hook and just reset your "counter" to 0 when you receive an event from either. When your counter reaches the idle time that you're looking for, perform your background actions.

    There is some code here that allows you to easily do the hooking in .NET: http://www.codeproject.com/KB/cs/globalhook.aspx

    0 讨论(0)
  • 2020-12-20 04:43

    Rather than figuring out when to run more intensive work... Consider doing your "intensive work" as early as you can, but at a lower thread priority.

    I don't think your questions have an answer in pure C#, unless you poll the mouse position and observe movements... Or something like that.

    0 讨论(0)
提交回复
热议问题