[Scilab-users] cancelling a loop by key pressing Classic List Threaded 4 messages Open this post in threaded view
|

[Scilab-users] cancelling a loop by key pressing Dear all,out of curiosity I made a small animation ... see code below.Note:The part of the sound section is from a example where I generate different tones within one sound file.Hence the intervalls.... To the topic:The animation runs in a while loop.It is possible to stop the loop, by just closing the figure that displays the animation.However this is kind of crashing the program.Question: Is it possible to have a function within the loop to check if a specific key is pressed?And if this key is pressed, the control variable of the loop changes and the loop stops? Thank you,PhilippHere the code (Scilab 6.0.2)```clc; clear(); xdel(); // define the sound //set bit rate bitRate = 22050; // At first we create 0.05 seconds of sound parameters. t=soundsec(0.05, bitRate); [nr,nc]=size(t); // decide how many tones we have nTones = 1; // get the intervall size..must be an integer intervall = nc / nTones; // Then we generate the sound. for(i = 1:nTones) si = sin(2*%pi*(i*700)*t((i-1)*intervall+1 : i*intervall)); s1((i-1)*intervall+1 : i*intervall) = si; end for(i = 1:nTones) si = 0.5+sin(2*%pi*(i*800)*t((i-1)*intervall+1 : i*intervall)); s2((i-1)*intervall+1 : i*intervall) = si; end for(i = 1:nTones) si = 0.5+sin(2*%pi*(i*400)*t((i-1)*intervall+1 : i*intervall)); s3((i-1)*intervall+1 : i*intervall) = si; end for(i = 1:nTones) si = 0.5+sin(2*%pi*(i*600)*t((i-1)*intervall+1 : i*intervall)); s4((i-1)*intervall+1 : i*intervall) = si; end // create the scene xRange = linspace(0,99,100); yRange = linspace(0,49,100); xMin = min(xRange); xMax = max(xRange); yMin = min(yRange); yMax = max(yRange); rectW = xMax - xMin; rectH = yMax - yMin; //define the scene rectangle rectScene = [xMin,yMax, rectW, rectH]; // define a point center = start position xc = rectW / 2; yc = rectH / 2; // plot the scene f = figure(); f.background = 8; // plot the rectangle (scene field) xrect(rectScene); // plot the center plot(xc,yc,'o'); a = gca(); e = gce(); a.axes_visible = ["off","off","off"]; a.data_bounds = [xMin,yMin;xMax,yMax]; a.margins = [0.01, 0.01, 0.01, 0.01]; a.tight_limits = ["on","on","off"]; // animate the point as long as playGame = 1 playGame = 1; xDir = 0.5; // xDir < 0 = to the left; xDir > 0 = to the right yDir = -1; // yDir < 0 = falling; yDir >0 = raising speed = 1; while playGame == 1 if(xDir < 0) xc = xc-speed; else xc = xc+speed; end if(yDir < 0) yc = yc - speed; else yc = yc + speed; end e.children.data = [xc, yc]; // this is what happens when we reach left border if(xc<=xMin+1) playsnd(s1); xDir = 1; end // this is what happens when we reach right border if(xc>=xMax-1) playsnd(s2); xDir = -1; end // this is what happens when we reach lower border if(yc<=yMin+1) playsnd(s3); yDir = 1; end // this is what happens when we reach upper border if(yc>=yMax-1) playsnd(s4); yDir = -1; end sleep(1); // implement a event handle function ? // to check if (and if yes which) key has been pressed // eg.: if return value of event handle function = 0 // --> set playGame = 0; // --> thus stops the while - loop end``` _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

