Trackpad events -- counting fingers in macOS

I’m keeping track of the number of fingers currently placed on the trackpad simply by incrementing an integer with every FINGERDOWN event, and decrementing with every FINGERUP. On balance, this works surprisingly well – the state never gets out of balance.

However, I have noticed that when multiple fingers are placed on the trackpad, FINGERUP events are sometimes missed at the time they actually occur, and are deferred until the next touch event occurs. I also get what seem like spurious FINGERDOWN events, though they may be points of extremely brief contact where the FINGERUP is deferred. Therefore, my application sometimes thinks a finger is down when none are. The state is always corrected upon the next touch event, but no sooner.

All of this makes it extremely difficult to implement elegant scrolling behavior.

I’m not sure what’s going on and therefore how best to address it. Does macOS (or SDL?) account for deficiencies in the hardware’s ability to detect finger placement/removal by separately counting the number of touch points with each event, and issuing corrective touch events to get the number of fingers back in line? If that’s the case, how do all my other applications implement such seamless scrolling? The spurious or transient FINGERDOWNs halt inertial scroll in a way that doesn’t happen elsewhere.

If anyone has had any luck with this sort of thing, I would be very glad to hear how you handled it!