Likely by detecting certain keystrokes (that's what Snapchat does on iOS for example). Idk if there are other ways on macOS or Windows.
https://developer.apple.com/documentation/uikit/uiapplicatio...
AFAIK, on iOS, the app is actually sent an event by the OS when a screenshot is captured.
[0] https://security.stackexchange.com/questions/170596/is-it-po...