Skip to content

Commit

Permalink
Merge pull request #2 from winggar/hotkey
Browse files Browse the repository at this point in the history
Hotkey
  • Loading branch information
benjaminiserman authored Jan 8, 2022
2 parents 425d631 + cafce1e commit 04c58fe
Show file tree
Hide file tree
Showing 8 changed files with 1,124 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Iksokodo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WK.Libraries.HotkeyListenerNS;

internal record Config
{
public Hotkey ToggleHotkey { get; set; } = new(Keys.Alt, Keys.E);
}
72 changes: 72 additions & 0 deletions EnterHotkeyForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions EnterHotkeyForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Iksokodo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

public partial class EnterHotkeyForm : Form
{
public EnterHotkeyForm()
{
InitializeComponent();
}

private void SubmitButton_Click(object sender, EventArgs e)
{
HotkeyManager.UpdateHotkey(HotkeyTextBox.Text);
Close();
}

public Control GetHotkeyTextbox() => HotkeyTextBox;
}
921 changes: 921 additions & 0 deletions EnterHotkeyForm.resx

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions HotKeyManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace Iksokodo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using WK.Libraries.HotkeyListenerNS;

internal static class HotkeyManager
{
static HotkeyListener _listener;
static Action<object, EventArgs> _action;
static Hotkey _hotkey;

public static void RegisterHotkey(Action<object, EventArgs> action)
{
_listener = new();
_action = action;

_hotkey = Program.Config.ToggleHotkey;

_listener.Add(_hotkey);

_listener.HotkeyPressed += (_, e) =>
{
if (e.Hotkey == _hotkey)
{
_action(null, null);
}
};
}

public static void EnableSelector(Control control) => new HotkeySelector().Enable(control);

public static void UpdateHotkey(string s)
{
Hotkey newHotkey;

try
{
newHotkey = new(s);
}
catch (Exception ex)
{
MessageBox.Show($"Change hotkey failed.\n{ex}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}

_listener.Update(_hotkey, newHotkey);

_hotkey = newHotkey;
Program.Config.ToggleHotkey = newHotkey;

Program.SaveConfig();
}
}
1 change: 1 addition & 0 deletions Iksokodo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

<ItemGroup>
<PackageReference Include="H.InputSimulator" Version="1.2.1" />
<PackageReference Include="HotkeyListener" Version="1.9.0" />
</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
namespace Iksokodo;
using System.Text.Json;

internal static class Program
{
public static Config Config { get; private set; }
public const string CONFIG_PATH = @"config.json";

[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();

if (!File.Exists(CONFIG_PATH))
{
Config = new Config();
SaveConfig();
}
else Config = JsonSerializer.Deserialize<Config>(File.ReadAllText(CONFIG_PATH));

SystemTrayProcess taskBarProcess = new();

AppDomain.CurrentDomain.ProcessExit += new EventHandler(taskBarProcess.Exit);

Application.Run(taskBarProcess);
}

public static void SaveConfig() => File.WriteAllText(CONFIG_PATH, JsonSerializer.Serialize(Config));
}
23 changes: 21 additions & 2 deletions SystemTrayProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ internal class SystemTrayProcess : ApplicationContext
private static readonly Icon _normal = new("iksokodo.ico"), _suspended = new("iksokodo_suspend.ico");

private readonly NotifyIcon _trayIcon;
private readonly ToolStripMenuItem _toggleButton, _exitButton;
private readonly ToolStripMenuItem _toggleButton, _changeHotkeyButton, _exitButton;

private readonly Converter _converter;
private readonly Timer _timer;

private const string PAUSE_MESSAGE = "Pause", RESUME_MESSAGE = "Resume", EXIT_MESSAGE = "Exit";
private const string PAUSE_MESSAGE = "Pause", RESUME_MESSAGE = "Resume", CHANGE_HOTKEY_MESSAGE = "Change Hotkey", EXIT_MESSAGE = "Exit";

internal SystemTrayProcess()
{
Expand All @@ -26,16 +26,24 @@ internal SystemTrayProcess()
Text = PAUSE_MESSAGE,
};

_changeHotkeyButton = new()
{
Name = CHANGE_HOTKEY_MESSAGE,
Text = CHANGE_HOTKEY_MESSAGE,
};

_exitButton = new()
{
Name = EXIT_MESSAGE,
Text = EXIT_MESSAGE,
};

_toggleButton.Click += new EventHandler(ToggleLoop);
_changeHotkeyButton.Click += new EventHandler(ChangeHotkey);
_exitButton.Click += new EventHandler(Exit);

menuStrip.Items.Add(_toggleButton);
menuStrip.Items.Add(_changeHotkeyButton);
menuStrip.Items.Add(_exitButton);

_trayIcon = new()
Expand All @@ -50,6 +58,8 @@ internal SystemTrayProcess()

_converter = new();

HotkeyManager.RegisterHotkey(ToggleLoop);

_timer = new() { Interval = 10 };
_timer.Elapsed += (object _, ElapsedEventArgs _) => _converter.Convert();
_timer.Start();
Expand Down Expand Up @@ -84,6 +94,15 @@ private void ToggleLoop(object sender, EventArgs e)
_converter.Paused = !wasPaused;
}

private void ChangeHotkey(object sender, EventArgs e)
{
EnterHotkeyForm form = new();

HotkeyManager.EnableSelector(form.GetHotkeyTextbox());

form.Show();
}

internal void Exit(object sender, EventArgs e)
{
_trayIcon.Visible = false;
Expand Down

0 comments on commit 04c58fe

Please sign in to comment.