'Break loop using hotkey

There are hundreds of similar questions but none of them appear to be a solution in my case. My code is shaped in this way

def iterative_func():
    # do things
while True:
    iterative_func()

Then I would like it to stop when I press a hotkey let's say 'ctrl+k'. I tried pynput but the listener is not applicable since it waits for an Input and the rest of the script (iterative_func()) won't run, in my case the script should continuously run until I press some hotkey. Also the solution

while True:
    try:
        iterative_func()
    except KeyboardInterrupt:
        break

doesn't work for me (I don't know why, but maybe it's because I'm running VSCode), anyway it's not code I want to implement because the script will be deployed as a .exe file.

PS. I cannot import Key and Controller from pynput, it prompts an error and I have no clue on how to fix this so also solutions using these should be avoided.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source