My code is for finding dwell(time to hold the key) and flight time(time interval between key released and next key press) when the user type a password/username.
function Keypress_set
x1=0;
x2=0;
x3=0;
f=0;
h = figure;
set(h,'KeyPressFcn',@KeyDownCb,'KeyReleaseFcn',@KeyUpCb)
tic;
function KeyDownCb(~,evnt)
out = sprintf('Key: %s\n',evnt.Key);
disp(out)
x1=toc;
f=x1-x2;
disp(x1)
disp(f)
end
function KeyUpCb(~,evnt)
out = sprintf('Key: %s\n',evnt.Key);
disp(out)
x2=toc;
disp(x2)
x3=(x2-x1);
disp(x3)
end
end
i want to write it in GUI and display f and x3 in different text box(i display key ,x1,x2 just to check the answer,not necessary). Please help me.I dont know the format propely for writing GUI.

 採用された回答

Geoff Hayes
Geoff Hayes 2014 年 6 月 17 日

0 投票

Similar to your code above, you will need to add two callbacks to the GUI. In the GUI editor, from the menu, select View-->View Callbacks and select KeyPressFcn and the function signature and empty body will be added to the m file for this GUI. Repeat this callback selection and choose KeyReleaseFcn. In your m file, you will now have something similar to
% --- Executes on key press with focus on figure1 and none of its controls.
function figure1_KeyPressFcn(hObject, eventdata, handles)
% --- Executes on key release with focus on figure1 and none of its controls.
function figure1_KeyReleaseFcn(hObject, eventdata, handles)
Now the code for each of these callbacks will be very similar to what you did already except that the f, x1 and x2 (and maybe x3) cannot be the local variables that you have defined above but will be members within the handles struct that you will update on each press and release. For example, the figure1_KeyPressFcn body may look something like
function figure1_KeyPressFcn(hObject, eventdata, handles)
out = sprintf('Key: %s\n', eventdata.Key);
disp(out);
% get and set the variables in the handles structure
handles.x1=toc;
handles.f=handles.x1-handles.x2;
% now save the data
guidata(hObject,handles);
% now display the data in the edit boxes
set(handles.editf,'String',num2str(handles.f));
set(handles.editx1,'String',num2str(handles.x1));
The above assumes that you have created two edit text boxes named editf and editx1. The figure1_KeyReleaseFcn would be written in a similar fashion. Note that you will have to initialize the f, x1 and x2 (and maybe x3) like you have but in the handles structure. This logic can be added to your figName_OpeningFcn function as
handles.f = 0;
handles.x1 = 0;
handles.x2 = 0;
handles.x3 = 0;
tic;
just prior to the guidata(hObject,handles) call within that function. The above initialization is just like yours, including the tic command too. Note that we call the guidata function to save your data to the handles structure.
Try the above and see what happens!

22 件のコメント

