Microcontrollers (STM32) and GPIO with momentary buttons
That momentary button is not actually momentary. Usually you won't be able to perform a shorter press than ~100ms. Now, your controller is running at some frequency between 1MHz and 1Ghz. Now calculate how often your code will get to the point where it detects the button press (hint: many thousands of times). So, detecting a press is easy. 

It gets harder then because you will need to debounce it and you'll need to know if it was pressed again or if it's still the same press from your previous loop iteration.

For debouncing I always use the integral method. Something like Kenneth's code from here: https://hackaday.com/2010/11/09/debounce-code-one-post-to-rule-them-all/