I apologize for grave digging, but this page comes as a top search result for triggering a screensaver with a shortcut key. I thought it'd be useful to add another, updated method for doing something that few people seem to know how to do.
Note: This is a solution made for and tested on Windows 8 but should also work for Windows 7. There is a better solution for older OSes, however; check here: https://stackoverflow.com/questions/1430108/how-to-turn-screensaver-on-windows-7-by-a-code-in-cmd.
First, create a folder. Then, find a cmd.exe and copy it to the folder. Now, create a text file (don't worry about naming it yet) and paste the following code into it:
using System;
using System.Runtime.InteropServices;
public static class LockDesktop
{
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
private const int SC_SCREENSAVE = 0xF140;
private const int WM_SYSCOMMAND = 0x0112;
public static void SetScreenSaverRunning()
{
SendMessage(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0);
}
public static void Main()
{
LockDesktop.SetScreenSaverRunning();
}
}
Now, click "save as" and set the file type to "all" before saving the file as "lock.cs". Make sure to save it to the folder you created. Finally, open the CMD.exe from the folder and type in the following command:
%SystemRoot%\Microsoft.NET\Framework\v3.5\csc.exe lock.cs
Press Enter and wait a moment. You'll now find a "lock.exe" file in your folder. You can create a shortcut (send to desktop) to it and go to properties to assign a keyboard shortcut.
**Note: This will simply launch your set screensaver as if the timer ran out. If you do not have it set to display logon screen at resume, it will not lock. There is also a delay of about five seconds before it locks since this is the default behavior of screensaver so that the user has time to cancel it from locking.
If you do not want to set your screen saver to lock on resume and/or want you computer to lock instantly with the shortcut, continue reading.**
Create another text file or use the old one, it doesn't matter. Now, enter the following batch code and save it as "lock.bat".
@START lock.exe
@%windir%\system32\rundll32.exe user32.dll,LockWorkStation
Now you can create a shortcut once again and give it a keyboard command.
The answer you accepted is for a question you didn't ask clearly, IMO. This question should read How do I lock my windows PC with one key? – Fuhrmanator – 2015-05-17T00:08:38.923
Related: How to lock Windows 7 session with just key strokes?
– G-Man Says 'Reinstate Monica' – 2015-08-03T00:51:30.2732People still use screen-savers? – Marc Gravell – 2011-10-08T08:20:25.123
@MarcGravell: Why not? – dmr – 2011-10-09T02:01:43.663
@dmr I (and many others) just set the monitor(s) to power down if not being used – Marc Gravell – 2011-10-09T07:02:00.990
2@MarcGravell Some monitors take time to switch back from being powered down to an active state. Mine even switches the input signal, something I don't want, which is why I need a screen-saver. – slhck – 2011-10-10T06:07:18.313
@slhck fair enough! – Marc Gravell – 2011-10-10T06:23:52.730
Some monitors take time to switch back from being powered down to an active state.
How much time? ಠ_ఠ My old CRT has to click a relay to turn on and off, but it’s still worth the extra 2-3 seconds to save 70W of electricity (which we pay for). My old LCD recovers from standby in 1-2 seconds and uses 31W; also worth the “wait”.Mine even switches the input signal, something I don't want, which is why I need a screen-saver.
Can you set it to not do that? Do you at least use the default blank screensaver or a fancy one (3D ones suck up CPU cycles which wastes even more electricity). – Synetech – 2013-09-10T17:20:47.790What's the fastest way to trigger your screensaver?
Hmm, I could have sworn I’ve seen multimedia keyboards with screensaver keys, but I can’t find a single photo of one on Google Images… – Synetech – 2013-09-10T17:22:02.060