-
Notifications
You must be signed in to change notification settings - Fork 94
/
App.xaml.cs
111 lines (97 loc) · 3.95 KB
/
App.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using Application = System.Windows.Application;
using WinForms = System.Windows.Forms;
namespace BabySmash
{
public partial class App : Application
{
private static readonly InterceptKeys.LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
private void Application_Startup(object sender, StartupEventArgs e)
{
Controller.Instance.Launch();
}
public App()
{
ShutdownMode = ShutdownMode.OnExplicitShutdown;
Application.Current.Exit += new ExitEventHandler(Current_Exit);
try
{
_hookID = InterceptKeys.SetHook(_proc);
}
catch
{
DetachKeyboardHook();
}
}
void Current_Exit(object sender, ExitEventArgs e)
{
DetachKeyboardHook();
}
/// <summary>
/// Detach the keyboard hook; call during shutdown to prevent calls as we unload
/// </summary>
private static void DetachKeyboardHook()
{
if (_hookID != IntPtr.Zero)
InterceptKeys.UnhookWindowsHookEx(_hookID);
}
public static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
bool alt = (WinForms.Control.ModifierKeys & Keys.Alt) != 0;
bool control = (WinForms.Control.ModifierKeys & Keys.Control) != 0;
int vkCode = Marshal.ReadInt32(lParam);
Keys key = (Keys)vkCode;
if (alt && key == Keys.F4)
{
Application.Current.Shutdown();
return (IntPtr)1; // Handled.
}
if (!AllowKeyboardInput(alt, control, key))
{
return (IntPtr)1; // Handled.
}
}
return InterceptKeys.CallNextHookEx(_hookID, nCode, wParam, lParam);
}
/// <summary>Determines whether the specified keyboard input should be allowed to be processed by the system.</summary>
/// <remarks>Helps block unwanted keys and key combinations that could exit the app, make system changes, etc.</remarks>
public static bool AllowKeyboardInput(bool alt, bool control, Keys key)
{
// Disallow various special keys.
if (key <= Keys.Back || key == Keys.None ||
key == Keys.Menu || key == Keys.Pause ||
key == Keys.Help)
{
return false;
}
// Disallow ranges of special keys.
// Currently leaves volume controls enabled; consider if this makes sense.
// Disables non-existing Keys up to 65534, to err on the side of caution for future keyboard expansion.
if ((key >= Keys.LWin && key <= Keys.Sleep) ||
(key >= Keys.KanaMode && key <= Keys.HanjaMode) ||
(key >= Keys.IMEConvert && key <= Keys.IMEModeChange) ||
(key >= Keys.BrowserBack && key <= Keys.BrowserHome) ||
(key >= Keys.MediaNextTrack && key <= Keys.LaunchApplication2) ||
(key >= Keys.ProcessKey && key <= (Keys)65534))
{
return false;
}
// Disallow specific key combinations. (These component keys would be OK on their own.)
if ((alt && key == Keys.Tab) ||
(alt && key == Keys.Space) ||
(control && key == Keys.Escape))
{
return false;
}
// Allow anything else (like letters, numbers, spacebar, braces, and so on).
return true;
}
}
}