Require a combination Ctrl + L to exit app

* Require a combination Ctrl + L to exit app, while preventing any other access to the desktop
* Exits and calls Windows lock screen with Ctrl + L
* Uses low level keyboard hook to block system keys
This commit is contained in:
Andros Rosa 2017-08-16 20:48:44 -04:00 committed by Will Hilton
parent 5cc5255288
commit 52c538c37d
No known key found for this signature in database
GPG key ID: 9609B8A5928BA6B9

View file

@ -20,6 +20,9 @@ using System.Drawing;
using System.Collections;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;
using System.Collections.Generic;
namespace Screensavers
{
@ -99,7 +102,6 @@ namespace Screensavers
Application.DoEvents();
updateEvent.Reset();
}
}
void StopUpdating()
@ -593,7 +595,6 @@ namespace Screensavers
private void RunWindowed()
{
Form form = new Form();
form.FormBorderStyle = FormBorderStyle.FixedSingle;
form.Text = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
@ -620,10 +621,20 @@ namespace Screensavers
if (Window0 != null && Window0.Form != null)
Window0.Form.FormClosing += new FormClosingEventHandler(Form_FormClosing);
// enable keyboard hook
toggleKeyboardHook(true);
StartUpdating();
}
void Form_FormClosing(object sender, FormClosingEventArgs e)
{
if (!ctrlLPressed)
{
if (e.CloseReason == CloseReason.UserClosing)
e.Cancel = true;
}
else
{
StopUpdating();
LockWorkStation();
@ -631,6 +642,62 @@ namespace Screensavers
Exit(this, new EventArgs());
e.Cancel = false;
}
}
#region Low Level Keyboard Hook
private bool ctrlLPressed;
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _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)
{
int vkCode = Marshal.ReadInt32(lParam);
List<Keys> acceptedKeys = new List<Keys> { Keys.LControlKey, Keys.RControlKey, Keys.L };
if (acceptedKeys.Contains((Keys)vkCode))
return CallNextHookEx(_hookID, nCode, wParam, lParam);
else
return (IntPtr)1;
}
[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);
public void toggleKeyboardHook(bool enable)
{
if (enable)
_hookID = SetHook(_proc);
else
UnhookWindowsHookEx(_hookID);
}
#endregion
#region IDisposable Members
@ -651,17 +718,18 @@ namespace Screensavers
void OnKeyboardInput()
{
if (closeOnMouseMove)
{
// disable keyboard hook
toggleKeyboardHook(false);
if (Window0.Form != null)
Window0.Form.Close();
else
Application.Exit();
}
}
void OnMouseClick()
{
/*
if (closeOnMouseMove)
{
if (Window0.Form != null)
@ -671,6 +739,7 @@ namespace Screensavers
Application.Exit();
}
}
*/
}
/// <summary>
@ -784,22 +853,33 @@ namespace Screensavers
{
if (KeyPress != null)
KeyPress(this, e);
screensaver.OnKeyboardInput();
//screensaver.OnKeyboardInput();
}
void form_KeyUp(object sender, KeyEventArgs e)
{
if (KeyUp != null)
KeyUp(this, e);
if (e.Control && e.KeyCode == Keys.L)
{
screensaver.ctrlLPressed = true;
screensaver.OnKeyboardInput();
}
}
void form_KeyDown(object sender, KeyEventArgs e)
{
if (KeyDown != null)
KeyDown(this, e);
if (e.Control && e.KeyCode == Keys.L)
{
screensaver.ctrlLPressed = true;
screensaver.OnKeyboardInput();
}
}
void form_MouseWheel(object sender, MouseEventArgs e)
{