Здравствуйте, krpav, Вы писали:
K>Здравствуйте, alku, Вы писали:
A>>есть да способа:
A>>1. через хуки
A>>2. через Direct Input
A>>второй способ проще — потому как уже есть для Visual Basic ком врапер...
K>Скажи подробней, что за зверь Direct Input, и где найти врапер
вот тебе старая наработочка... только при юзе или перепиши класс или тогда уже копирайт оставь...
using System;
using DxVBLib;
using System.Collections;
using System.Diagnostics;
using UtilityLibrary.Serialization;
namespace ELEKS_Software_Ltd.TeamPro_Client
{
public class UserIdleCatcher : DirectXEvent, IDisposable
{
#region Class Members
private bool m_bDisposed;
private bool m_bDevicesInitialized;
private int m_hndl;
private DateTime m_lastActivity; //Time of last user activity
private int EventN; //DirectX Event
private DirectX7 directX; //DirectX itself
private DirectInput DInput; //DirectInput itself
private DirectInputDevice device; //Temporary var for devices
private ArrayList DeviceList = new ArrayList( 5 );//List of used devices
private SettingsSerialization m_Options;
#endregion
#region Class Properties
/// <summary>
///
/// </summary>
public int MaxIdleTime
{
get
{
return m_Options[ "Time Track" ].GetIntValue( "Idle time", 5 );
}
set
{
m_Options[ "Time Track" ].AddValue( "Idle time", value );
}
}
/// <summary>
///
/// </summary>
public bool isUserOnIdle
{
get
{
TimeSpan interval = DateTime.Now - m_lastActivity;
return interval.TotalMinutes > MaxIdleTime;
}
}
#endregion
#region DirectXEvent Support
public void DXCallback(int eventid)
{
m_lastActivity = DateTime.Now;
}
#endregion
#region Class Initialization / Finalization
/// <summary>
///
/// </summary>
private void InitDirectX()
{
try
{
directX = new DirectX7Class();
DInput = directX.DirectInputCreate();
EventN = directX.CreateEvent( this );
Trace.WriteLine( "DirectX Initialized" );
}
catch
{
throw new ApplicationException( "Needs DirectX 7" );
}
}
/// <summary>
///
/// </summary>
private void InitDevices()
{
if ( m_bDevicesInitialized || directX == null ) return;
try
{
DirectInputEnumDevices ED = DInput.GetDIEnumDevices( CONST_DIDEVICETYPE.DIDEVTYPE_KEYBOARD, CONST_DIENUMDEVICESFLAGS.DIEDFL_ATTACHEDONLY );
for ( int i = 1; i <= ED.GetCount(); i++ )
{
device = DInput.CreateDevice( ED.GetItem( i ).GetGuidInstance() );
device.SetCooperativeLevel( m_hndl, CONST_DISCLFLAGS.DISCL_NONEXCLUSIVE | CONST_DISCLFLAGS.DISCL_BACKGROUND );
device.SetCommonDataFormat( CONST_DICOMMONDATAFORMATS.DIFORMAT_KEYBOARD );
device.SetEventNotification( EventN );
DeviceList.Add( device );
device.Acquire();
Trace.WriteLine( "Keyboard Initialized" );
}
}
catch
{
throw new ApplicationException( "Can not initialize keyboard devices" );
}
try
{
DirectInputEnumDevices ED = DInput.GetDIEnumDevices( CONST_DIDEVICETYPE.DIDEVTYPE_MOUSE, CONST_DIENUMDEVICESFLAGS.DIEDFL_ATTACHEDONLY );
for ( int i = 1; i <= ED.GetCount(); i++ )
{
device = DInput.CreateDevice( ED.GetItem( i ).GetGuidInstance() );
device.SetCooperativeLevel( m_hndl, CONST_DISCLFLAGS.DISCL_NONEXCLUSIVE | CONST_DISCLFLAGS.DISCL_BACKGROUND );
device.SetCommonDataFormat( CONST_DICOMMONDATAFORMATS.DIFORMAT_MOUSE );
device.SetEventNotification( EventN );
DeviceList.Add( device );
device.Acquire();
Trace.WriteLine( "Mouse Initialized" );
}
m_bDevicesInitialized = true;
}
catch
{
throw new ApplicationException( "Can not initialize mouse devices" );
}
}
/// <summary>
///
/// </summary>
private UserIdleCatcher (){}
/// <summary>
///
/// </summary>
/// <param name="Handle"></param>
public UserIdleCatcher( int Handle )
{
m_lastActivity = DateTime.Now;
m_hndl = Handle;
InitDirectX();
InitDevices();
m_Options = new SettingsSerialization( null );
}
/// <summary>
///
/// </summary>
/// <param name="Handle"></param>
/// <param name="settings"></param>
public UserIdleCatcher( int Handle, SettingsSerialization settings )
{
m_lastActivity = DateTime.Now;
m_hndl = Handle;
InitDirectX();
InitDevices();
m_Options = settings;
}
/// <summary>
///
/// </summary>
~UserIdleCatcher()
{
Dispose();
}
/// <summary>
///
/// </summary>
public void Dispose()
{
if ( m_bDisposed ) return;
m_bDisposed = true;
for ( int i = 0; i < DeviceList.Count; i++ )
{
DirectInputDevice DID = DeviceList[ i ] as DirectInputDevice;
if ( DID != null )
{
DID.Unacquire();
//DID.SetEventNotification( 0 );
}
}
DeviceList.Clear();
}
#endregion
}
}