Implementing power on/off through gpio pins for mcu while asserting for few seconds
You could do it with a few passives, a flip-flop and a schmitt trigger, something like that:



When the switch is closed, the capacitor slowly charges. When the high-level transition input voltage of the schmitt trigger is reached (here at ~4s), the schmitt trigger buffer (you can use SN74xx1G17) output goes high, triggering the clock of the flip-flop (you can use SN74xx1G74), which then toggle its output. Note the D flip flop is configured with the inverting output connected to its D input, so it changes state at each clock rising edge.

When the button is released, the capacitor discharges (it takes a bit more time than the charge time due to R2) and you can then press again to toggle the output off.

To sum up, here, each time you press the button for longer than 4s, the "PWREN OUT" output will toggle (this can directly drive the enable input of a regulator), and you'll still be able to detect shorter button presses from the MCU with the "SW OUT" output.

The advantage of this solution is that it doesn't involve the software at all. So if there is a bug somewhere and the MCU hangs, you can still shut it down.

Be aware, however, that if the MCU is powered off, applying a voltage on the GPIO ("SW OUT" being high because the button is pressed) may damage it. Check the MCU ratings in the datasheet. To solve this, you can put a high-valued (100k or so) resistor in series with the "SW OUT" line (and eventually a diode that will clamp any voltage higher than the MCU supply voltage, but the MCU most likely has such diodes builtin).