-
Notifications
You must be signed in to change notification settings - Fork 1
/
FormMain.vb
116 lines (99 loc) · 4.77 KB
/
FormMain.vb
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
Imports NHotkey
Imports NHotkey.WindowsForms
Public Class FormMain
Private _spotifyHwnd As IntPtr
Private Sub ButtonRegister_Click(sender As Object, e As EventArgs) Handles ButtonRegister.Click
Select Case ButtonRegister.Text
Case "Register"
CheckSetSpotify()
RegisterHotkeysFromFile($"{Application.StartupPath}\Hotkeys.txt")
HotkeyManager.Current.IsEnabled = True
ButtonRegister.Text = "Unregister"
Case "Unregister"
HotkeyManager.Current.IsEnabled = False
ButtonRegister.Text = "Register"
End Select
End Sub
Private Sub RegisterHotkeysFromFile(filePath As String)
Dim lines As String() = IO.File.ReadAllLines(filePath)
For Each line As String In lines
Dim parts As String() = line.Split("="c)
If parts.Length = 2 Then
Dim action As String = parts(0).Trim()
Dim keys As String = parts(1).Trim()
RegisterHotkey(action, keys)
End If
Next
End Sub
Private Sub RegisterHotkey(action As String, keyCombination As String)
Try
Dim parts As String() = keyCombination.Split("+"c)
Dim modifierNames = parts.Take(parts.Length - 1) ' Take all but the last element as modifiers
Dim keyName = parts.Last() ' The last element is the key
Dim modifiers = HotkeyParser.ParseModifier(String.Join("+", modifierNames))
Dim key = HotkeyParser.ParseKey(keyName)
HotkeyManager.Current.AddOrReplace(action, key Or modifiers, AddressOf HotkeyPressed)
Console.WriteLine($"Successfully registered hotkey: {action}")
Catch ex As Exception
Console.WriteLine($"Error registering hotkey {action}: {ex.Message}")
End Try
End Sub
Private Sub HotkeyPressed(sender As Object, e As HotkeyEventArgs)
e.Handled = True
Select Case e.Name
Case "VolUp"
NativeMethods.SendMessage(_spotifyHwnd, NativeMethods.WM_APPCOMMAND, 0, New IntPtr(CInt(NativeMethods.AppCommand.VolUp)))
Case "VolDown"
NativeMethods.SendMessage(_spotifyHwnd, NativeMethods.WM_APPCOMMAND, 0, New IntPtr(CInt(NativeMethods.AppCommand.VolDown)))
Case "PrevTrack"
NativeMethods.SendMessage(_spotifyHwnd, NativeMethods.WM_APPCOMMAND, 0, New IntPtr(CInt(NativeMethods.AppCommand.Previous)))
Case "NextTrack"
NativeMethods.SendMessage(_spotifyHwnd, NativeMethods.WM_APPCOMMAND, 0, New IntPtr(CInt(NativeMethods.AppCommand.Next)))
Case "PlayPause"
NativeMethods.SendMessage(_spotifyHwnd, NativeMethods.WM_APPCOMMAND, 0, New IntPtr(CInt(NativeMethods.AppCommand.PlayPause)))
Case "Like"
SpotifyHelper.SendHotkeyToSpotify("+%(b)")
Case "Dislike"
SpotifyHelper.SendHotkeyToSpotify("+%(b)")
End Select
End Sub
Private Sub CheckSetSpotify()
_spotifyHwnd = SpotifyHelper.GetSpotify()
If _spotifyHwnd <> IntPtr.Zero Then
LabelStatus.Text = "Status: Ready!"
Else
LabelStatus.Text = "Status: Failure!"
End If
End Sub
Private Sub ButtonHotKey_Click(sender As Object, e As EventArgs) Handles ButtonHotKey.Click
Dim frmHotKey As New FormHotKeyHelper
frmHotKey.ShowDialog()
End Sub
Private Sub FormMain_Resize(sender As Object, e As EventArgs) Handles Me.Resize
If Me.WindowState = FormWindowState.Minimized Then
Me.Hide() ' Hide the form
NotifyIcon1.Visible = True ' Show the NotifyIcon in the system tray
End If
End Sub
Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
ShowWindow()
End Sub
Private Sub RestoreToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RestoreToolStripMenuItem.Click
ShowWindow()
End Sub
Private Sub ShowWindow()
Me.Show() ' Show the form
Me.WindowState = FormWindowState.Normal ' Restore the window to normal state
NotifyIcon1.Visible = False ' Optionally hide the NotifyIcon once the form is visible
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
End
End Sub
Private Sub GitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GitToolStripMenuItem.Click
Process.Start("https://github.com/moorer2k/SpotiKeys")
End Sub
Private Sub FormMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
NotifyIcon1.Visible = False
NotifyIcon1.Dispose()
End Sub
End Class