Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch);
void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e)
if (e.Reason == SessionSwitchReason.SessionLock)
//I left my desk
else if (e.Reason == SessionSwitchReason.SessionUnlock)
//I returned to my desk
There is no reliable API for detected it from MSDN.
This function has the same result as pressing Ctrl+Alt+Del and clicking Lock Workstation. To unlock the workstation, the user must log in. There is no function you can call to determine whether the workstation is locked. To receive notification when the user
logs in, use the WTSRegisterSessionNotification function to receive WM_WTSSESSION_CHANGE messages. You can use session notifications to track the desktop state so you know whether it is possible to interact with the user.
If your process is running under user mode, you may call OpenInputDesktop() to detect secure desktops if it fails.
Note this may not what you wanted because it fails for UAC popup/Locked desktop. There is no API for differentiate between Locked Desktop and UAC popup.