![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1669006/image.png)
How to start recording by pressing a certain key, without involving figures?
8 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Pratyush Swain
2024-4-15
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1669006/image.png)
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:
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!