visual-c++mfcnumpadregisterhotkey

Using RegisterHotKey with number pad values not working?


I have some constants:

constexpr int MonitorDisplay1 = 100;
constexpr int MonitorDisplay2 = 200;
constexpr int MonitorDisplay3 = 400;
constexpr int MonitorDisplay4 = 500;

constexpr int MonitorDisplay1KeyPad = 101;
constexpr int MonitorDisplay2KeyPad = 201;
constexpr int MonitorDisplay3KeyPad = 401;
constexpr int MonitorDisplay4KeyPad = 501;

I have an OnCreate where I setup the hot keys:

int CCenterCursorOnScreenDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (__super::OnCreate(lpCreateStruct) == -1)
        return -1;

    auto RegisterAppHotkey = [hAppWnd = GetSafeHwnd()](const int nHotKeyID, UINT vk)->bool
    {
        if (!::RegisterHotKey(hAppWnd, nHotKeyID, MOD_CONTROL | MOD_SHIFT, vk))
        {
            auto const ec = ::GetLastError();
            auto const err_msg = std::format(L"RegisterHotKey failed (error: {})\n",
                ec);
            AfxMessageBox(err_msg.c_str());
            return false;
        }

        return true;
    };

    if (m_monitors.rcMonitors.size() > 0)
    {
        if (!RegisterAppHotkey(MonitorDisplay1, '1'))
        {
            return -1;
        }
        if (!RegisterAppHotkey(MonitorDisplay1KeyPad, VK_NUMPAD1))
        {
            return -1;
        }
    }

    if (m_monitors.rcMonitors.size() > 1)
    {
        if (!RegisterAppHotkey(MonitorDisplay2, '2'))
        {
            return -1;
        }
        if (!RegisterAppHotkey(MonitorDisplay2KeyPad, VK_NUMPAD2))
        {
            return -1;
        }
    }

    if (m_monitors.rcMonitors.size() > 2)
    {
        if (!RegisterAppHotkey(MonitorDisplay3, '3'))
        {
            return -1;
        }
        if (!RegisterAppHotkey(MonitorDisplay3KeyPad, VK_NUMPAD3))
        {
            return -1;
        }
    }

    if (m_monitors.rcMonitors.size() > 3)
    {
        if (!RegisterAppHotkey(MonitorDisplay4, '4'))
        {
            return -1;
        }
        if (!RegisterAppHotkey(MonitorDisplay4KeyPad, VK_NUMPAD4))
        {
            return -1;
        }
    }

    return 0;
}

Previously I just had the 4 hotkeys for 1 / 2 / 3 and 4. They still work. I tried to add new hotkeys for for the number pad on the keyboard, but they are not working.

My OnHotKey handler:

void CCenterCursorOnScreenDlg::OnHotKey(UINT nHotKeyId, UINT nKey1, UINT nKey2)
{
    if (nHotKeyId == MonitorDisplay1 || nHotKeyId == MonitorDisplay1KeyPad)
    {
        CenterCursorOnMonitor(0);
    }
    else if (nHotKeyId == MonitorDisplay2 || nHotKeyId == MonitorDisplay2KeyPad)
    {
        CenterCursorOnMonitor(1);
    }
    else if (nHotKeyId == MonitorDisplay3 || nHotKeyId == MonitorDisplay3KeyPad)
    {
        CenterCursorOnMonitor(2);
    }
    else if (nHotKeyId == MonitorDisplay4 || nHotKeyId == MonitorDisplay4KeyPad)
    {
        CenterCursorOnMonitor(3);
    }

    __super::OnHotKey(nHotKeyId, nKey1, nKey2);
}

But the number pad versions are not working. Why?


I am unregistering all 8 hotkeys, and num-lock is on. I get no warnings when registering.


Solution

  • This article explains List of Keys (Keyboard, Mouse and Joystick):

    enter image description here

    Because I was using CTRL + SHIFT, then the meaning of the 4 numeric keys was changing:

    It made sense to change my hotkeys to CTRL + ALT instead to avoid this issue.