Re: cancelling a loop by key pressing Hello, > De : P M > Envoyé : jeudi 6 juin 2019 21:30 > > Question: > Is it possible to have a function within the loop to check if a specific key is pressed? I don't know about a keystroke but you may use a mouse click. https://help.scilab.org/docs/6.0.2/en_US/xclick.htmlRegards -- Christophe Dang Ngoc Chan Mechanical calculation engineer General This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users Correction: the function name is misleading, the xclick() function also takes keystroke into account. Regards > -----Message d'origine----- > De : users [mailto:[hidden email]] De la part de Dang Ngoc > Chan, Christophe > Envoyé : vendredi 7 juin 2019 09:06 > À : Users mailing list for Scilab <[hidden email]> > Objet : {EXT} Re: [Scilab-users] cancelling a loop by key pressing > > Hello, > > > De : P M > > Envoyé : jeudi 6 juin 2019 21:30 > > > > Question: > > Is it possible to have a function within the loop to check if a specific key is > pressed? > > I don't know about a keystroke but you may use a mouse click. > > https://help.scilab.org/docs/6.0.2/en_US/xclick.html> > Regards > > -- > Christophe Dang Ngoc Chan > Mechanical calculation engineer > > General > This e-mail may contain confidential and/or privileged information. If you are > not the intended recipient (or have received this e-mail in error), please > notify the sender immediately and destroy this e-mail. Any unauthorized > copying, disclosure or distribution of the material in this e-mail is strictly > forbidden. > _______________________________________________ > users mailing list > [hidden email] > https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Flists.sc> ilab.org%2Fmailman%2Flistinfo%2Fusers&data=02%7C01%7Cchristophe > .dang%40sidel.com%7C1916b3f039bf4329022308d6eb16b392%7C2390cbd1e6 > 634321bc93ba298637ce52%7C0%7C0%7C636954880069853058&sdata=Tx > a562IBIu8r1Jmy2vMKsFNFCXXpb%2BM%2ByNfVRUBK6PA%3D&reserve > d=0 This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users In reply to this post by der_Phil To stop animation when window is closed, don't use event_handler with ibut=-1000. Here is a simpler solution: ```h = gcf(); while %t if is_handle_valid(h) xtitle(msprintf("k = %d",k)) else break end k=k+1; end``` S. Le 06/06/2019 à 21:29, P M a écrit : Dear all, out of curiosity I made a small animation ... see code below. Note: The part of the sound section is from a example where I generate different tones within one sound file. Hence the intervalls....   To the topic: The animation runs in a while loop. It is possible to stop the loop, by just closing the figure that displays the animation. However this is kind of crashing the program. Question: Is it possible to have a function within the loop to check if a specific key is pressed? And if this key is pressed, the control variable of the loop changes and the loop stops? Thank you, Philipp Here the code (Scilab 6.0.2) ```clc; clear(); xdel(); // define the sound //set bit rate bitRate = 22050; // At first we create 0.05 seconds of sound parameters. t=soundsec(0.05, bitRate); [nr,nc]=size(t); // decide how many tones we have nTones = 1; // get the intervall size..must be an integer intervall = nc / nTones; // Then we generate the sound. for(i = 1:nTones) si = sin(2*%pi*(i*700)*t((i-1)*intervall+1 : i*intervall)); s1((i-1)*intervall+1 : i*intervall) = si; end for(i = 1:nTones) si = 0.5+sin(2*%pi*(i*800)*t((i-1)*intervall+1 : i*intervall)); s2((i-1)*intervall+1 : i*intervall) = si; end for(i = 1:nTones) si = 0.5+sin(2*%pi*(i*400)*t((i-1)*intervall+1 : i*intervall)); s3((i-1)*intervall+1 : i*intervall) = si; end for(i = 1:nTones) si = 0.5+sin(2*%pi*(i*600)*t((i-1)*intervall+1 : i*intervall)); s4((i-1)*intervall+1 : i*intervall) = si; end // create the scene xRange = linspace(0,99,100); yRange = linspace(0,49,100); xMin = min(xRange); xMax = max(xRange); yMin = min(yRange); yMax = max(yRange); rectW = xMax - xMin; rectH = yMax - yMin; //define the scene rectangle rectScene = [xMin,yMax, rectW, rectH]; // define a point center = start position xc = rectW / 2; yc = rectH / 2; // plot the scene f = figure(); f.background = 8; // plot the rectangle (scene field) xrect(rectScene); // plot the center plot(xc,yc,'o'); a = gca(); e = gce(); a.axes_visible = ["off","off","off"]; a.data_bounds = [xMin,yMin;xMax,yMax]; a.margins = [0.01, 0.01, 0.01, 0.01]; a.tight_limits = ["on","on","off"]; // animate the point as long as playGame = 1 playGame = 1; xDir = 0.5; // xDir < 0 = to the left; xDir > 0 = to the right yDir = -1; // yDir < 0 = falling; yDir >0 = raising speed = 1; while playGame == 1 if(xDir < 0) xc = xc-speed; else xc = xc+speed; end if(yDir < 0) yc = yc - speed; else yc = yc + speed; end e.children.data = [xc, yc]; // this is what happens when we reach left border if(xc<=xMin+1) playsnd(s1); xDir = 1; end // this is what happens when we reach right border if(xc>=xMax-1) playsnd(s2); xDir = -1; end // this is what happens when we reach lower border if(yc<=yMin+1) playsnd(s3); yDir = 1; end // this is what happens when we reach upper border if(yc>=yMax-1) playsnd(s4); yDir = -1; end sleep(1); // implement a event handle function ? // to check if (and if yes which) key has been pressed // eg.: if return value of event handle function = 0 // --> set playGame = 0; // --> thus stops the while - loop end``` ```_______________________________________________ users mailing list [hidden email] https://antispam.utc.fr/proxy/1/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/lists.scilab.org/mailman/listinfo/users ``` ```-- Stéphane Mottelet Ingénieur de recherche EA 4297 Transformations Intégrées de la Matière Renouvelable Département Génie des Procédés Industriels Sorbonne Universités - Université de Technologie de Compiègne CS 60319, 60203 Compiègne cedex Tel : +33(0)344234688 http://www.utc.fr/~mottelet``` _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users