Skip to content

Commit

Permalink
Merge pull request #12 from solo12zw74/goto_desktop_by_number
Browse files Browse the repository at this point in the history
Implement Goto desktop by number
  • Loading branch information
Joe Ipson committed Oct 25, 2016
2 parents c70e53a + a1f4480 commit 1b241dc
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion VirtualDestopCycle/Form1.cs
Expand Up @@ -28,6 +28,7 @@ public partial class Form1 : Form

private readonly HotKeyManager _rightHotkey;
private readonly HotKeyManager _leftHotkey;
private readonly HotKeyManager _numberHotkey;

private bool closeToTray;

Expand All @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -121,18 +143,20 @@ private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
_rightHotkey.Dispose();
_leftHotkey.Dispose();
_numberHotkey.Dispose();

closeToTray = false;

this.Close();
}

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)
{
Expand All @@ -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)
{
Expand All @@ -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 = "";
Expand Down

0 comments on commit 1b241dc

Please sign in to comment.