I suspect that you are getting synchronous code (e.g. the evaluation of a script or function) mixed up with asynchronous code (e.g. callbacks and events triggered by GUI interactions).
To get the "pressed key" character ultimately means that you are waiting for an event to occur in GUI, which is naturally best processed within the GUI itself (as callbacks are by their very nature designed to work with asynchronous code evaluation). If you want that character returned by the function then you can use WAITFOR to wait until the user closes the figure, and works without error:
function out = pressedKey()