1

Is it possible to emulate key presses with a windows service? For example say I have a service running in the background that anytime a trigger occurs for example the trigger could be it's 2:00PM then I would for example press window key+L to lock computer. Would this be possible in C#?

Marcus Vinicius
  • 1,891
  • 1
  • 19
  • 35
user541597
  • 4,247
  • 11
  • 59
  • 87
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Mar 13 '13 at 18:02
  • Regardless of what approach you take, you'll probably need to get your system service to launch a subprocess in the user's session. Possibly useful API calls include WTSGetActiveConsoleSessionId, WTSQueryUserToken, and CreateProcessAsUser. (Theoretically another option is to use a device driver, but that's even more complicated.) – Harry Johnston Mar 15 '13 at 02:58

1 Answers1

2

I realized that you want only to lock your computer.

By using this code, you can lock your computer same as Windows Logo + L

[DllImport("user32")]
public static extern void LockWorkStation();

and about the time. it might be look like this.

DateTime d = DateTime.Now;
if (d.TimeOfDay.Hours >= youSettedTime)
{
  LockWorkStation();
}

Reference: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/7aab9893-430a-4aed-8d51-b8368a61860e/

spajce
  • 7,044
  • 5
  • 29
  • 44
  • Does this work from session 0? Surely it locks the workstation for the session it is running in, rather than the session the user is logged into? – Harry Johnston Mar 15 '13 at 02:54
  • I apologize but I did not test :), but if the OP mentioned about the `session` it might be another problem. – spajce Mar 15 '13 at 11:23