Writing key presses and time point of presses to file

1 view (last 30 days)
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.

Answers (1)

Dennis
Dennis on 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

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!