Skip to content

Instantly share code, notes, and snippets.

@erkantaylan
Created November 28, 2018 14:50
Show Gist options
  • Save erkantaylan/bf781be431cb3a00c5df176ce1058f08 to your computer and use it in GitHub Desktop.
Save erkantaylan/bf781be431cb3a00c5df176ce1058f08 to your computer and use it in GitHub Desktop.
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Input;
using MahApps.Metro.Controls;
namespace ChangeThis
{
public class AutohotkeyToMahappsHotKeyConverter : IValueConverter
{
public object Convert(object value, Type targetType = null, object parameter = null, CultureInfo culture = null)
{
if (!(value is string shortcut))
{
return null;
}
bool shift = shortcut.Contains("+");
bool ctrl = shortcut.Contains("^");
bool alt = shortcut.Contains("!");
bool windows = shortcut.Contains("#");
ModifierKeys modifierKeys = ModifierKeys.None;
if (shift)
{
modifierKeys = modifierKeys | ModifierKeys.Shift;
}
if (alt)
{
modifierKeys = modifierKeys | ModifierKeys.Alt;
}
if (ctrl)
{
modifierKeys = modifierKeys | ModifierKeys.Control;
}
if (windows)
{
modifierKeys = modifierKeys | ModifierKeys.Windows;
}
string keyString = shortcut.Trim('+', '!', '^', '#');
keyString = int.TryParse(keyString, out int _) ? $"D{keyString}" : keyString; // if string is number, add D to beginning to correct enum conversion
Key key = (Key) Enum.Parse(typeof(Key), keyString, true);
return new HotKey(key, modifierKeys);
}
public object ConvertBack(object value, Type targetType = null, object parameter = null, CultureInfo culture = null)
{
if (!(value is HotKey hotkey))
{
return null;
}
string ahkKey = "";
if (hotkey.ModifierKeys.Has(ModifierKeys.Control))
{
ahkKey += "^";
}
if (hotkey.ModifierKeys.Has(ModifierKeys.Shift))
{
ahkKey += "+";
}
if (hotkey.ModifierKeys.Has(ModifierKeys.Alt))
{
ahkKey += "!";
}
if (hotkey.ModifierKeys.Has(ModifierKeys.Windows))
{
ahkKey += "#";
}
/*if key is number, it happens like this D0 D1 D2... and we don`t need the 'D'*/
if (hotkey.Key >= Key.D0
&& hotkey.Key <= Key.D9)
{
ahkKey += hotkey.Key.ToString()[1];
}
/*if it is a function key*/
else if (hotkey.Key >= Key.F1
&& hotkey.Key <= Key.F12)
{
ahkKey += hotkey.Key.ToString();
}
else if (hotkey.Key >= Key.A && hotkey.Key <= Key.Z)
{
ahkKey += hotkey.Key.ToString();
}
return ahkKey;
}
}
}
public static class EnumerationExtensions
{
public static bool Has<T>(this Enum type, T value)
{
try
{
return ((int) (object) type & (int) (object) value) == (int) (object) value;
}
catch
{
return false;
}
}
public static bool Is<T>(this Enum type, T value)
{
try
{
return (int) (object) type == (int) (object) value;
}
catch
{
return false;
}
}
public static T Add<T>(this Enum type, T value)
{
try
{
return (T) (object) ((int) (object) type | (int) (object) value);
}
catch (Exception ex)
{
throw new ArgumentException($"Could not append value from enumerated type '{typeof(T).Name}'.", ex);
}
}
public static T Remove<T>(this Enum type, T value)
{
try
{
return (T) (object) ((int) (object) type & ~(int) (object) value);
}
catch (Exception ex)
{
throw new ArgumentException($"Could not remove value from enumerated type '{typeof(T).Name}'.", ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment