1 #include "stdafx.h" 2 #include "CHotKey.h" 3 #include "Common.h" 4 CHotKey()5CHotKey::CHotKey() 6 { 7 } 8 9 ~CHotKey()10CHotKey::~CHotKey() 11 { 12 } 13 14 Modifiers() const15WORD CHotKey::Modifiers() const 16 { 17 WORD control_key{}; 18 if (ctrl) 19 control_key |= MOD_CONTROL; 20 if (shift) 21 control_key |= MOD_SHIFT; 22 if (alt) 23 control_key |= MOD_ALT; 24 25 return control_key; 26 } 27 ToString() const28wstring CHotKey::ToString() const 29 { 30 wstring str; 31 if (key == 0) 32 return str; 33 34 if (ctrl) 35 str += L"Ctrl+"; 36 if (shift) 37 str += L"Shift+"; 38 if (alt) 39 str += L"Alt+"; 40 41 if ((key >= '0'&&key <= '9') || (key >= 'A' && key <= 'Z')) 42 { 43 str += static_cast<wchar_t>(key); 44 } 45 else 46 { 47 wchar_t buff[16]; 48 swprintf_s(buff, L"%d", key); 49 str += buff; 50 } 51 52 return str; 53 } 54 FromString(const wstring & str)55void CHotKey::FromString(const wstring & str) 56 { 57 Clear(); 58 vector<wstring> str_list; 59 CCommon::StringSplit(str, L'+', str_list); 60 if (str_list.empty()) 61 { 62 return; 63 } 64 65 if (str_list.back().size() == 1) 66 { 67 wchar_t ch = str_list.back()[0]; 68 if (ch >= 'a' && ch <= 'z') 69 ch = -32; 70 71 //if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z')) 72 key = ch; 73 //else 74 // CHotKey(); 75 } 76 else 77 { 78 key = _wtoi(str_list.back().c_str()); 79 } 80 81 for (size_t i = 0; i < str_list.size() - 1; i++) 82 { 83 if (str_list[i] == L"Ctrl") 84 ctrl = true; 85 if (str_list[i] == L"Shift") 86 shift = true; 87 if (str_list[i] == L"Alt") 88 alt = true; 89 } 90 } 91 GetHotkeyName() const92wstring CHotKey::GetHotkeyName() const 93 { 94 wstring str; 95 if (ctrl) 96 str += L"Ctrl + "; 97 if (shift) 98 str += L"Shift + "; 99 if (alt) 100 str += L"Alt + "; 101 102 BOOL bExtended = FALSE; 103 if (key <= 0x2F) 104 bExtended = TRUE; 105 106 str += CHotKeyCtrl::GetKeyName(key, bExtended); 107 108 return str; 109 } 110 Clear()111void CHotKey::Clear() 112 { 113 alt = false; 114 ctrl = false; 115 shift = false; 116 key = 0; 117 } 118