How to start recording by pressing a certain key, without involving figures?

7 views (last 30 days)
I'm collecting frames from a camera, trying to start and stop the data recording by pressing certain keys (eg. once I press "b", the recording starts). However, the functions I found until now, such as WindowKeyPressFcn and waitforbuttonpress both involves a figure, which is not part of my recording. I also tried getkey, which didn't seem to work.
Does anyone have ideas about how I can get to my aim without creating an extra figure? Your help is very appreciated!
My previous attempt is to use userInput and control the recording by typing "start" and "stop", but it apperently also waits for user input to record any frames, hence 0 frame was recorded when I stop it.
fprintf('Recording %s. Type "start" to begin recording.\n', taskname);
while true
userInput = input('', 's');
if strcmpi(userInput, 'start')
...
tic;
fprintf('Recording in progress. Type "stop" to end recording.\n');
while true
userInput = input('', 's');
if strcmpi(userInput, 'stop')
break;
end
...
s = toc;
fprintf('Recording stopped for %s.\n', taskname);
fprintf('%d frames\n', frames_num);
fprintf('%f seconds\n', s);
fprintf('%f fps\n', frames_num / s);
...
break;
else
fprintf('Invalid input. Type "start" to begin recording.\n');
end
end

Answers (1)

Pratyush Swain
Pratyush Swain on 15 Apr 2024
Hi Julia,
I see that you are looking for a non-blocking or non-waiting approach to start and stop your data recording process and you do not want to involve any figures.
One workaround for this issue would to use a library for detecting keyboard inputs:
You can refer to Matlab Input library fro keyboard from HebiRobotics on File Exchange: https://in.mathworks.com/matlabcentral/fileexchange/61306-hebirobotics-matlabinput.
It can be used in the following manner :
% Initialize Object for HebiKeyboard %
kb = HebiKeyboard();
while true
% Read keystroke %
state = read(kb);
fprintf('Waiting for Input. Hit SPACE to start recording . \n');
% If pressed button is SPACE - start recording %
if state.SPACE
tic;
fprintf('Recording in progress. Hit ESC to end recording.\n');
% Once recording is in progess, exit only on ESC key %
while true
state = read(kb);
% Exit Recording on ESC Key %
if state.ESC
disp("RECORDING STOPS");
break;
end
end
% Exit while loop once recording process is done %
s = toc;
break;
end
end
The whole process is non-blocking and doesn't require creation/focus on any particular figure window. In the above demonstration I have assumed "SPACE" and "ESC" buttons for starting and stopping the recording process, the state struct also has 12 options for checking different key strokes as follows:
You can also use other keyboard characters in the following manner:
% Checking for 'b' to start recording - as mentioned in your query %
if find(state.keys('b'))
% Recording in now in progress, proceed to next steps.
...
end
The installation for the same is also fairly simple:
1- Download latest source file(zip/tar) from https://github.com/HebiRobotics/MatlabInput/releases
2- Extract the folder containing the files and do add to path (including folder & subfolders) on MATLAB Desktop.
3- Use HebiKeyboard() in your code file.
For more information on HebiKeyboard Library, please refer to https://github.com/HebiRobotics/MatlabInput.
For other alternatives on Non-blocking keyboard reads, you can refer to:
Hope this helps.

Categories

Find more on Desktop in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!