mirror of
https://github.com/wmhilton/lock-screen.git
synced 2024-09-30 00:57:48 +13:00
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:
parent
5cc5255288
commit
52c538c37d
1 changed files with 117 additions and 37 deletions
|
@ -20,6 +20,9 @@ using System.Drawing;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Screensavers
|
namespace Screensavers
|
||||||
{
|
{
|
||||||
|
@ -99,7 +102,6 @@ namespace Screensavers
|
||||||
Application.DoEvents();
|
Application.DoEvents();
|
||||||
updateEvent.Reset();
|
updateEvent.Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void StopUpdating()
|
void StopUpdating()
|
||||||
|
@ -593,7 +595,6 @@ namespace Screensavers
|
||||||
|
|
||||||
private void RunWindowed()
|
private void RunWindowed()
|
||||||
{
|
{
|
||||||
|
|
||||||
Form form = new Form();
|
Form form = new Form();
|
||||||
form.FormBorderStyle = FormBorderStyle.FixedSingle;
|
form.FormBorderStyle = FormBorderStyle.FixedSingle;
|
||||||
form.Text = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
|
form.Text = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
|
||||||
|
@ -620,10 +621,20 @@ namespace Screensavers
|
||||||
if (Window0 != null && Window0.Form != null)
|
if (Window0 != null && Window0.Form != null)
|
||||||
Window0.Form.FormClosing += new FormClosingEventHandler(Form_FormClosing);
|
Window0.Form.FormClosing += new FormClosingEventHandler(Form_FormClosing);
|
||||||
|
|
||||||
|
// enable keyboard hook
|
||||||
|
toggleKeyboardHook(true);
|
||||||
|
|
||||||
StartUpdating();
|
StartUpdating();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Form_FormClosing(object sender, FormClosingEventArgs e)
|
void Form_FormClosing(object sender, FormClosingEventArgs e)
|
||||||
|
{
|
||||||
|
if (!ctrlLPressed)
|
||||||
|
{
|
||||||
|
if (e.CloseReason == CloseReason.UserClosing)
|
||||||
|
e.Cancel = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
StopUpdating();
|
StopUpdating();
|
||||||
LockWorkStation();
|
LockWorkStation();
|
||||||
|
@ -631,6 +642,62 @@ namespace Screensavers
|
||||||
Exit(this, new EventArgs());
|
Exit(this, new EventArgs());
|
||||||
e.Cancel = false;
|
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
|
#region IDisposable Members
|
||||||
|
|
||||||
|
@ -651,17 +718,18 @@ namespace Screensavers
|
||||||
|
|
||||||
void OnKeyboardInput()
|
void OnKeyboardInput()
|
||||||
{
|
{
|
||||||
if (closeOnMouseMove)
|
// disable keyboard hook
|
||||||
{
|
toggleKeyboardHook(false);
|
||||||
|
|
||||||
if (Window0.Form != null)
|
if (Window0.Form != null)
|
||||||
Window0.Form.Close();
|
Window0.Form.Close();
|
||||||
else
|
else
|
||||||
Application.Exit();
|
Application.Exit();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void OnMouseClick()
|
void OnMouseClick()
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
if (closeOnMouseMove)
|
if (closeOnMouseMove)
|
||||||
{
|
{
|
||||||
if (Window0.Form != null)
|
if (Window0.Form != null)
|
||||||
|
@ -671,6 +739,7 @@ namespace Screensavers
|
||||||
Application.Exit();
|
Application.Exit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -784,22 +853,33 @@ namespace Screensavers
|
||||||
{
|
{
|
||||||
if (KeyPress != null)
|
if (KeyPress != null)
|
||||||
KeyPress(this, e);
|
KeyPress(this, e);
|
||||||
screensaver.OnKeyboardInput();
|
|
||||||
|
//screensaver.OnKeyboardInput();
|
||||||
}
|
}
|
||||||
|
|
||||||
void form_KeyUp(object sender, KeyEventArgs e)
|
void form_KeyUp(object sender, KeyEventArgs e)
|
||||||
{
|
{
|
||||||
if (KeyUp != null)
|
if (KeyUp != null)
|
||||||
KeyUp(this, e);
|
KeyUp(this, e);
|
||||||
|
|
||||||
|
if (e.Control && e.KeyCode == Keys.L)
|
||||||
|
{
|
||||||
|
screensaver.ctrlLPressed = true;
|
||||||
screensaver.OnKeyboardInput();
|
screensaver.OnKeyboardInput();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void form_KeyDown(object sender, KeyEventArgs e)
|
void form_KeyDown(object sender, KeyEventArgs e)
|
||||||
{
|
{
|
||||||
if (KeyDown != null)
|
if (KeyDown != null)
|
||||||
KeyDown(this, e);
|
KeyDown(this, e);
|
||||||
|
|
||||||
|
if (e.Control && e.KeyCode == Keys.L)
|
||||||
|
{
|
||||||
|
screensaver.ctrlLPressed = true;
|
||||||
screensaver.OnKeyboardInput();
|
screensaver.OnKeyboardInput();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void form_MouseWheel(object sender, MouseEventArgs e)
|
void form_MouseWheel(object sender, MouseEventArgs e)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue