|
| 1 | +using Microsoft.Win32; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Drawing; |
| 6 | +using System.Drawing.Imaging; |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | +using System.Runtime.InteropServices; |
| 10 | +using System.Text; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using System.Windows; |
| 13 | +using System.Windows.Controls; |
| 14 | +using System.Windows.Data; |
| 15 | +using System.Windows.Documents; |
| 16 | +using System.Windows.Input; |
| 17 | +using System.Windows.Interop; |
| 18 | +using System.Windows.Media; |
| 19 | +using System.Windows.Media.Animation; |
| 20 | +using System.Windows.Media.Imaging; |
| 21 | +using System.Windows.Navigation; |
| 22 | +using System.Windows.Shapes; |
| 23 | + |
| 24 | +namespace MaterialKeyboardIndicator |
| 25 | +{ |
| 26 | + public static class BitmapExtension |
| 27 | + { |
| 28 | + public static BitmapImage ToBitmapImage(this Bitmap bitmap) |
| 29 | + { |
| 30 | + using (var memory = new MemoryStream()) |
| 31 | + { |
| 32 | + bitmap.Save(memory, ImageFormat.Png); |
| 33 | + memory.Position = 0; |
| 34 | + |
| 35 | + var bitmapImage = new BitmapImage(); |
| 36 | + bitmapImage.BeginInit(); |
| 37 | + bitmapImage.StreamSource = memory; |
| 38 | + bitmapImage.CacheOption = BitmapCacheOption.OnLoad; |
| 39 | + bitmapImage.EndInit(); |
| 40 | + bitmapImage.Freeze(); |
| 41 | + |
| 42 | + return bitmapImage; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public partial class MainWindow : Window |
| 48 | + { |
| 49 | + [DllImport("user32.dll")] |
| 50 | + private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk); |
| 51 | + |
| 52 | + [DllImport("user32.dll")] |
| 53 | + private static extern bool UnregisterHotKey(IntPtr hWnd, int id); |
| 54 | + |
| 55 | + private const int HOTKEY_ID = 9000; |
| 56 | + |
| 57 | + public const int WS_EX_TRANSPARENT = 0x00000020; |
| 58 | + private const int WS_EX_NOACTIVATE = 0x08000000; |
| 59 | + public const int GWL_EXSTYLE = (-20); |
| 60 | + |
| 61 | + [DllImport("user32.dll")] |
| 62 | + public static extern int GetWindowLong(IntPtr hwnd, int index); |
| 63 | + |
| 64 | + [DllImport("user32.dll")] |
| 65 | + public static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle); |
| 66 | + |
| 67 | + //Modifiers: |
| 68 | + private const uint MOD_NONE = 0x0000; //(none) |
| 69 | + private const uint MOD_ALT = 0x0001; //ALT |
| 70 | + private const uint MOD_CONTROL = 0x0002; //CTRL |
| 71 | + private const uint MOD_SHIFT = 0x0004; //SHIFT |
| 72 | + private const uint MOD_WIN = 0x0008; //WINDOWS |
| 73 | + //CAPS LOCK: |
| 74 | + private const uint VK_CAPITAL = 0x14; |
| 75 | + private const uint VK_NUMLOCK = 0x90; |
| 76 | + |
| 77 | + public MainWindow() |
| 78 | + { |
| 79 | + InitializeComponent(); |
| 80 | + |
| 81 | + SetStartup(); |
| 82 | + } |
| 83 | + |
| 84 | + private void SetStartup() |
| 85 | + { |
| 86 | + RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); |
| 87 | + rk.SetValue("MaterialKeyboardIndicator", Process.GetCurrentProcess().MainModule.FileName); |
| 88 | + Debug.WriteLine(Process.GetCurrentProcess().MainModule.FileName); |
| 89 | + } |
| 90 | + |
| 91 | + private IntPtr _windowHandle; |
| 92 | + private HwndSource _source; |
| 93 | + protected override void OnSourceInitialized(EventArgs e) |
| 94 | + { |
| 95 | + base.OnSourceInitialized(e); |
| 96 | + |
| 97 | + IntPtr hwnd = new WindowInteropHelper(this).Handle; |
| 98 | + int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE); |
| 99 | + SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT | WS_EX_NOACTIVATE); |
| 100 | + |
| 101 | + _windowHandle = new WindowInteropHelper(this).Handle; |
| 102 | + _source = HwndSource.FromHwnd(_windowHandle); |
| 103 | + _source.AddHook(HwndHook); |
| 104 | + |
| 105 | + RegisterHotKey(_windowHandle, HOTKEY_ID, MOD_NONE, VK_CAPITAL); //CAPS_LOCK |
| 106 | + RegisterHotKey(_windowHandle, HOTKEY_ID, MOD_NONE, VK_NUMLOCK); //NUM_LOCK |
| 107 | + |
| 108 | + var desktopWorkingArea = System.Windows.SystemParameters.WorkArea; |
| 109 | + this.Left = (desktopWorkingArea.Right / 2) - (this.Width / 2); |
| 110 | + this.Top = desktopWorkingArea.Bottom - this.Height; |
| 111 | + Keyboard.ClearFocus(); |
| 112 | + } |
| 113 | + |
| 114 | + public bool CanPlayAnim = true; |
| 115 | + public void TryPlayAnimation() |
| 116 | + { |
| 117 | + if (CanPlayAnim) |
| 118 | + { |
| 119 | + Storyboard sb = this.FindResource("Opening") as Storyboard; |
| 120 | + sb.Completed += this.Sb_Completed; |
| 121 | + sb.Begin(this); |
| 122 | + CanPlayAnim = false; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) |
| 127 | + { |
| 128 | + const int WM_HOTKEY = 0x0312; |
| 129 | + switch (msg) |
| 130 | + { |
| 131 | + case WM_HOTKEY: |
| 132 | + switch (wParam.ToInt32()) |
| 133 | + { |
| 134 | + case HOTKEY_ID: |
| 135 | + int vkey = (((int)lParam >> 16) & 0xFFFF); |
| 136 | + if (vkey == VK_CAPITAL) |
| 137 | + { |
| 138 | + TryPlayAnimation(); |
| 139 | + txtKey.Text = "CAPS LOCK"; |
| 140 | + txtState.Text = Keyboard.IsKeyToggled(Key.CapsLock) ? "ENABLED" : "DISABLED"; |
| 141 | + icon.Source = Keyboard.IsKeyToggled(Key.CapsLock) ? Properties.Resources.LockIcon.ToBitmapImage() : Properties.Resources.UnlockIcon.ToBitmapImage(); |
| 142 | + border.Background = Keyboard.IsKeyToggled(Key.CapsLock) ? new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF, 0x00, 0x87, 0x1D)) : new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF, 0xC7, 0, 0x2D)); |
| 143 | + } |
| 144 | + |
| 145 | + if (vkey == VK_NUMLOCK) |
| 146 | + { |
| 147 | + TryPlayAnimation(); |
| 148 | + txtKey.Text = "NUM LOCK"; |
| 149 | + Debug.WriteLine(Keyboard.IsKeyToggled(Key.NumLock)); |
| 150 | + txtState.Text = Keyboard.IsKeyToggled(Key.NumLock) ? "ENABLED" : "DISABLED"; |
| 151 | + icon.Source = Keyboard.IsKeyToggled(Key.NumLock) ? Properties.Resources.LockIcon.ToBitmapImage() : Properties.Resources.UnlockIcon.ToBitmapImage(); |
| 152 | + border.Background = Keyboard.IsKeyToggled(Key.NumLock) ? new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF, 0x00, 0x87, 0x1D)) : new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF, 0xC7, 0, 0x2D)); |
| 153 | + } |
| 154 | + |
| 155 | + handled = true; |
| 156 | + break; |
| 157 | + } |
| 158 | + break; |
| 159 | + } |
| 160 | + |
| 161 | + return IntPtr.Zero; |
| 162 | + } |
| 163 | + |
| 164 | + private void Sb_Completed(object sender, EventArgs e) |
| 165 | + { |
| 166 | + CanPlayAnim = true; |
| 167 | + } |
| 168 | + |
| 169 | + protected override void OnClosed(EventArgs e) |
| 170 | + { |
| 171 | + _source.RemoveHook(HwndHook); |
| 172 | + UnregisterHotKey(_windowHandle, HOTKEY_ID); |
| 173 | + base.OnClosed(e); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
| 177 | + |
0 commit comments