diff --git a/VirtualDestopCycle/Form1.cs b/VirtualDestopCycle/Form1.cs index 608f8cb..1aa076c 100644 --- a/VirtualDestopCycle/Form1.cs +++ b/VirtualDestopCycle/Form1.cs @@ -28,6 +28,7 @@ public partial class Form1 : Form private readonly HotKeyManager _rightHotkey; private readonly HotKeyManager _leftHotkey; + private readonly HotKeyManager _numberHotkey; private bool closeToTray; @@ -47,6 +48,9 @@ public Form1() _leftHotkey = new HotKeyManager(); _leftHotkey.KeyPressed += LeftKeyManagerPressed; + _numberHotkey = new HotKeyManager(); + _numberHotkey.KeyPressed += NumberHotkeyPressed; + VirtualDesktop.CurrentChanged += VirtualDesktop_CurrentChanged; VirtualDesktop.Created += VirtualDesktop_Added; VirtualDesktop.Destroyed += VirtualDesktop_Destroyed; @@ -64,6 +68,24 @@ public Form1() } } + private void NumberHotkeyPressed(object sender, KeyPressedEventArgs e) + { + var index = (int) e.HotKey.Key - (int)Key.D0 - 1; + var currentDesktopIndex = getCurrentDesktopIndex(); + + if (index == currentDesktopIndex) + { + return; + } + + if (index > desktops.Count - 1) + { + return; + } + + desktops.ElementAt(index)?.Switch(); + } + private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (closeToTray) @@ -121,6 +143,7 @@ private void exitToolStripMenuItem_Click(object sender, EventArgs e) { _rightHotkey.Dispose(); _leftHotkey.Dispose(); + _numberHotkey.Dispose(); closeToTray = false; @@ -128,11 +151,12 @@ private void exitToolStripMenuItem_Click(object sender, EventArgs e) } private void normalHotkeys() - { + { try { _rightHotkey.Register(Key.Right, System.Windows.Input.ModifierKeys.Control | System.Windows.Input.ModifierKeys.Alt); _leftHotkey.Register(Key.Left, System.Windows.Input.ModifierKeys.Control | System.Windows.Input.ModifierKeys.Alt); + RegisterNumberHotkeys(System.Windows.Input.ModifierKeys.Control | System.Windows.Input.ModifierKeys.Alt); } catch (Exception err) { @@ -148,6 +172,7 @@ private void alternateHotkeys() { _rightHotkey.Register(Key.Right, System.Windows.Input.ModifierKeys.Shift | System.Windows.Input.ModifierKeys.Alt); _leftHotkey.Register(Key.Left, System.Windows.Input.ModifierKeys.Shift | System.Windows.Input.ModifierKeys.Alt); + RegisterNumberHotkeys(System.Windows.Input.ModifierKeys.Shift | System.Windows.Input.ModifierKeys.Alt); } catch (Exception err) { @@ -157,6 +182,19 @@ private void alternateHotkeys() } } + private void RegisterNumberHotkeys(ModifierKeys modifiers) + { + _numberHotkey.Register(Key.D1, modifiers); + _numberHotkey.Register(Key.D2, modifiers); + _numberHotkey.Register(Key.D3, modifiers); + _numberHotkey.Register(Key.D4, modifiers); + _numberHotkey.Register(Key.D5, modifiers); + _numberHotkey.Register(Key.D6, modifiers); + _numberHotkey.Register(Key.D7, modifiers); + _numberHotkey.Register(Key.D8, modifiers); + _numberHotkey.Register(Key.D9, modifiers); + } + private void Form1_Load(object sender, EventArgs e) { labelStatus.Text = "";