views:

308

answers:

1

I have a dialog box that allows users to set hotkeys for use in a 3d program on windows. I'm using CHotKeyCtrl, which is pretty good, but doesn't handle some keys that the users would like to use - specifically, tab and space.

The hotkey handling is smart enough to be able to fire on those keys, I just need a UI to let them be set. A control similar to CHotKeyCtrl would be ideal, but other workarounds are also appreciated.

+1  A: 

One workarounds option would be to use a stock standard edit control with a message hook function.

This would allow you to trap the keyboard WM_KEYDOWN messages sent to that edit control.

The hook function would look something like this:

    LRESULT CALLBACK MessageHook(int code, WPARAM wParam, LPMSG lpMsg)
    {
      LRESULT lResult = 0;

      if ((code >= 0) && (code == MSGF_DIALOGBOX))
      {
        if (lpMsg->message == WM_KEYDOWN)
        {
          //-- process the key down message
          lResult = 1;
        }
      }

      // do default processing if required
      if (lResult == 0)
      {
        lResult = CallNextHookEx(MessageFilterHook, code, wParam, (LPARAM)lpMsg);
      }

      return lResult;
    }

The hook can then be attached to edit control when the edit control gets focus as follows:

    //-- create an instance thunk for our hook callback
    FARPROC FilterProc = (FARPROC) MakeProcInstance((HOOKPROC)(MessageHook),
                                                    hInstance);
    //-- attach the message hook
    FilterHook = SetWindowsHookEx(WH_MSGFILTER, 
                                 (HOOKPROC)FilterProc, 
                                  hInstance, GetCurrentThreadId());

and removed when the edit control when looses focus as follows:

    //-- remove a message hook
    UnhookWindowsHookEx(MessageFilterHook);

Using this approach every key press will be sent to the hook, provided the edit control has focus.

jussij
This is looking pretty good. I'll see if I can implement this and post it back when I get a chance.
tfinniga