puja dutta
puja dutta 2014 年 6 月 17 日
編集済み: puja dutta 2014 年 6 月 17 日
Thanks a lot for a code.But i want to display f and x3 when i press key in another text field in a figure. in gui i will give text field for user to give password.when the user type password in a text field then it must show f and x3 for each key typed together in editf and editx3 and store the values permanently.Can i store in mat file or will need database.
Geoff Hayes
Geoff Hayes 2014 年 6 月 17 日
You could just store the data in an array and then write to file at some point (i.e. when the application closes, or have a separate button to "save" the data). A database isn't necessary.
puja dutta
puja dutta 2014 年 6 月 18 日
Thanks for the answer.How can that recorded data can be extracted later for further calculation??
Geoff Hayes
Geoff Hayes 2014 年 6 月 18 日
Similar to above, you would store this arrayed data in the handles structure prior to writing to a file (or not, and maybe you will just write to a file every time new data is recorded). Afterward, you can use MATLAB to read data from that file and perform whatever analysis you need.
puja dutta
puja dutta 2014 年 6 月 18 日
i made a code to display when i press 3 keys 3 times like abc 3 times and means of each coloumn.
function Keypress_set
d(3,3)=0;
fr(3,3)=0;
mfr(1,3)=0;
md(1,3)=0;
i=1;j=1; i1=1;j1=1; x1=0; x2=0; x3=0; f=0; h = figure; set(h,'KeyPressFcn',@KeyDownCb,'KeyReleaseFcn',@KeyUpCb) tic;
function KeyDownCb(~,evnt)
out = sprintf('Key: %s\n',evnt.Key);
disp(out)
x1=toc;
f=x1-x2;
fr(i,j)=f;
if j==3
j=1; i=i+1;
else j=j+1;
end
mfr=mean(fr,1);
disp(fr)
disp(mfr)
end
function KeyUpCb(~,evnt)
out = sprintf('Key: %s\n',evnt.Key);
disp(out)
x2=toc;
x3=(x2-x1);
d(i1,j1)=x3;
if j1==3
j1=1; i1=i1+1;
else j1=j1+1;
end
md=mean(d,1);
disp(d)
disp(md)
end
end
is that right??i dont know.please help me
if it is correct then how will i store it in file because i am not being able to store it.it displays the array every time i press the key but i just want to store the final array which i get after pressing eg: abc 3times
Geoff Hayes
Geoff Hayes 2014 年 6 月 19 日
puja - the above code is hard to follow in part because its formatting is skewed and because there is a lack of comments to indicate to the reader what each variable means or what is being attempted in the code. Please update it to make clear what is supposed to be happening.
If you want to store the results to file, then check out doc fopen, doc fprintf and doc fclose to open a file, write to a file, and close a file. Just type each of these bolded statements in the Command Window to view the documentation for each of these functions.
puja dutta
puja dutta 2014 年 6 月 20 日
編集済み: puja dutta 2014 年 6 月 20 日
When i press a key ,its f and x3 overwrites the previous f and x3.i want f and x3 for a password whose length is 3 and retype the password 3 times.when first time i type the password then the flight time for 3 key must come in one row of array.
puja dutta
puja dutta 2014 年 6 月 20 日
i tried a lot but sint able to make an array and store in a file.How can i write the values of f and x3 in array and write it in file.please help me
Geoff Hayes
Geoff Hayes 2014 年 6 月 20 日
Puja - Please see the attached file. It is similar to yours but I commented it and gave the variables names that were related to what they are supposed to record/do. It runs the same as before, it is just named DwellFlightCalculator. Try it out and see if it does what you want. It does assume that the password is only three characters long and that there are there password attempts.
I suggest that you put breakpoints in the code and step through it line by line to ensure that it makes sense to you.
puja dutta
puja dutta 2014 年 6 月 22 日
thanks a lot.I actually want this only.But can you explain how you compute flight time by
flightTimes(atPwdAttemptNum,numKeyDownEvents) = flightTime;
Geoff Hayes
Geoff Hayes 2014 年 6 月 22 日
flightTime is calculated in the if block just prior to this assignment
% if this is the first key down event, then there was no key
% pressed before, so the flight time is zero
if atPwdAttemptNum==1 && numKeyDownEvents==1
flightTime = 0;
else
% else a key has been released so we can compute the flight
% time
flightTime = toc;
end
The if atPwdAttemptNum==1 && numKeyDownEvents==1 checks to see if this is the first key pressed for the first password attempt. If this is true, then the flight time is zero because there was no key pressed before this one.
Else, a key was already pressed which resulted in the tic function being called (in the KeyUpCb function) so now we set the flight time as flightTime = toc; which would be the duration between the previous key up/release and the current key down.
Hope that clears it up!
puja dutta
puja dutta 2014 年 6 月 23 日
Yea thanks a lot its clear now.Like mean i have calculated standard deviation using std command .Now i also want to store a threshold with mean and standard deviation.Can you please tell how can i compute threshold using mean and standard deviation.
Geoff Hayes
Geoff Hayes 2014 年 6 月 23 日
Puja - given that you are the author of your software and know what the intent of it is (to calculate the dwell and flight times, the means and standard deviations) I think that you are better situated to determine what the threshold should be…based on how you intend to use it. I don't have that information or insight into the purpose of your software. Good luck!
puja dutta
puja dutta 2014 年 6 月 24 日
Thanks a lot :-)
sumi Gogoi
sumi Gogoi 2014 年 6 月 25 日
I am getting problem in reading the dwellflightStats.txt file.I want to extract mean of dwell and flight from that file. can you please help me I also tried by writing the mean variable in mat file save('puja.mat','meanDwelltime'); it is created but i cant open it and when i write in new m file and how can i read the array in another new m file
puja dutta
puja dutta 2014 年 6 月 25 日
Geoff-Can you please help me.Actually i add
Username = input('Username: ', 's');
Password = input('Password: ', 's');
in starting of the code you gave.When i run it,I type 'Username' and 'Password'.After which i press key 4 times for which the code works.It works if i press any key.I need that it must work when it matches the character of Password. or is there any way that evnt.Key works for Keys written in Password only and not just any key
please help me.
Geoff Hayes
Geoff Hayes 2014 年 6 月 26 日
Puja - if you want to record the dwell and flight times when the user enter his/her password only, then set the figure handle to capture the key press event AFTER the user name has been entered but before the password is entered:
Username = input('Username: ', 's');
set(h,'KeyPressFcn',@KeyDownCb,'KeyReleaseFcn',@KeyUpCb) ;
Password = input('Password: ', 's');
If you want to remove the callbacks then do something like
set(h,'KeyPressFcn','','KeyReleaseFcn','') ;
crystal dsouza
crystal dsouza 2014 年 6 月 26 日
I calculated dwell time and flight time for e attempt of password and then read the mean dwell time from a file.I want to find distance between them using manhattan distance metric.I tried every syntax but it just showing error
% Calculates the dwell and flight times for key pressing. function DwellFlightAuthentication
% password length
pwdLength = 3;
% password attempts
pwdAttempts = 1;
% the dwell time is the absolute time difference between when the
% key is pressed and that same key is released
dwellTimes = zeros(pwdAttempts,pwdLength);
% the flight time is the time from releasing a key and pressing a
% subsequent key
flightTimes = zeros(pwdAttempts,pwdLength);
Username=input('username: ','s');
h = figure;
set(h,'KeyPressFcn',@KeyDownCb,'KeyReleaseFcn',@KeyUpCb) ;
tic;
numKeyDownEvents = 0;
numKeyUpEvents = 0;
atPwdAttemptNum = 1;
function KeyDownCb(~,evnt)
if atPwdAttemptNum<=pwdAttempts
numKeyDownEvents = numKeyDownEvents + 1;
fprintf('(%d,%d) Key: %s\n',atPwdAttemptNum,numKeyDownEvents,evnt.Key);
% if this is the first key down event, then there is no was no key
% pressed before, so the flight time is zero
if atPwdAttemptNum==1 && numKeyDownEvents==1
flightTime = 0;
else
% else a key has been released so we can compute the flight
% time
flightTime = toc;
end
% record the flight time
flightTimes(atPwdAttemptNum,numKeyDownEvents) = flightTime;
% start the "timer" for the dwell time
tic;
end
end
but it showing error
subscript indices must be either be real positive integer or logical
please help me.Its really urgent and important
function KeyUpCb(~,~)
if atPwdAttemptNum<=pwdAttempts
numKeyUpEvents = numKeyUpEvents + 1;
% since a key up follows a key down then we can compute the dwell
% time
dwellTime = toc;
% record the dwell time
dwellTimes(atPwdAttemptNum,numKeyUpEvents) = dwellTime;
% move to the next password attempt if the number of key ups is
% the same size as the password length
if numKeyUpEvents==pwdLength
atPwdAttemptNum = atPwdAttemptNum + 1;
numKeyUpEvents = 0;
numKeyDownEvents = 0;
% display all results to the console
disp('Dwell times:');
disp(dwellTimes);
disp('Flight times:');
disp(flightTimes);
A=dlmread('puja.mat')
z(i,:)=sum(abs(dwellTimes-A))
end
end
end
end
end
puja dutta
puja dutta 2014 年 6 月 27 日
Geoff-i tried using
Username=input('Username: ','s');
set(h,'KeyPressFcn',@KeyDownCb,'KeyReleaseFcn',@KeyUpCb) ;
Password=input('Password: ','s' );
but it shows error like
Undefined function or variable "atPwdAttemptNum".
Error in DwellFlightCalculator/KeyUpCb
if atPwdAttemptNum<=pwdAttempts
Error using input
Error while evaluating figure KeyReleaseFcn
I have no idea what to do.Please help me
Geoff Hayes
Geoff Hayes 2014 年 6 月 27 日
Puja - the lines
Username = input('Username: ', 's');
set(h,'KeyPressFcn',@KeyDownCb,'KeyReleaseFcn',@KeyUpCb) ;
Password = input('Password: ', 's');
was just an example of the ordering needed to prevent the key press and release callbacks from firing when the user entered his/her username. You must still account for the other code in the file that deals with the initialization of local variables (like atPwdAttemptNum) that must occur before the user user starts entering his/her password.
puja dutta
puja dutta 2014 年 6 月 28 日
Geoff- The code you gave DwellFlightCalculator.m starts the working after a key is pressed.How can i make the working after each password attempt
I mean like the flight time must come in the way(just an example)
0 0.0700 0.0695 0 0.0747 0.0767 0 0.695 0.0712
Please help me
Geoff Hayes
Geoff Hayes 2014 年 6 月 28 日
Puja - the answer to that is in my previous two comments. I urge you to put a breakpoint in the code and step through it and see why it is not working and try to understand what the code is doing and what the error message (if any) means and how to address it.

サインインしてコメントする。

その他の回答 (1 件)

puja dutta
puja dutta 2014 年 6 月 17 日

0 投票

This is so embarrassing . I got an email that john has answered to my question but here i am not being able to see any answer.Its really urgent,i need the answer please help me.

2 件のコメント

ES
ES 2014 年 6 月 17 日
What all do you want to display? Which of them are dynamic? when should the function be called (eg., on clicking a button in the GUI etc)?
puja dutta
puja dutta 2014 年 6 月 18 日
i want to display f and x3.the function should be called when i enter some key in a text field which i will name as password.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLive Scripts and Functions についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by