forked from jfchapman/VUPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDlgHotkey.cpp
182 lines (170 loc) · 4.58 KB
/
DlgHotkey.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "DlgHotkey.h"
#include "resource.h"
#include "Utility.h"
INT_PTR CALLBACK DlgHotkey::DialogProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
switch ( message ) {
case WM_INITDIALOG : {
DlgHotkey* dialog = reinterpret_cast<DlgHotkey*>( lParam );
if ( nullptr != dialog ) {
SetWindowLongPtr( hwnd, DWLP_USER, lParam );
dialog->OnInitDialog( hwnd );
}
break;
}
case WM_DESTROY : {
SetWindowLongPtr( hwnd, DWLP_USER, 0 );
break;
}
case WM_COMMAND : {
switch ( LOWORD( wParam ) ) {
case IDCANCEL :
case IDOK : {
EndDialog( hwnd, 0 );
return TRUE;
}
default : {
break;
}
}
break;
}
default : {
break;
}
}
return FALSE;
}
LRESULT CALLBACK DlgHotkey::ButtonProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
DlgHotkey* dialog = reinterpret_cast<DlgHotkey*>( GetWindowLongPtr( hwnd, GWLP_USERDATA ) );
if ( nullptr != dialog ) {
switch ( message ) {
case WM_KEYDOWN: {
if ( dialog->OnKeyDown( wParam, lParam ) ) {
PostMessage( GetParent( hwnd ), WM_COMMAND, IDOK, 0 );
} else {
switch ( wParam ) {
case VK_ESCAPE :
case VK_RETURN : {
PostMessage( GetParent( hwnd ), WM_COMMAND, IDCANCEL, 0 );
break;
}
}
}
break;
}
case WM_GETDLGCODE : {
return DLGC_WANTALLKEYS;
}
case WM_DESTROY : {
SetWindowLongPtr( hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>( dialog->GetDefaultButtonProc() ) );
break;
}
default : {
break;
}
}
}
return CallWindowProc( dialog->GetDefaultButtonProc(), hwnd, message, wParam, lParam );
}
DlgHotkey::DlgHotkey( const HINSTANCE instance, const HWND parent ) :
m_Hotkey( {} ),
m_Valid( false ),
m_UnmodifiedHotkeys(),
m_ModifiedHotkeys(),
m_hWnd( nullptr ),
m_DefaultButtonProc( nullptr )
{
InitAllowedHotkeys();
DialogBoxParam( instance, MAKEINTRESOURCE( IDD_HOTKEY ), parent, DialogProc, reinterpret_cast<LPARAM>( this ) );
}
DlgHotkey::~DlgHotkey()
{
}
void DlgHotkey::OnInitDialog( const HWND hwnd )
{
m_hWnd = hwnd;
CentreDialog( m_hWnd );
const HWND buttonWnd = GetDlgItem( m_hWnd, IDCANCEL );
SetWindowLongPtr( buttonWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>( this ) );
m_DefaultButtonProc = reinterpret_cast<WNDPROC>( SetWindowLongPtr( buttonWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>( ButtonProc ) ) );
}
WNDPROC DlgHotkey::GetDefaultButtonProc()
{
return m_DefaultButtonProc;
}
bool DlgHotkey::GetHotkey( Settings::Hotkey& hotkey ) const
{
hotkey = m_Hotkey;
return m_Valid;
}
bool DlgHotkey::OnKeyDown( WPARAM wParam, LPARAM lParam )
{
const int keycode = static_cast<int>( wParam );
bool alt = false;
bool control = false;
bool shift = false;
GetKeyStates( alt, control, shift );
const bool modified = ( alt || control || shift );
m_Valid = ( m_UnmodifiedHotkeys.end() != m_UnmodifiedHotkeys.find( keycode ) );
if ( !m_Valid && modified ) {
m_Valid = ( m_ModifiedHotkeys.end() != m_ModifiedHotkeys.find( keycode ) );
}
if ( m_Valid ) {
m_Hotkey.Code = keycode;
m_Hotkey.Alt = alt;
m_Hotkey.Ctrl = control;
m_Hotkey.Shift = shift;
const int bufSize = 64;
WCHAR buffer[ bufSize ] = {};
if ( 0 != GetKeyNameText( static_cast<LONG>( lParam ), buffer, bufSize ) ) {
m_Hotkey.Name = buffer;
}
}
return m_Valid;
}
void DlgHotkey::InitAllowedHotkeys()
{
for ( int key = VK_F1; key <= VK_F11; key++ ) {
m_UnmodifiedHotkeys.insert( key );
}
for ( int key = VK_F13; key <= VK_F24; key++ ) {
m_UnmodifiedHotkeys.insert( key );
}
for ( int key = VK_VOLUME_MUTE; key <= VK_MEDIA_PLAY_PAUSE; key++ ) {
m_UnmodifiedHotkeys.insert( key );
}
for ( int key = 0x30 /* 0 */; key <= 0x39 /* 9 */; key++ ) {
m_ModifiedHotkeys.insert( key );
}
for ( int key = 0x41 /* A */; key <= 0x5A /* Z */; key++ ) {
m_ModifiedHotkeys.insert( key );
}
for ( int key = VK_NUMPAD0; key <= VK_DIVIDE; key++ ) {
m_ModifiedHotkeys.insert( key );
}
for ( int key = VK_SPACE; key <= VK_HELP; key++ ) {
m_ModifiedHotkeys.insert( key );
}
for ( int key = VK_BROWSER_BACK; key <= VK_LAUNCH_APP2; key++ ) {
m_ModifiedHotkeys.insert( key );
}
for ( int key = VK_OEM_1; key <= VK_OEM_3; key++ ) {
m_ModifiedHotkeys.insert( key );
}
for ( int key = VK_OEM_4; key <= VK_OEM_8; key++ ) {
m_ModifiedHotkeys.insert( key );
}
m_ModifiedHotkeys.insert( VK_PAUSE );
m_ModifiedHotkeys.insert( VK_SLEEP );
}
void DlgHotkey::GetKeyStates( bool& alt, bool &control, bool& shift ) const
{
SHORT value = GetAsyncKeyState( VK_MENU );
alt = value < 0;
value = GetAsyncKeyState( VK_CONTROL );
control = value < 0;
value = GetAsyncKeyState( VK_SHIFT );
shift = value < 0;
}