-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from winggar/hotkey
Hotkey
- Loading branch information
Showing
8 changed files
with
1,124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters