Keypress and release function

101 views (last 30 days)
Christopher
Christopher on 1 Jun 2013
Commented: Walter Roberson on 16 Feb 2020
So i am writing a program which, uses the arrow keys to control an item on an axis. Within a loop i have used a range of if and elseif statments to say if key press equal uparrow then some value changes. The problem is upon key release the code still recognises the button as being pressed. This is my code thus far for the recognition of the key press:
Key=get(gcf,'CurrentKey');
%using strcmp for string comparison if comparison is true = 1
if strcmp(num2str(Key),'')==1
%If up arrow is pressed thrust = 1
elseif strcmp(num2str(Key),'uparrow')==1
Thrust=0.04;
end
Any ideas how I could make it only work for when the key is pressed? Also i found this code
% a simple exemplary callback command
com=['disp([get(gcf,''currentkey''),',...
'get(gcf,''currentmodifier'')]);'];
fign='foo';
fh=figure('name',fign,'numbertitle','off');
set(fh,'keypressfcn',com);
set(fh,'windowbuttondownfcn',com);
% ...now
import com.mathworks.mde.desk.*;
desk=MLDesktop.getInstance;
fj=desk.getClient(fign);
set(fj,'keyreleasedcallback','disp(''RELEASE'')')
set(fj,'keypressedcallback','disp(''PRESSED'')')
It seems to do what I want but I'm not sure if I could manipulate it into an if statement. Thanks in advance

Answers (1)

Walter Roberson
Walter Roberson on 1 Jun 2013
There is no CurrentKey property of figures: there is a CurrentCharacter property. That property reflects the last key pressed . There is no timeout on that information, so if you last pressed a key 6.28 days ago, that key information will still be returned. CurrentCharacter has no information about whether a key has been released or not.
When it comes to the choice of polling (checking in a loop to see if something has happened yet) or interrupting (arrange to have the system call a routine when the system detects the event has happened), then the rule of thumb is:
Do not poll. Not unless:
(A) the hardware or software does not support polling;
or (B) the required response time can be achieved in a loop and is meaningfully lower than the interrupt response time.
There have been some architectures where interrupts are comparatively costly. This is not an issue with MATLAB itself, in that if you cannot live with the polling interrupt response time you get in MATLAB, then you are using the wrong platform for your task, much the same way as it would be improper to use a 10 tonne truck in circumstances where you need to weave around closely-spaced obstacles at high speed (e.g., use a motorcycle instead)
  4 Comments
Karsten Schulz
Karsten Schulz on 15 Feb 2020
dosen't work for all scripts here:
myfig = figure(); %need graphics to use keypress / release
set(myfig, 'UserData', []);
set(myfig, 'KeyPressCallback',@(src,evt) set(ancestor(src,'figure'),'UserData', struct('key', get(ancestor(src,'figure'),'CurrentCharacter'),'mod', get(ancestor(src,'figure'), 'CurrentModifier') ) );
set(myfig, 'KeyReleasedCallback', @(src,evt) set(ancestor(src,'figure'),'UserData', []));
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
Walter Roberson
Walter Roberson on 16 Feb 2020
set(myfig, 'KeyPressCallback',@(src,evt) set(ancestor(src,'figure'),'UserData', struct('key', get(ancestor(src,'figure'),'CurrentCharacter'),'mod', get(ancestor(src,'figure'), 'CurrentModifier'))));

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!