Skip to content

Commit

Permalink
improve hotkey to string
Browse files Browse the repository at this point in the history
  • Loading branch information
thantthet committed Mar 4, 2019
1 parent a37a5dd commit fdedf67
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
37 changes: 28 additions & 9 deletions windows/KeyMagic2/KeyMagic2/HotkeyManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,41 +49,60 @@ void HotkeyManager::ReloadHotkey()
}
}


std::string HotkeyManager::funcKeyNameForVK(BYTE vkCode)
{
if (vkCode >= VK_F1 && vkCode <= VK_F24) {
int number = (vkCode - VK_F1) + 1;
auto name = std::string("F") + std::to_string(number);
return name;
}
return std::string();
}

std::string HotkeyManager::wHotkeyToString(WORD hotkey)
{
{
std::string s;
std::stringstream ss;

BYTE vk = LOBYTE(hotkey);
BYTE mod = HIBYTE(hotkey);

if (mod & HOTKEYF_ALT)
{
s += "Alt+";
ss << "Alt+";
}
if (mod & HOTKEYF_CONTROL)
{
s += "Ctrl+";
ss << "Ctrl+";
}
if (mod & HOTKEYF_SHIFT)
{
s += "Shift+";
ss << "Shift+";
}
if (mod & HOTKEYF_EXT)
{
s += "Win+";
ss << "Win+";
}

if (vk)
{
s += (char)MapVirtualKey(vk, MAPVK_VK_TO_CHAR);
if (auto ch = (char)MapVirtualKey(vk, MAPVK_VK_TO_CHAR)) {
ss << ch;
}
else {
ss << funcKeyNameForVK(vk);
}
}

if (s.back() == '+') {
std::string s = ss.str();

if (s.back() == '+')
{
s.pop_back();
}

if (s.back() == ' ') {
else if (s.back() == ' ')
{
s.pop_back();
s += "Space";
}
Expand Down
1 change: 1 addition & 0 deletions windows/KeyMagic2/KeyMagic2/HotkeyManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class HotkeyManager
void WriteHotkey();
void ReloadHotkey();

static std::string funcKeyNameForVK(BYTE vkCode);
static std::string wHotkeyToString(WORD hotkey);

void AddOnHotkeyHandler(std::function<void()> callback, LPWORD hotkeyPointer)
Expand Down

0 comments on commit fdedf67

Please sign in to comment.