Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
BeezBeez committed Feb 6, 2020
1 parent ea7a56d commit feb13f9
Show file tree
Hide file tree
Showing 14 changed files with 711 additions and 0 deletions.
25 changes: 25 additions & 0 deletions MaterialKeyboardIndicator.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Blend for Visual Studio Version 16
VisualStudioVersion = 16.0.29020.237
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MaterialKeyboardIndicator", "MaterialKeyboardIndicator\MaterialKeyboardIndicator.csproj", "{60A023F9-4B12-4298-BD78-52578A8762CE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{60A023F9-4B12-4298-BD78-52578A8762CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{60A023F9-4B12-4298-BD78-52578A8762CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60A023F9-4B12-4298-BD78-52578A8762CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{60A023F9-4B12-4298-BD78-52578A8762CE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6F23E097-BDF5-41A8-822C-42B9917E1477}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions MaterialKeyboardIndicator/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
9 changes: 9 additions & 0 deletions MaterialKeyboardIndicator/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="MaterialKeyboardIndicator.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MaterialKeyboardIndicator"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
17 changes: 17 additions & 0 deletions MaterialKeyboardIndicator/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace MaterialKeyboardIndicator
{
/// <summary>
/// Logique d'interaction pour App.xaml
/// </summary>
public partial class App : Application
{
}
}
69 changes: 69 additions & 0 deletions MaterialKeyboardIndicator/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<Window x:Class="MaterialKeyboardIndicator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MaterialKeyboardIndicator"
mc:Ignorable="d"
Title="MainWindow" Focusable="False" Height="135" Width="470" WindowStyle="None" Background="{x:Null}" AllowsTransparency="True" Topmost="True" ShowInTaskbar="False">
<Window.Resources>
<Storyboard x:Key="Opening" x:Name="Opening">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="grid">
<EasingDoubleKeyFrame KeyTime="0" Value="0">
<EasingDoubleKeyFrame.EasingFunction>
<QuarticEase EasingMode="EaseInOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1">
<EasingDoubleKeyFrame.EasingFunction>
<QuarticEase EasingMode="EaseInOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="grid">
<EasingDoubleKeyFrame KeyTime="0" Value="0">
<EasingDoubleKeyFrame.EasingFunction>
<QuarticEase EasingMode="EaseInOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1">
<EasingDoubleKeyFrame.EasingFunction>
<QuarticEase EasingMode="EaseInOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid">
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:1.5" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Grid x:Name="grid" RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="0" ScaleY="0"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<Border x:Name="border" BorderThickness="0" Background="#FF005AC7" Margin="16" CornerRadius="8">
<Border.Effect>
<DropShadowEffect BlurRadius="12" Direction="-90" ShadowDepth="4" RenderingBias="Quality" Opacity="0.35"/>
</Border.Effect>
<Grid Margin="0,0,0,7">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="96"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1" Margin="0,0,0,8" VerticalAlignment="Center" HorizontalAlignment="Left">
<TextBlock x:Name="txtKey" TextWrapping="Wrap" Text="CAPS LOCK" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="White" FontSize="24" Padding="0" FontFamily="Segoe UI Light"/>
<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"/>
</Grid>
<Image x:Name="icon" Margin="16" Source="Resources/icnLockIcon.png"/>
</Grid>
</Border>

</Grid>
</Window>
177 changes: 177 additions & 0 deletions MaterialKeyboardIndicator/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MaterialKeyboardIndicator
{
public static class BitmapExtension
{
public static BitmapImage ToBitmapImage(this Bitmap bitmap)
{
using (var memory = new MemoryStream())
{
bitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;

var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();

return bitmapImage;
}
}
}

public partial class MainWindow : Window
{
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

private const int HOTKEY_ID = 9000;

public const int WS_EX_TRANSPARENT = 0x00000020;
private const int WS_EX_NOACTIVATE = 0x08000000;
public const int GWL_EXSTYLE = (-20);

[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr hwnd, int index);

[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);

//Modifiers:
private const uint MOD_NONE = 0x0000; //(none)
private const uint MOD_ALT = 0x0001; //ALT
private const uint MOD_CONTROL = 0x0002; //CTRL
private const uint MOD_SHIFT = 0x0004; //SHIFT
private const uint MOD_WIN = 0x0008; //WINDOWS
//CAPS LOCK:
private const uint VK_CAPITAL = 0x14;
private const uint VK_NUMLOCK = 0x90;

public MainWindow()
{
InitializeComponent();

SetStartup();
}

private void SetStartup()
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rk.SetValue("MaterialKeyboardIndicator", Process.GetCurrentProcess().MainModule.FileName);
Debug.WriteLine(Process.GetCurrentProcess().MainModule.FileName);
}

private IntPtr _windowHandle;
private HwndSource _source;
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);

IntPtr hwnd = new WindowInteropHelper(this).Handle;
int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT | WS_EX_NOACTIVATE);

_windowHandle = new WindowInteropHelper(this).Handle;
_source = HwndSource.FromHwnd(_windowHandle);
_source.AddHook(HwndHook);

RegisterHotKey(_windowHandle, HOTKEY_ID, MOD_NONE, VK_CAPITAL); //CAPS_LOCK
RegisterHotKey(_windowHandle, HOTKEY_ID, MOD_NONE, VK_NUMLOCK); //NUM_LOCK

var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;
this.Left = (desktopWorkingArea.Right / 2) - (this.Width / 2);
this.Top = desktopWorkingArea.Bottom - this.Height;
Keyboard.ClearFocus();
}

public bool CanPlayAnim = true;
public void TryPlayAnimation()
{
if (CanPlayAnim)
{
Storyboard sb = this.FindResource("Opening") as Storyboard;
sb.Completed += this.Sb_Completed;
sb.Begin(this);
CanPlayAnim = false;
}
}

private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
const int WM_HOTKEY = 0x0312;
switch (msg)
{
case WM_HOTKEY:
switch (wParam.ToInt32())
{
case HOTKEY_ID:
int vkey = (((int)lParam >> 16) & 0xFFFF);
if (vkey == VK_CAPITAL)
{
TryPlayAnimation();
txtKey.Text = "CAPS LOCK";
txtState.Text = Keyboard.IsKeyToggled(Key.CapsLock) ? "ENABLED" : "DISABLED";
icon.Source = Keyboard.IsKeyToggled(Key.CapsLock) ? Properties.Resources.LockIcon.ToBitmapImage() : Properties.Resources.UnlockIcon.ToBitmapImage();
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));
}

if (vkey == VK_NUMLOCK)
{
TryPlayAnimation();
txtKey.Text = "NUM LOCK";
Debug.WriteLine(Keyboard.IsKeyToggled(Key.NumLock));
txtState.Text = Keyboard.IsKeyToggled(Key.NumLock) ? "ENABLED" : "DISABLED";
icon.Source = Keyboard.IsKeyToggled(Key.NumLock) ? Properties.Resources.LockIcon.ToBitmapImage() : Properties.Resources.UnlockIcon.ToBitmapImage();
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));
}

handled = true;
break;
}
break;
}

return IntPtr.Zero;
}

private void Sb_Completed(object sender, EventArgs e)
{
CanPlayAnim = true;
}

protected override void OnClosed(EventArgs e)
{
_source.RemoveHook(HwndHook);
UnregisterHotKey(_windowHandle, HOTKEY_ID);
base.OnClosed(e);
}
}
}

Loading

0 comments on commit feb13f9

Please sign in to comment.