code_junkie

How to tell when Windows is inactive

Nov 11th, 2011
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. [DllImport("user32.dll")]
  2. static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
  3.  
  4. static int GetLastInputTime()
  5. {
  6. int idleTime = 0;
  7. LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
  8. lastInputInfo.cbSize = Marshal.SizeOf( lastInputInfo );
  9. lastInputInfo.dwTime = 0;
  10.  
  11. int envTicks = Environment.TickCount;
  12.  
  13. if( GetLastInputInfo( ref lastInputInfo ) )
  14. {
  15. int lastInputTick = lastInputInfo.dwTime;
  16.  
  17. idleTime = envTicks - lastInputTick;
  18. }
  19.  
  20. return (( idleTime > 0 ) ? ( idleTime / 1000 ) : idleTime );
  21. }
  22.  
  23. [StructLayout( LayoutKind.Sequential )]
  24. struct LASTINPUTINFO
  25. {
  26. public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));
  27.  
  28. [MarshalAs(UnmanagedType.U4)]
  29. public int cbSize;
  30. [MarshalAs(UnmanagedType.U4)]
  31. public UInt32 dwTime;
  32. }
  33.  
  34. using System;
  35. using System.Runtime.InteropServices;
  36. using System.Threading;
  37. using System.Windows.Forms;
  38.  
  39. namespace Example {
  40.  
  41. public class Hook {
  42.  
  43. delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
  44.  
  45. [FlagsAttribute]
  46. public enum WindowMessage {
  47. WM_KEYDOWN = 0x0000000000000100, // &H100
  48. WM_MOUSEMOVE = 0x0000000000000200, // &H200
  49. WM_LBUTTONDOWN = 0x0000000000000201, // &H201
  50. WM_RBUTTONDOWN = 0x0000000000000204, // &H204
  51. WH_KEYBOARD = 2,
  52. WH_MOUSE = 7,
  53. HC_ACTION = 0
  54. }
  55.  
  56. [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
  57. private static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
  58.  
  59. [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
  60. private static extern bool UnhookWindowsHookEx(int idHook);
  61.  
  62. [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
  63. private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
  64.  
  65. //Declare MouseHookProcedure as a HookProc type.
  66. static HookProc MouseHookProcedure;
  67. static HookProc KeyboardHookProcedure;
  68.  
  69. private static int mhMouseHook = 0;
  70. private static int mhKeyboardHook = 0;
  71.  
  72. public Hook() {}
  73.  
  74. public static void Init() {
  75. MouseHookProcedure = new HookProc( MouseHookProc );
  76. KeyboardHookProcedure = new HookProc( KeyboardHookProc );
  77. mhMouseHook = SetWindowsHookEx( (int)WindowMessage.WH_MOUSE, MouseHookProcedure, (IntPtr)0, AppDomain.GetCurrentThreadId() );
  78. mhKeyboardHook = SetWindowsHookEx( (int)WindowMessage.WH_KEYBOARD, KeyboardHookProcedure, (IntPtr)0, AppDomain.GetCurrentThreadId() );
  79. }
  80.  
  81. public static void Terminate() {
  82. UnhookWindowsHookEx( mhMouseHook );
  83. UnhookWindowsHookEx( mhKeyboardHook );
  84. }
  85.  
  86. private static int MouseHookProc( int nCode, IntPtr wParam, IntPtr lParam ) {
  87. if ( nCode >= 0 ) {
  88. //do something here to update the last activity point, i.e. a keystroke was detected so reset our idle timer.
  89. }
  90. return CallNextHookEx( mhMouseHook, nCode, wParam, lParam );
  91. }
  92.  
  93. private static int KeyboardHookProc( int nCode, IntPtr wParam, IntPtr lParam ) {
  94. if ( nCode >= 0 ) {
  95. //do something here to update the last activity point, i.e. a mouse action was detected so reset our idle timer.
  96. }
  97. return CallNextHookEx( mhKeyboardHook, nCode, wParam, lParam );
  98. }
  99.  
  100. }
  101. }
Add Comment
Please, Sign In to add comment