Educational materials

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

r1 - 28 Aug 2007 - 15:59:36 - AaronStriegel
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback
Syndicate this site RSSATOM