MATLAB: Create keyboard listener to interrupt a running function

eventkeyboardlistenerMATLAB

Hello,
I am working on a project in which I have to interrupt a running function if the user press the Escape button of the keyboard. I explain : the running function is in two parts and between them there is (for now) a pause of 5 seconds. But, what I want is replace my pause by a while function which end after 5 seconds or if the user press the escape button (and in this case I want to end the running function to not execute second part of the code).
I saw it deals with listener but I don't understand how to implement this without create a figure which disturb me because I have no GUI open.
Can you help me please to understand how to solve my problem? I thank you in advance !
Related Question