Writing key presses and time point of presses to file

조회 수: 2 (최근 30일)
Helen Long
Helen Long 2018년 8월 2일
답변: Dennis 2018년 8월 2일
Can anyone share a script that can do the following: Start timer and whenever I press a key (typically the number keys 1-0), I'd like the script to write the key label and time point that the key was pressed to a file. If possible, the total amount of each key presses also calculated.

답변 (1개)

Dennis
Dennis 2018년 8월 2일
This should work for numbers and letters, avoid space,cr,tab ...
It also overwrites your old file if you don't change the filename (first line).
outfile='test.txt';
handles.f=figure;
handles.time=tic;
handles.fid=fopen(outfile,'w');
handles.counter=uicontrol('style','text','string','0');
handles.f.KeyPressFcn={@recordkey,handles};
handles.f.DeleteFcn={@fidcl,handles.fid};
function recordkey(~,~,handles)
counter=str2double(handles.counter.String);
handles.counter.String=num2str(counter+1);
key=handles.f.CurrentCharacter;
time=toc(handles.time);
fprintf(handles.fid,'%.4f \t %c \n',time,key);
end
function fidcl(~,~,fid)
fclose(fid)
disp('Done')
end

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by