Force Quit Button in PsychToolbox

42 visualizaciones (últimos 30 días)
KYLE CRAWSHAW
KYLE CRAWSHAW el 3 de Mzo. de 2022
Respondida: Abhijeet el 9 de En. de 2024
Hi,
I am creating a study for uni using PyschToolbox and would like an option for the participant to press escape to exit the study at any time during the study and it show the message "Thank you for your time".
I've tried messing around with if statements but I don't really know what i'm doing. I have attached the code I have so far if someone could help?
Thanks in advance
ListenChar(2); %stop input into MATLAB window
%generate random ID for ppts
a = [randperm(10,2)+47;randperm(26,2)+96];
b = char(a(:)');
c = convertCharsToStrings(b);
ScreenNumbers = Screen('Screens'); %find screens
OutScreen = max(ScreenNumbers); %output to max screen
%Screen('Preference', 'SkipSyncTests', 1); %remove
[scrn, rect] = PsychImaging('OpenWindow',OutScreen, [],[0 0 800 600])
Screen('TextSize',scrn,24); %Text Size
DrawFormattedText(scrn,['Thank you for taking part in this experiment. \n\n Please take time to read the information infront of you. \n\n Do not hesitate to ask any questions. \n\n Press any key to continue.'],'center','center');
Screen('flip', scrn); %Show Text on Display
KbStrokeWait; %wait for any keypress, [],[0 0 800 600]
DrawFormattedText(scrn, [b], 'center', 'center')
DrawFormattedText(scrn, ['\n\n\n\n\n\n\n\n Your Unique Code is Displayed Above, \n\n Please Take Note of This. \n\n Press Any Key to Continue'], 'center', 'center');
Screen('flip', scrn); %Show text
KbStrokeWait; %wait for any keypress
DrawFormattedText(scrn,['You are about to be presented with a video. \n\n Please pay attention as you will be asked to recall events afterwards. \n\n When you are ready, press any key to play'], 'center', 'center');
Screen('flip', scrn); %Show text
Clip_1 = [PsychtoolboxRoot 'PsychDemos/MovieDemos/8.1.mov'];
% Wait until user releases keys on keyboard:
KbStrokeWait;
% Select screen for display of movie:
screenid = max(Screen('Screens'));
% Open movie file:
[movie] = Screen('OpenMovie', scrn, Clip_1);
% Start playback engine:
Screen('PlayMovie', movie, 1);
% Playback loop: Runs until end of movie or keypress:
while ~KbCheck
% Wait for next movie frame, retrieve texture handle to it
tex = Screen('GetMovieImage', scrn, movie);
% Valid texture returned? A negative value means end of movie reached:
if tex<=0
% We're done, break out of loop:
break;
end
% Draw the new texture immediately to screen:
Screen('DrawTexture', scrn, tex);
% Update display:
Screen('Flip', scrn);
% Release texture:
Screen('Close', tex);
end
% Stop playback:
Screen('PlayMovie', movie, 0);
% Close movie:
Screen('CloseMovie', movie);
DrawFormattedText(scrn,['Please press and hold space for how long you believe the video lasted. '],'center','center');
Screen('flip', scrn); %Show Text on Display
KbTriggerWait(KbName(['space']));
tic;
KbReleaseWait;
firstResponse = toc;
DrawFormattedText(scrn,['+'],'center','center');
Screen('flip', scrn); %Show Text on Display
WaitSecs(1);
DrawFormattedText(scrn,['The next video will play now. '],'center','center');
Screen('flip', scrn); %Show Text on Display
WaitSecs(5);
Clip_2 = [PsychtoolboxRoot 'PsychDemos/MovieDemos/8.1.mov'];
% Select screen for display of movie:
screenid = max(Screen('Screens'));
% Open movie file:
[movie] = Screen('OpenMovie', scrn, Clip_2);
% Start playback engine:
Screen('PlayMovie', movie, 1);
% Playback loop: Runs until end of movie or keypress:
while ~KbCheck
% Wait for next movie frame, retrieve texture handle to it
tex = Screen('GetMovieImage', scrn, movie);
% Valid texture returned? A negative value means end of movie reached:
if tex<=0
% We're done, break out of loop:
break;
end
% Draw the new texture immediately to screen:
Screen('DrawTexture', scrn, tex);
% Update display:
Screen('Flip', scrn);
% Release texture:
Screen('Close', tex);
end
% Stop playback:
Screen('PlayMovie', movie, 0);
% Close movie:
Screen('CloseMovie', movie);
DrawFormattedText(scrn,['Please press and hold space for how long you believe the video lasted. '],'center','center');
Screen('flip', scrn); %Show Text on Display
KbTriggerWait(KbName(['space']));
tic;
KbReleaseWait;
secondResponse = toc;
DrawFormattedText(scrn,['+'],'center','center');
Screen('flip', scrn); %Show Text on Displayb
WaitSecs(1);
DrawFormattedText(scrn,['Thank You'],'center','center');
Screen('flip', scrn); %Show Text on Display
T = table(c, firstResponse, secondResponse);
writetable(T,'Data.csv','WriteMode','Append','WriteRowNames',true)
KbStrokeWait;% wait for any keypress
Screen('CloseAll');
ListenChar(1); %Reinstate ability to type in MATLAB

Respuestas (1)

Abhijeet
Abhijeet el 9 de En. de 2024
Hi Kyle,
I understand that you want to create a dialog box which opens on pressing escape key and exits the study in ‘PyschToolbox’.
Please refer to the following sample code for creating a dialog box for this particular use case :-
% Assuming you have initialized Psychtoolbox and have a window pointer named 'window'
% Define the key you want to check for (Escape in this case)
escapeKey = KbName('ESCAPE');
% Start your experiment loop
while true % or some condition related to your study
% Other experiment code goes here
% Check for keyboard input
[keyIsDown, ~, keyCode] = KbCheck;
% If a key was pressed and it's the Escape key, exit the loop
if keyIsDown
if keyCode(escapeKey)
% Display 'Thank you for your time' message
DrawFormattedText(window, 'Thank you for your time', 'center', 'center', [255 255 255]);
Screen('Flip', window);
% Wait a second for the participant to read the message
WaitSecs(1);
% Break out of the loop, ending the experiment
break;
end
end
% Other experiment code goes here
end
% Close the Psychtoolbox window and clean up
Please refer to the following matlab answer which is similar to your query :-
I hope this resolves the issue you were facing.

Categorías

Más información sobre Timing and presenting 2D and 3D stimuli en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by