Writing key presses and time point of presses to file

1 visualización (últimos 30 días)
Helen Long
Helen Long el 2 de Ag. de 2018
Respondida: Dennis el 2 de Ag. de 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.

Respuestas (1)

Dennis
Dennis el 2 de Ag. de 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

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by