Skip to content

Commit feb13f9

Browse files
committed
First version
1 parent ea7a56d commit feb13f9

14 files changed

+711
-0
lines changed

MaterialKeyboardIndicator.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Blend for Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29020.237
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MaterialKeyboardIndicator", "MaterialKeyboardIndicator\MaterialKeyboardIndicator.csproj", "{60A023F9-4B12-4298-BD78-52578A8762CE}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{60A023F9-4B12-4298-BD78-52578A8762CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{60A023F9-4B12-4298-BD78-52578A8762CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{60A023F9-4B12-4298-BD78-52578A8762CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{60A023F9-4B12-4298-BD78-52578A8762CE}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {6F23E097-BDF5-41A8-822C-42B9917E1477}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
5+
</startup>
6+
</configuration>

MaterialKeyboardIndicator/App.xaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Application x:Class="MaterialKeyboardIndicator.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:MaterialKeyboardIndicator"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</Application>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Configuration;
4+
using System.Data;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
9+
namespace MaterialKeyboardIndicator
10+
{
11+
/// <summary>
12+
/// Logique d'interaction pour App.xaml
13+
/// </summary>
14+
public partial class App : Application
15+
{
16+
}
17+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<Window x:Class="MaterialKeyboardIndicator.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:MaterialKeyboardIndicator"
7+
mc:Ignorable="d"
8+
Title="MainWindow" Focusable="False" Height="135" Width="470" WindowStyle="None" Background="{x:Null}" AllowsTransparency="True" Topmost="True" ShowInTaskbar="False">
9+
<Window.Resources>
10+
<Storyboard x:Key="Opening" x:Name="Opening">
11+
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="grid">
12+
<EasingDoubleKeyFrame KeyTime="0" Value="0">
13+
<EasingDoubleKeyFrame.EasingFunction>
14+
<QuarticEase EasingMode="EaseInOut"/>
15+
</EasingDoubleKeyFrame.EasingFunction>
16+
</EasingDoubleKeyFrame>
17+
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1">
18+
<EasingDoubleKeyFrame.EasingFunction>
19+
<QuarticEase EasingMode="EaseInOut"/>
20+
</EasingDoubleKeyFrame.EasingFunction>
21+
</EasingDoubleKeyFrame>
22+
</DoubleAnimationUsingKeyFrames>
23+
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="grid">
24+
<EasingDoubleKeyFrame KeyTime="0" Value="0">
25+
<EasingDoubleKeyFrame.EasingFunction>
26+
<QuarticEase EasingMode="EaseInOut"/>
27+
</EasingDoubleKeyFrame.EasingFunction>
28+
</EasingDoubleKeyFrame>
29+
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1">
30+
<EasingDoubleKeyFrame.EasingFunction>
31+
<QuarticEase EasingMode="EaseInOut"/>
32+
</EasingDoubleKeyFrame.EasingFunction>
33+
</EasingDoubleKeyFrame>
34+
</DoubleAnimationUsingKeyFrames>
35+
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid">
36+
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
37+
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
38+
<EasingDoubleKeyFrame KeyTime="0:0:1.5" Value="0"/>
39+
</DoubleAnimationUsingKeyFrames>
40+
</Storyboard>
41+
</Window.Resources>
42+
<Grid x:Name="grid" RenderTransformOrigin="0.5,0.5">
43+
<Grid.RenderTransform>
44+
<TransformGroup>
45+
<ScaleTransform ScaleX="0" ScaleY="0"/>
46+
<SkewTransform/>
47+
<RotateTransform/>
48+
<TranslateTransform/>
49+
</TransformGroup>
50+
</Grid.RenderTransform>
51+
<Border x:Name="border" BorderThickness="0" Background="#FF005AC7" Margin="16" CornerRadius="8">
52+
<Border.Effect>
53+
<DropShadowEffect BlurRadius="12" Direction="-90" ShadowDepth="4" RenderingBias="Quality" Opacity="0.35"/>
54+
</Border.Effect>
55+
<Grid Margin="0,0,0,7">
56+
<Grid.ColumnDefinitions>
57+
<ColumnDefinition Width="96"/>
58+
<ColumnDefinition/>
59+
</Grid.ColumnDefinitions>
60+
<Grid Grid.Column="1" Margin="0,0,0,8" VerticalAlignment="Center" HorizontalAlignment="Left">
61+
<TextBlock x:Name="txtKey" TextWrapping="Wrap" Text="CAPS LOCK" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="White" FontSize="24" Padding="0" FontFamily="Segoe UI Light"/>
62+
<TextBlock x:Name="txtState" Margin="0,32,0,0" TextWrapping="Wrap" Text="DISABLED" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="White" FontSize="16" Padding="0" FontFamily="Segoe UI Light"/>
63+
</Grid>
64+
<Image x:Name="icon" Margin="16" Source="Resources/icnLockIcon.png"/>
65+
</Grid>
66+
</Border>
67+
68+
</Grid>
69+
</Window>
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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

Comments
 (0)