-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrayApplicationContext.cs
78 lines (64 loc) · 2.3 KB
/
TrayApplicationContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using TheTalosCompanion.Properties;
namespace TheTalosCompanion
{
internal class TrayApplicationContext : ApplicationContext
{
private NotifyIcon _trayIcon;
private GlobalKeyboardHook _globalKeyboardHook;
private QrCodeForm? _qrCodeForm = null;
private HexDecodeForm? _hexDecodeForm = null;
public TrayApplicationContext()
{
_trayIcon = new NotifyIcon()
{
Icon = Resources.AppIcon,
ContextMenuStrip = _buildContextMenuStrip(),
Visible = true
};
_globalKeyboardHook = new GlobalKeyboardHook(new Keys[] { Keys.PrintScreen, Keys.Insert });
_globalKeyboardHook.KeyboardPressed += OnKeyPressed;
}
public ContextMenuStrip _buildContextMenuStrip()
{
ContextMenuStrip menuStrip = new ContextMenuStrip();
ToolStripMenuItem exitItem = new ToolStripMenuItem("Exit", null, Exit);
menuStrip.Items.Add(exitItem);
return menuStrip;
}
private void Exit(object? sender, EventArgs e)
{
_trayIcon.Visible = false;
Application.Exit();
}
private void OnKeyPressed(object? sender, GlobalKeyboardHookEventArgs e)
{
if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyUp)
{
switch (e.KeyboardData.Key)
{
case Keys.PrintScreen:
_handleQrCode();
e.Handled = true;
break;
case Keys.Insert:
_handleHexDecode();
e.Handled = true;
break;
}
}
}
private void _handleQrCode()
{
if (_qrCodeForm == null || _qrCodeForm.IsDisposed) _qrCodeForm = new QrCodeForm();
if (!_qrCodeForm.Visible)
_qrCodeForm.Show();
else
_qrCodeForm.DecodeNewImage();
}
private void _handleHexDecode()
{
if (_hexDecodeForm == null || _hexDecodeForm.IsDisposed) _hexDecodeForm = new HexDecodeForm();
if (!_hexDecodeForm.Visible) _hexDecodeForm.Show();
}
}
}