How can I make my DLL constantly check for hotkeys but without affecting other functions in it? If I put a loop, it will not be able to get any other calls to functions.. Do I create a thread and pass my functions to the thread upon attachment?

My silly attempt..

#include "main.h"

// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
    MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}

enum {F9_KEYID = 1, F10_KEYID = 2};
void DLL_EXPORT Meh()
{
    MSG msg = {0};
    PeekMessage(&msg, 0, 0, 0, 0x0001);
    TranslateMessage(&msg);
    switch(msg.message)
    {
        case WM_HOTKEY:
            if(msg.wParam == F9_KEYID)
            {
                MessageBoxA(0, "F9 Pressed", "DLL Message", MB_OK | MB_ICONINFORMATION);
            }
            else if(msg.wParam == F10_KEYID)
            {
                MessageBoxA(0, "F10 Pressed", "DLL Message", MB_OK | MB_ICONINFORMATION);
            }
    }
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            // attach to process
            // return FALSE to fail DLL load
                RegisterHotKey(0, F9_KEYID, 0x4000, VK_F9);
                RegisterHotKey(0, F10_KEYID, 0x4000, VK_F10);
                Meh();
            break;

        case DLL_PROCESS_DETACH:
            // detach from process
            break;

        case DLL_THREAD_ATTACH:
            // attach to thread
            break;

        case DLL_THREAD_DETACH:
            // detach from thread
            break;
    }
    return TRUE; // succesful
}

i think the best way is to capture the keys pressed using the GetKeyboardState.

unsigned char KeyStates[256];
GetKeyboardState(KeyStates);
 
for (int i = 0; i < 256; i++)
{
      if (KeyStates[i] & 0x8000)
      {
            // The keyboard key that 'i' represents is currently held down.
      }

you could substitute the values for whatever key you need. however you'll need to pass this to each function you want to have this process.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.