MATLAB Example Code - keyHandlerPress
The keyHandlerPress function is the handler for when keys are pressed in the MATLAB window. Once registered, this function will be called each time a key is pressed. For simplicity, the function simply adjusts a global variable to denote what key is currently being pressed.
It is important to note that the currentKey attribute of the window from the get function will not change to a blank or null value when a key is released. The key press and key release routines are critical to correctly detect keystrokes.
Examples
Register the key press handler for the current figure (GUI/window)
set(gcf,'KeyPressFcn','keyHandlerPress');
Source Code
function keyHandlerPress(src,evnt)
% usage: keyHandlerPress(src, evnt)
% purpose: Sets the global variable to reflect the current key press
global gCurrentKeyPress
%disp('Invoked key handler');
gCurrentKeyPress = get(gcf,'CurrentCharacter');
% disp(y);
% disp(evnt.Character);
end
Other Notes
- Keystrokes are not queued, only the current keystroke is recorded.
Related Code / Code Dependencies