The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #if JUCE_WINDOWS
  14. namespace juce
  15. {
  16. // This function is in juce_win32_Windowing.cpp
  17. extern bool offerKeyMessageToJUCEWindow (MSG&);
  18. static HHOOK mouseWheelHook = nullptr, keyboardHook = nullptr;
  19. static int numHookUsers = 0;
  20. struct WindowsHooks
  21. {
  22. WindowsHooks()
  23. {
  24. if (numHookUsers++ == 0)
  25. {
  26. mouseWheelHook = SetWindowsHookEx (WH_MOUSE, mouseWheelHookCallback,
  27. (HINSTANCE) juce::Process::getCurrentModuleInstanceHandle(),
  28. GetCurrentThreadId());
  29. keyboardHook = SetWindowsHookEx (WH_GETMESSAGE, keyboardHookCallback,
  30. (HINSTANCE) juce::Process::getCurrentModuleInstanceHandle(),
  31. GetCurrentThreadId());
  32. }
  33. }
  34. ~WindowsHooks()
  35. {
  36. if (--numHookUsers == 0)
  37. {
  38. if (mouseWheelHook != nullptr)
  39. {
  40. UnhookWindowsHookEx (mouseWheelHook);
  41. mouseWheelHook = nullptr;
  42. }
  43. if (keyboardHook != nullptr)
  44. {
  45. UnhookWindowsHookEx (keyboardHook);
  46. keyboardHook = nullptr;
  47. }
  48. }
  49. }
  50. static LRESULT CALLBACK mouseWheelHookCallback (int nCode, WPARAM wParam, LPARAM lParam)
  51. {
  52. if (nCode >= 0 && wParam == WM_MOUSEWHEEL)
  53. {
  54. // using a local copy of this struct to support old mingw libraries
  55. struct MOUSEHOOKSTRUCTEX_ : public MOUSEHOOKSTRUCT { DWORD mouseData; };
  56. auto& hs = *(MOUSEHOOKSTRUCTEX_*) lParam;
  57. if (auto* comp = Desktop::getInstance().findComponentAt ({ hs.pt.x, hs.pt.y }))
  58. if (comp->getWindowHandle() != nullptr)
  59. return PostMessage ((HWND) comp->getWindowHandle(), WM_MOUSEWHEEL,
  60. hs.mouseData & 0xffff0000, (hs.pt.x & 0xffff) | (hs.pt.y << 16));
  61. }
  62. return CallNextHookEx (mouseWheelHook, nCode, wParam, lParam);
  63. }
  64. static LRESULT CALLBACK keyboardHookCallback (int nCode, WPARAM wParam, LPARAM lParam)
  65. {
  66. MSG& msg = *(MSG*) lParam;
  67. if (nCode == HC_ACTION && wParam == PM_REMOVE
  68. && offerKeyMessageToJUCEWindow (msg))
  69. {
  70. zerostruct (msg);
  71. msg.message = WM_USER;
  72. return 1;
  73. }
  74. return CallNextHookEx (keyboardHook, nCode, wParam, lParam);
  75. }
  76. };
  77. } // namespace juce
  78. #endif