time user input as soon as they start typing
Show older comments
I'm relatively new to matlab and am struggling to find a means to use a timer to measure the time it takes for a user to type their input in as soon as they start typing. I've looked at using "tic toc" with no luck and sinestream. Would very much appreciate any hints or pointers cheers!
Answers (2)
per isakson
on 24 Jan 2014
Edited: per isakson
on 26 Jan 2014
0 votes
Comments
- timer is not appropriate for this job
- tic, str = user_inputs_data; user_time = toc; should work
How does the user input the data?
.
[Later]
Comments
- this is far from trivial to do with matlab
- there has been discussions on how to check the user input while the user is typing. AFAIK: there is no good solution with plain Matlab
- I think it is possible to achieve with the callbacks of figure: KeyPressFcn etc. [It's not possible when the user types in a uicontrol('Style','Edit').]
- but first you should see Editbox data input validation
[26 Jan]
- I'm convinced that with plain Matlab it is not possible to measure the elapsed time from the user's first keystroke to the last of input('iput satetment','s'). The property, KeyPressFcn, does not make it. Neither that of figure nor that of uicontrol('Style','Edit'). Prove me wrong!
- A low level hack using GUI automation using a Robot might be possible. And search for java.awt.robot at that site.
- You might be able to find a solution based on an actxcontrol (Create Microsoft ActiveX control in figure window) or Java Matlab-Java book
- You might want to read about the HG2 update
W. Owen Brimijoin
on 24 Jan 2014
Per is exactly right, this sort of a question calls for the use of the KeyPressFcn.
The idea is to make a figure that waits for key presses. When the first key is pressed it calls a function that checks what time it is and updates a variable. Afterwards the function ignores any key except 'enter', which causes it to compare the current time to the time the first key was pressed.
If you save the following code as a single function then you should get the behaviour you are looking for:
function keyboard_timer
global typing_detected
typing_detected = 0;
h1 = figure;
set(h1,'KeyPressFcn',@detect_key_event)
function detect_key_event(hObject,~)
global typing_detected
key = get(hObject,'CurrentCharacter'); %get the key that was pressed
switch double(key),
case 13 %this is the ASCII code for the 'enter' key.
elapsed = rem(now - typing_detected,1)*216000; %compare timestamps!
typing_detected = 0;%reset the global variable
display(['Elapsed Time was ',num2str(elapsed)]) %here's the answer
otherwise %any other key triggers this, and if it's the first checks the time
if typing_detected==0,typing_detected = now;end
end
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!