Writing key presses and time point of presses to file

1 vue (au cours des 30 derniers jours)
Helen Long
Helen Long le 2 Août 2018
Réponse apportée : Dennis le 2 Août 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.

Réponses (1)

Dennis
Dennis le 2 Août 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

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by