Improve company productivity with a Business Account.Sign Up

x
?
Solved

Knowing when the user locks the PC

Posted on 2009-05-07
5
Medium Priority
?
327 Views
Last Modified: 2013-12-17
Hi,
   As an initiative to save power in my company, I am planning to write an application which turns off the monitor when the user locks his/her PC.

   The OS is Windows XP SP2.

   I have the code for switching off the monitor.

   The user locks his/her machine by either pressing the "WindowsKey + L"  or pressing "Ctrl + Alt+ Del" and then choosing the Lock Computer option.

   Should I make a windows service? how will the service come to know whether the user has locked the machine?
   I want to code it in C#.

regards,
CuteBug
0
Comment
Question by:CuteBug
5 Comments
 
LVL 6

Expert Comment

by:Cebik
ID: 24325101
just enable power safe - disable monitor after period of time
;)
0
 
LVL 16

Author Comment

by:CuteBug
ID: 24325446
Well we do not have access to that feature. Only the sysadmins have access.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1200 total points
ID: 24327035
You can use a global key hook to trap keys pressed within the OS. From http://blogs.msdn.com/toub/archive/2006/05/03/589423.aspx

Now obviously you can modify the code to fit your needs, but as is, you could check "message" for "LWin/RWin", store it to some private variable if set and then check for "L" to be the next key pressed.

Ctrl-Alt-Delete will be a little more difficult to capture. The method below will not work. From what I have read, you have to write something called a GINA stub to capture that--please don't ask me to explain :/ The following article talks about it though.

http://msdn.microsoft.com/en-us/library/aa375198(VS.85).aspx
http://bytes.com/groups/net/108144-capture-ctrl-alt-del   <<--- last post
//////////////////////////////////////////
// Intercept Keys Class
//////////////////////////////////////////
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
 
class InterceptKeys
{
    private const int WH_KEYBOARD_LL = 13;
    private const int WM_KEYDOWN = 0x0100;
    private const int WM_SYSKEYDOWN = 0x0104; 
    private static LowLevelKeyboardProc _proc = HookCallback;
    private static IntPtr _hookID = IntPtr.Zero;
    private static ExternalCallback callback;
 
    public delegate void ExternalCallback(string message);
 
    public static void Begin(ExternalCallback cb)
    {
        _hookID = SetHook(_proc);
        callback = cb;
    }
 
    public static void End()
    {
        UnhookWindowsHookEx(_hookID);
        _hookID = IntPtr.Zero;
    }
 
    private static IntPtr SetHook(LowLevelKeyboardProc proc)
    {
        using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
            return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
                GetModuleHandle(curModule.ModuleName), 0);
        }
    }
 
    private delegate IntPtr LowLevelKeyboardProc(
        int nCode, IntPtr wParam, IntPtr lParam);
 
    private static IntPtr HookCallback(
        int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 && (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN))
        {
            int vkCode = Marshal.ReadInt32(lParam);
            callback.Invoke(((Keys)vkCode).ToString());
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }
 
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int idHook,
        LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
 
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool UnhookWindowsHookEx(IntPtr hhk);
 
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
        IntPtr wParam, IntPtr lParam);
 
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string lpModuleName);
}
 
 
//////////////////////////////////////////
// Form
//////////////////////////////////////////
using System;
using System.Windows.Forms;
 
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            InterceptKeys.Begin(UpdateBox);
        }
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            InterceptKeys.End();
        }
 
        private void UpdateBox(string message)
        {
            this.listBox1.Items.Add(message);
        }
    }
}

Open in new window

0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 800 total points
ID: 24328106
0
 
LVL 16

Author Closing Comment

by:CuteBug
ID: 31578966
Thanks a lot! Your answers have been very helpful.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Watch the video to know the process of migration of Exchange or Office 365 mailboxes in absence of MS Outlook. It is an eminent tool which can easily migrate Public, Archive user mailboxes from one another Exchange server and Office 365. Kernel Migr…
Watch the software video of Kernel Import PST to Office 365 tools which can easily import PST and OST files to Office 365 for bulk mailboxes. The process of migration is simple and user can map source and destination mailboxes and easily import data…

595 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question