84 lines
1.9 KiB
C#
84 lines
1.9 KiB
C#
using System.ComponentModel;
|
|
using System.Windows.Input;
|
|
|
|
namespace tsf_3074.Windows.Dialog
|
|
{
|
|
public partial class KeybindingDialog : System.Windows.Window
|
|
{
|
|
private readonly KeybindingDialogView _data = new KeybindingDialogView();
|
|
|
|
public KeybindingDialog()
|
|
{
|
|
InitializeComponent();
|
|
DataContext = _data;
|
|
}
|
|
|
|
private void KeybindingDialog_OnKeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.Key == Key.Return || e.Key == Key.Escape)
|
|
{
|
|
Close();
|
|
return;
|
|
}
|
|
|
|
var ctrl = Keyboard.IsKeyDown(Key.LeftCtrl);
|
|
var alt = Keyboard.IsKeyDown(Key.LeftAlt);
|
|
var shift = Keyboard.IsKeyDown(Key.LeftShift);
|
|
|
|
var fs = ctrl ? VirtualKey.ModCtrl :
|
|
alt ? VirtualKey.ModAlt :
|
|
shift ? VirtualKey.ModShift : 0;
|
|
|
|
_data.SetKey(e.Key, fs);
|
|
}
|
|
|
|
public static void OpenKeybindingDialog(out Key key, out uint fs, Key initKey = Key.None, uint initFs = 0)
|
|
{
|
|
var dialog = new KeybindingDialog
|
|
{
|
|
_data =
|
|
{
|
|
Key = initKey,
|
|
Modifier = initFs
|
|
}
|
|
};
|
|
|
|
// wait for exit, and put the values to the ref fields
|
|
dialog.ShowDialog();
|
|
|
|
key = dialog._data.Key;
|
|
fs = dialog._data.Modifier;
|
|
}
|
|
}
|
|
|
|
public class KeybindingDialogView : INotifyPropertyChanged
|
|
{
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
public string StatusText { get; set; } = "Press keys";
|
|
|
|
internal Key Key { get; set; }
|
|
internal uint Modifier { get; set; }
|
|
|
|
public void SetKey(Key key, uint fsMod)
|
|
{
|
|
if (key == Key.LeftCtrl || key == Key.LeftAlt || key == Key.LeftShift || key == Key.System) return;
|
|
|
|
Key = key;
|
|
Modifier = fsMod;
|
|
if (fsMod != 0)
|
|
{
|
|
var mod = fsMod == VirtualKey.ModCtrl ? "Ctrl" :
|
|
fsMod == VirtualKey.ModAlt ? "Alt" :
|
|
fsMod == VirtualKey.ModShift ? "Shift" : "Unknown";
|
|
StatusText = $"{mod} + {Key}";
|
|
}
|
|
else
|
|
{
|
|
StatusText = $"{Key}";
|
|
}
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(StatusText)));
|
|
}
|
|
}
|
|
} |