Skip to content

Commit

Permalink
Added keyboard shortcuts section in Settings (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-Peyronnet committed Apr 17, 2022
1 parent 002e451 commit 195d97c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 9 deletions.
5 changes: 5 additions & 0 deletions ColorPicker/Classes/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public static string GetHiSentence
}
}

/// <summary>
/// If the user is trying to new keyboard shortcuts, the value should be false.
/// </summary>
public static bool KeyBoardShortcutsAvailable { get; set; }

/// <summary>
/// <c>ToString()</c> method for <see cref="ColorTypes"/> enum.
/// </summary>
Expand Down
6 changes: 4 additions & 2 deletions ColorPicker/Pages/PickerPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public PickerPage()
miniPicker.Left = Env.GetMouseCursorPositionWPF().X / factor; // Define position
miniPicker.Top = Env.GetMouseCursorPositionWPF().Y / factor + 5; // Define position
};

Global.KeyBoardShortcutsAvailable = true;
}

private void InitUI()
Expand Down Expand Up @@ -147,7 +149,7 @@ private void InitUI()

private void HandleSelectKeyboard()
{
if (Global.Settings.EnableKeyBoardShortcuts.Value)
if (Global.Settings.EnableKeyBoardShortcuts.Value && Global.KeyBoardShortcutsAvailable)
{
if (isRunning)
{
Expand All @@ -170,7 +172,7 @@ private void HandleSelectKeyboard()

private void HandleCopyKeyboard()
{
if (Global.Settings.EnableKeyBoardShortcuts.Value)
if (Global.Settings.EnableKeyBoardShortcuts.Value && Global.KeyBoardShortcutsAvailable)
{
if (isRunning)
{
Expand Down
39 changes: 32 additions & 7 deletions ColorPicker/Pages/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@

<CheckBox Margin="0 5 0 2" x:Name="HEXUseUpperCaseChk" Style="{DynamicResource CheckBoxStyle1}" Content="{x:Static lang:Resources.UseUpperCaseHEX}" BorderThickness="3" Foreground="{Binding Source={StaticResource Foreground1}}" FontSize="14" VerticalContentAlignment="Center" Unchecked="HEXUseUpperCaseChk_Checked" Checked="HEXUseUpperCaseChk_Checked"/>

<StackPanel Orientation="Horizontal">
<TextBlock Text="{x:Static lang:Resources.DefaultColor}" Foreground="{Binding Source={StaticResource Foreground1}}" VerticalAlignment="Center"/>
<ComboBox SelectionChanged="FavoriteColorComboBox_SelectionChanged" BorderThickness="2" Padding="5" Style="{DynamicResource ComboBoxStyle1}" x:Name="FavoriteColorComboBox" Background="Transparent" BorderBrush="{Binding Source={StaticResource AccentColor}}" Foreground="{Binding Source={StaticResource Foreground1}}" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Center"/>
</StackPanel>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{x:Static lang:Resources.DefaultColor}" Foreground="{Binding Source={StaticResource Foreground1}}" VerticalAlignment="Center"/>
<ComboBox SelectionChanged="FavoriteColorComboBox_SelectionChanged" BorderThickness="2" Padding="5" Style="{DynamicResource ComboBoxStyle1}" x:Name="FavoriteColorComboBox" Background="Transparent" BorderBrush="{Binding Source={StaticResource AccentColor}}" Foreground="{Binding Source={StaticResource Foreground1}}" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Center"/>
</StackPanel>
</StackPanel>
</Expander>

<Expander Style="{DynamicResource ExpanderStyle1}" Margin="10,10,10,0" Background="{Binding Source={StaticResource Background2}}" Foreground="{Binding Source={StaticResource Foreground1}}">
Expand All @@ -165,8 +165,33 @@
</StackPanel>
</StackPanel>
</Expander.Header>
<CheckBox Margin="10" x:Name="UseKeyboardShortcutsChk" Style="{DynamicResource CheckBoxStyle1}" Content="{x:Static lang:Resources.UseKeyboardShortcuts}" BorderThickness="3" Foreground="{Binding Source={StaticResource Foreground1}}" FontSize="14" VerticalContentAlignment="Center" Unchecked="UseKeyboardShortcutsChk_Checked" Checked="UseKeyboardShortcutsChk_Checked"/>

<StackPanel>
<CheckBox Margin="10" x:Name="UseKeyboardShortcutsChk" Style="{DynamicResource CheckBoxStyle1}" Content="{x:Static lang:Resources.UseKeyboardShortcuts}" BorderThickness="3" Foreground="{Binding Source={StaticResource Foreground1}}" FontSize="14" VerticalContentAlignment="Center" Unchecked="UseKeyboardShortcutsChk_Checked" Checked="UseKeyboardShortcutsChk_Checked"/>
<TextBlock Text="{x:Static lang:Resources.StartSelection}" Margin="10 0 0 0"/>
<StackPanel Orientation="Horizontal">
<Border Background="{Binding Source={StaticResource Gray}}" Padding="5" Margin="10 5 0 5" CornerRadius="5" HorizontalAlignment="Left">
<TextBlock x:Name="SelectShortcutTxt" Text="Shift+S" FontFamily="Consolas"/>
</Border>
<Button Content="&#xF3DE;" Padding="5" x:Name="EditSelectShortcutBtn" Click="EditSelectShortcutBtn_Click" FontFamily="..\Fonts\#FluentSystemIcons-Regular" Style="{DynamicResource TabButtonStyle}" Background="{Binding Source={StaticResource AccentColor}}" Foreground="{Binding Source={StaticResource WindowButtonsHoverForeground1}}" FontSize="14" Margin="5" VerticalAlignment="Center">
<Button.ToolTip>
<ToolTip Content="{x:Static lang:Resources.NeedRestartToApplyChanges}" Foreground="{Binding Source={StaticResource Foreground1}}" Background="{Binding Source={StaticResource Background1}}"/>
</Button.ToolTip>
</Button>
<TextBlock x:Name="PressKeys1Txt" Text="{x:Static lang:Resources.PressKeys}" Visibility="Collapsed" VerticalAlignment="Center"/>
</StackPanel>
<TextBlock Text="{x:Static lang:Resources.CopyColor}" Margin="10 0 0 0"/>
<StackPanel Orientation="Horizontal">
<Border Background="{Binding Source={StaticResource Gray}}" Padding="5" Margin="10 5 0 5" CornerRadius="5" HorizontalAlignment="Left">
<TextBlock x:Name="CopyShortcutTxt" Text="Shift+S" FontFamily="Consolas"/>
</Border>
<Button Content="&#xF3DE;" Padding="5" x:Name="EditCopyShortcutBtn" Click="EditCopyShortcutBtn_Click" FontFamily="..\Fonts\#FluentSystemIcons-Regular" Style="{DynamicResource TabButtonStyle}" Background="{Binding Source={StaticResource AccentColor}}" Foreground="{Binding Source={StaticResource WindowButtonsHoverForeground1}}" FontSize="14" Margin="5" VerticalAlignment="Center">
<Button.ToolTip>
<ToolTip Content="{x:Static lang:Resources.NeedRestartToApplyChanges}" Foreground="{Binding Source={StaticResource Foreground1}}" Background="{Binding Source={StaticResource Background1}}"/>
</Button.ToolTip>
</Button>
<TextBlock x:Name="PressKeys2Txt" Text="{x:Static lang:Resources.PressKeys}" Visibility="Collapsed" VerticalAlignment="Center"/>
</StackPanel>
</StackPanel>
</Expander>

<Expander Style="{DynamicResource ExpanderStyle1}" Margin="10,10,10,0" Background="{Binding Source={StaticResource Background2}}" Foreground="{Binding Source={StaticResource Foreground1}}">
Expand Down
65 changes: 65 additions & 0 deletions ColorPicker/Pages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ MIT License
SOFTWARE.
*/
using ColorPicker.Classes;
using Gma.System.MouseKeyHook;
using LeoCorpLibrary;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows;
Expand All @@ -41,6 +43,7 @@ public partial class SettingsPage : Page
{
bool isAvailable;
readonly System.Windows.Forms.NotifyIcon notifyIcon = new();
private IKeyboardMouseEvents GlobalHook;
public SettingsPage()
{
InitializeComponent();
Expand All @@ -55,9 +58,30 @@ public SettingsPage()
Environment.Exit(0); // Close
}
};
GlobalHook = Hook.GlobalEvents();

InitUI(); // Load the UI
}

List<string> keys = new();
private void GlobalHook_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (keys.Contains(e.KeyCode.ToString()))
{
return; // Stop
}

keys.Add(e.KeyCode.ToString()); // Add key to list
if (selectShortcutInput)
{
SelectShortcutTxt.Text += (SelectShortcutTxt.Text.Length == 0) ? e.KeyCode.ToString() : $"+{e.KeyCode}";
}
else
{
CopyShortcutTxt.Text += (CopyShortcutTxt.Text.Length == 0) ? e.KeyCode.ToString() : $"+{e.KeyCode}";
}
}

private async void InitUI()
{
try
Expand Down Expand Up @@ -472,5 +496,46 @@ private void FavoriteColorComboBox_SelectionChanged(object sender, SelectionChan
Global.Settings.FavoriteColorType = (Enums.ColorTypes)FavoriteColorComboBox.SelectedIndex; // Set
SettingsManager.Save(); // Save changes
}

bool copyShortcutInput, selectShortcutInput = false;
private void EditCopyShortcutBtn_Click(object sender, RoutedEventArgs e)
{
keys = new(); // Create new list
copyShortcutInput = !copyShortcutInput; // Toggle
selectShortcutInput = false; // Set
PressKeys2Txt.Visibility = copyShortcutInput ? Visibility.Visible : Visibility.Collapsed; // Show/Hide

if (copyShortcutInput)
{
GlobalHook.KeyDown += GlobalHook_KeyDown; // Subscribe
CopyShortcutTxt.Text = "";
Global.KeyBoardShortcutsAvailable = false; // Set
}
else
{
GlobalHook.KeyDown -= GlobalHook_KeyDown; // Unsubscribe
Global.KeyBoardShortcutsAvailable = true; // Set
}
}

private void EditSelectShortcutBtn_Click(object sender, RoutedEventArgs e)
{
keys = new(); // Create new list
selectShortcutInput = !selectShortcutInput; // Toggle
copyShortcutInput = false; // Set
PressKeys1Txt.Visibility = selectShortcutInput ? Visibility.Visible : Visibility.Collapsed; // Show/Hide

if (selectShortcutInput)
{
GlobalHook.KeyDown += GlobalHook_KeyDown; // Subscribe
SelectShortcutTxt.Text = "";
Global.KeyBoardShortcutsAvailable = false; // Set
}
else
{
GlobalHook.KeyDown -= GlobalHook_KeyDown; // Unsubscribe
Global.KeyBoardShortcutsAvailable = true; // Set
}
}
}
}

0 comments on commit 195d97c

Please sign in to comment.