Writing key presses and time point of presses to file

1 Ansicht (letzte 30 Tage)
Helen Long
Helen Long am 2 Aug. 2018
Beantwortet: Dennis am 2 Aug. 2018
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.

Antworten (1)

Dennis
Dennis am 2 Aug. 2018
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

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by