1

我有一个对话框,允许用户设置热键以在 Windows 上的 3d 程序中使用。我正在使用 CHotKeyCtrl,它非常好,但不处理用户想要使用的某些键 - 特别是制表符和空格。

热键处理足够智能,可以触发这些键,我只需要一个 UI 来设置它们。类似于 CHotKeyCtrl 的控件将是理想的,但其他解决方法也值得赞赏。

4

1 回答 1

1

一种解决方法是使用带有消息挂钩功能的标准编辑控件。

这将允许您捕获发送到该编辑控件的键盘WM_KEYDOWN消息。

钩子函数看起来像这样:

    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;
    }

当编辑控件获得焦点时,挂钩可以附加到编辑控件,如下所示:

    //-- 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.

于 2008-10-01T03:23:42.243 回答