C # class for obtaining the time for keyboard and mouse operations

Source: Internet
Author: User

The screen saver function must be implemented recently in the development project, that is, when you do not perform any operation on the system within the specified time, the system will automatically enter the screen saver status.

Therefore, a class is encapsulated to obtain mouse and keyboard actions, and a method is provided to return the time when the user does not have an operating system.

The Code is as follows:

Public class mousekeyboardoperate
{
/// <Summary>
/// Create a struct to return the capture time
/// </Summary>
[Structlayout (layoutkind. Sequential)]
Struct lastinputinfo
{
/// <Summary>
/// Set the block capacity of the struct.
/// </Summary>
[Financialas (unmanagedtype. U4)]
Public int cbsize;

/// <Summary>
/// Capture time
/// </Summary>
[Financialas (unmanagedtype. U4)]
Public uint dwtime;
}

[Dllimport ("user32.dll")]
Private Static extern bool getlastinputinfo (ref lastinputinfo plii );
/// <Summary>
/// Obtain the time when no operation is performed on the keyboard or mouse.
/// </Summary>
/// <Returns> the time interval from the last time the user used the system to the present, measured in seconds </returns>
Public static long getlastinputtime ()
{
Lastinputinfo vlastinputinfo = new lastinputinfo ();
Vlastinputinfo. cbsize = marshal. sizeof (vlastinputinfo );
If (! Getlastinputinfo (ref vlastinputinfo ))
{
Return 0;
}
Else
{
Long Count = environment. tickcount-(long) vlastinputinfo. dwtime;
Long icount = count/0/1000;
Return icount;
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.