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#?
Asked
Active
Viewed 178 times
1
-
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 Answers
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();
}

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