-
Notifications
You must be signed in to change notification settings - Fork 8
/
Hotkeys.cpp
149 lines (138 loc) · 4.16 KB
/
Hotkeys.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "Hotkeys.h"
#include "resource.h"
// All hotkey IDs.
std::set<Hotkeys::ID> Hotkeys::s_Hotkeys = {
ID::Play,
ID::Stop,
ID::Previous,
ID::Next,
ID::StopAtTrackEnd,
ID::FadeOut,
ID::FadeToNext,
ID::MuteVolume,
ID::DecreaseVolume,
ID::IncreaseVolume,
ID::SkipBackwards,
ID::SkipForwards,
ID::DecreasePitch,
ID::IncreasePitch,
ID::ResetPitch,
ID::BalanceLeft,
ID::BalanceRight,
ID::ResetBalance,
ID::FollowTrackSelection
};
// Hotkey commands.
std::map<Hotkeys::ID,WPARAM> Hotkeys::s_CommandMap = {
{ ID::Play, ID_CONTROL_PLAY },
{ ID::Stop, ID_CONTROL_STOP },
{ ID::Previous, ID_CONTROL_PREVIOUSTRACK },
{ ID::Next, ID_CONTROL_NEXTTRACK },
{ ID::SkipBackwards, ID_CONTROL_SKIPBACKWARDS },
{ ID::SkipForwards, ID_CONTROL_SKIPFORWARDS },
{ ID::StopAtTrackEnd, ID_CONTROL_STOPTRACKEND },
{ ID::FadeOut, ID_CONTROL_FADEOUT },
{ ID::FadeToNext, ID_CONTROL_FADETONEXT },
{ ID::MuteVolume, ID_CONTROL_MUTE },
{ ID::DecreaseVolume, ID_CONTROL_VOLUMEDOWN },
{ ID::IncreaseVolume, ID_CONTROL_VOLUMEUP },
{ ID::DecreasePitch, ID_CONTROL_PITCHDOWN },
{ ID::IncreasePitch, ID_CONTROL_PITCHUP },
{ ID::ResetPitch, ID_CONTROL_PITCHRESET },
{ ID::BalanceLeft, ID_CONTROL_BALANCELEFT },
{ ID::BalanceRight, ID_CONTROL_BALANCERIGHT },
{ ID::ResetBalance, ID_CONTROL_BALANCERESET },
{ ID::FollowTrackSelection, ID_CONTROL_FOLLOWSELECTION }
};
// Hotkey descriptions.
std::map<Hotkeys::ID,UINT> Hotkeys::s_DescriptionMap = {
{ ID::Play, IDS_HOTKEY_PLAY },
{ ID::Stop, IDS_HOTKEY_STOP },
{ ID::Previous, IDS_HOTKEY_PREVIOUS },
{ ID::Next, IDS_HOTKEY_NEXT },
{ ID::SkipBackwards, IDS_HOTKEY_SKIPBACKWARDS },
{ ID::SkipForwards, IDS_HOTKEY_SKIPFORWARDS },
{ ID::StopAtTrackEnd, IDS_HOTKEY_STOPATEND },
{ ID::FadeOut, IDS_HOTKEY_FADEOUT },
{ ID::FadeToNext, IDS_HOTKEY_FADETONEXT },
{ ID::MuteVolume, IDS_HOTKEY_MUTE },
{ ID::DecreaseVolume, IDS_HOTKEY_VOLUMEDOWN },
{ ID::IncreaseVolume, IDS_HOTKEY_VOLUMEUP },
{ ID::DecreasePitch, IDS_HOTKEY_PITCHDOWN },
{ ID::IncreasePitch, IDS_HOTKEY_PITCHUP },
{ ID::ResetPitch, IDS_HOTKEY_PITCHRESET },
{ ID::BalanceLeft, IDS_HOTKEY_BALANCELEFT },
{ ID::BalanceRight, IDS_HOTKEY_BALANCERIGHT },
{ ID::ResetBalance, IDS_HOTKEY_BALANCERESET },
{ ID::FollowTrackSelection, IDS_HOTKEY_FOLLOWSELECTION }
};
Hotkeys::Hotkeys( const HWND wnd, Settings& settings ) :
m_hWnd( wnd ),
m_Settings( settings ),
m_RegisteredHotkeys()
{
Update();
}
Hotkeys::~Hotkeys()
{
Unregister();
}
void Hotkeys::Update()
{
Unregister();
bool enable = false;
Settings::HotkeyList hotkeys;
m_Settings.GetHotkeySettings( enable, hotkeys );
if ( enable ) {
for ( const auto& hotkey : hotkeys ) {
if ( ( 0 != hotkey.ID ) && ( 0 != hotkey.Code ) ) {
UINT modifiers = 0;
if ( hotkey.Alt ) {
modifiers |= MOD_ALT;
}
if ( hotkey.Ctrl ) {
modifiers |= MOD_CONTROL;
}
if ( hotkey.Shift ) {
modifiers |= MOD_SHIFT;
}
if ( FALSE != RegisterHotKey( m_hWnd, hotkey.ID, modifiers, static_cast<UINT>( hotkey.Code ) ) ) {
m_RegisteredHotkeys.insert( hotkey.ID );
}
}
}
}
}
void Hotkeys::Unregister()
{
for ( const auto& id : m_RegisteredHotkeys ) {
UnregisterHotKey( m_hWnd, id );
}
m_RegisteredHotkeys.clear();
}
void Hotkeys::OnHotkey( const WPARAM wParam )
{
const ID id = static_cast<ID>( wParam );
const auto iter = s_CommandMap.find( id );
if ( s_CommandMap.end() != iter ) {
const WPARAM command = iter->second;
SendMessage( m_hWnd, WM_COMMAND, command, 0 );
}
}
std::wstring Hotkeys::GetDescription( const HINSTANCE instance, const ID id )
{
std::wstring description;
const auto iter = s_DescriptionMap.find( id );
if ( s_DescriptionMap.end() != iter ) {
const UINT stringID = iter->second;
const int bufSize = 64;
WCHAR buffer[ bufSize ] = {};
LoadString( instance, stringID, buffer, bufSize );
description = buffer;
}
return description;
}
std::set<Hotkeys::ID> Hotkeys::GetHotkeyIDs()
{
return s_Hotkeys;
}