# Window loop custom event management

**URL:** <https://discourse.glfw.org/t/window-loop-custom-event-management/1895>\
**Category:** support\
**Created:** [July 11, 2021, 2:42pm UTC](https://discourse.glfw.org/t/window-loop-custom-event-management/1895 "2021-07-11T14:42:32Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![birb](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@birb](https://discourse.glfw.org/u/birb)\
**Post date:** [July 11, 2021, 2:42pm UTC](https://discourse.glfw.org/t/window-loop-custom-event-management/1895/1 "2021-07-11T14:42:32Z")

</div>

I noticed that GLFW doesn’t currently support trackpad (ex: windows precision trackpad) “pinch-to-zoom”. By zoom, I mean the one-to-one zoom effect something like below.

![ezgif.com-gif-maker (1)](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/9f047936af422aaecdd45117b077162e94d2950e.gif)

I want to try out implementing custom win32 event handling for my app. Is there a way I can achieve this without rewriting the internal event loop?

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 11, 2021, 3:10pm UTC](https://discourse.glfw.org/t/window-loop-custom-event-management/1895/2 "2021-07-11T15:10:02Z")

</div>

For `win32` I believe you can do this either by using [SetWindowsHookEx](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowshookexa) to add your own window callback function or using [PeekMessage](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-peekmessagea) with the filter parameters set to receive only the messages you need, though the later case might miss some messages which GLFW processes in [\_glfwPlatformPollEvents](https://github.com/glfw/glfw/blob/33cd8b865d9289cfbcf3d95e6e68e4050b94fcd3/src/win32_window.c#L1965-L2045) which you still need to call.

---

<div class="post-metadata">

**Author:** ![birb](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@birb](https://discourse.glfw.org/u/birb)\
**Post date:** [July 12, 2021, 5:30am UTC](https://discourse.glfw.org/t/window-loop-custom-event-management/1895/3 "2021-07-12T05:30:23Z")

</div>

How would I set this up, I’m not sure I understand what’s required.

So I add my filter with PeekMessage, process said event, and then call glfwPollEvents. Will that be sufficient?

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 12, 2021, 9:21am UTC](https://discourse.glfw.org/t/window-loop-custom-event-management/1895/4 "2021-07-12T09:21:42Z")

</div>

That should work, except occasionally it will miss some messages as if they happen between you calling PeekMessage and glfwPollEvents the GLFW message loop will ‘swallow’ them.

SetWindowsHookEx would be a more guaranteed solution to ensure you get all messages, though it’s slightly more complex to use so you could try the PeekMessage approach first.

An alternative is to modify the GLFW message function and maintain a fork, which might be easier.

---

<div class="post-metadata">

**Author:** ![birb](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@birb](https://discourse.glfw.org/u/birb)\
**Post date:** [July 12, 2021, 10:13am UTC](https://discourse.glfw.org/t/window-loop-custom-event-management/1895/5 "2021-07-12T10:13:32Z")

</div>

> it will miss some messages as if they happen between you calling PeekMessage and glfwPollEvents

How so? I thought peek doesn’t remove the message from the message queue.

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 12, 2021, 10:48am UTC](https://discourse.glfw.org/t/window-loop-custom-event-management/1895/6 "2021-07-12T10:48:48Z")

</div>

The difference between `GetMessage` and `PeekMessage` is that `PeekMessage` does not wait until a message is posted, and it has an extra parameter `wRemoveMsg` which controls whether a message is removed from the queue or not.

[\_glfwPlatformPollEvents](https://github.com/glfw/glfw/blob/33cd8b865d9289cfbcf3d95e6e68e4050b94fcd3/src/win32_window.c#L1965-L2045) uses `PM_REMOVE` which removes messages - it has to otherwise the queue would not drain and only the first message would be read.

---

<div class="post-metadata">

**Author:** ![birb](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@birb](https://discourse.glfw.org/u/birb)\
**Post date:** [July 12, 2021, 12:44pm UTC](https://discourse.glfw.org/t/window-loop-custom-event-management/1895/7 "2021-07-12T12:44:48Z")

</div>

Ahh I see now what you mean, and how events can be “swallowed”. Took me a while, thanks!

---

<div class="post-metadata">

**Author:** ![birb](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@birb](https://discourse.glfw.org/u/birb)\
**Post date:** [July 15, 2021, 9:41am UTC](https://discourse.glfw.org/t/window-loop-custom-event-management/1895/8 "2021-07-15T09:41:25Z")

</div>

Hello, I noticed that `WM_GESTURE` isn’t what I’m really looking for. This is off-topic, but wanted to ask if you knew how I can capture trackpad (not touch screen) events.

I’m not receiving any `WM_GESTURE` events when I try to zoom or rotate. I assume it’s the legacy gesture handling, and not the new “precision drivers” events

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 15, 2021, 10:03am UTC](https://discourse.glfw.org/t/window-loop-custom-event-management/1895/9 "2021-07-15T10:03:13Z")

</div>

Does `WM_TOUCH` work?

> **[Detecting and Tracking Multiple Touch Points - Win32 apps](https://docs.microsoft.com/en-us/windows/win32/wintouch/detecting-and-tracking-multiple-touch-points)**
>
> Detecting and Tracking Multiple Touch Points

---

<div class="post-metadata">

**Author:** ![birb](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@birb](https://discourse.glfw.org/u/birb)\
**Post date:** [July 15, 2021, 10:23am UTC](https://discourse.glfw.org/t/window-loop-custom-event-management/1895/10 "2021-07-15T10:23:29Z")

</div>

Nope ☹  
That didn’t work.

I’m doing something like this, hope that’s correct

![image](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/ba55a3c0b2f01923cd6a30dddcc5c1a789a878be.png)

I changed the file-drop to print “drop” and that seems to work, so I guess I _am_ messing with the right loop, but then again, not sure

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 15, 2021, 10:47am UTC](https://discourse.glfw.org/t/window-loop-custom-event-management/1895/11 "2021-07-15T10:47:49Z")

</div>

Note that copying text is more useful than an image as I can’t copy text from an image 🙂

You’ve put your code in the `if (!window)` section, which as per the comment is for _the hidden helper window and for a regular window during its initial creation_. You should put your changes in the second `switch (uMsg)` section after this, [namely lines 548-1214 of the original code](https://github.com/glfw/glfw/blob/33cd8b865d9289cfbcf3d95e6e68e4050b94fcd3/src/win32_window.c#L548-L1214) where `WM_MOUSEMOVE` and other input is processed.

---

<div class="post-metadata">

**Author:** ![birb](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@birb](https://discourse.glfw.org/u/birb)\
**Post date:** [July 15, 2021, 10:49am UTC](https://discourse.glfw.org/t/window-loop-custom-event-management/1895/12 "2021-07-15T10:49:58Z")

</div>

> copying text is more useful than an image

Understood :), my bad.

Ahh you’re right! I’ll check with the other loop, thanks for the heads up!

---

<div class="post-metadata">

**Author:** ![birb](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@birb](https://discourse.glfw.org/u/birb)\
**Post date:** [July 15, 2021, 10:55am UTC](https://discourse.glfw.org/t/window-loop-custom-event-management/1895/13 "2021-07-15T10:55:53Z")

</div>

```auto
    // ...
    switch (uMsg)
    {
        case WM_GESTURE:
        case WM_TOUCH: {
            printf("WM GESTURE OR TOUCH\n");
            break;
        }
        case WM_MOUSEACTIVATE:
        // ...  

```

I still get nothing

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 15, 2021, 11:06am UTC](https://discourse.glfw.org/t/window-loop-custom-event-management/1895/14 "2021-07-15T11:06:23Z")

</div>

I don’t know enough about touch/gesture input to help you further, so I would just advise to read the documentation and perhaps look for samples and try those.

---

<div class="post-metadata">

**Author:** ![birb](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@birb](https://discourse.glfw.org/u/birb)\
**Post date:** [July 15, 2021, 11:16am UTC](https://discourse.glfw.org/t/window-loop-custom-event-management/1895/15 "2021-07-15T11:16:09Z")

</div>

Thanks for your help so far!  
pinch-gesture seems to activate the WM\_MOUSEWHEEL event
