DroidScript wiki

(was AndroidScript) unofficial documentation by the community

User Tools

Site Tools


sample_code:simulate_touch

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sample_code:simulate_touch [2014/12/21 16:06] (current)
octazid created
Line 1: Line 1:
 +====== Shows how to use the SimulateTouch method ======
 +Code from the DroidScript-Beta forum posted by Dave Smart.
  
 +Here is an example which automatically keeps pressing a button forever
 +(Note you can simulate "​Up",​ "​Down"​ and "​Move"​ events)
 +<code JavaScript Simulattouch.js>​
 +//Called when application is started.
 +function OnStart()
 +{
 + //Create a layout with objects vertically centered.
 + lay = app.CreateLayout( "​linear",​ "​VCenter,​FillXY"​ );
 +
 + //Create image 1/5 of screen width and correct aspect ratio.
 + img = app.CreateImage( "/​Sys/​Img/​Hello.png",​ 0.2, -1 );
 + lay.AddChild( img );
 +
 + //Create a button 1/3 of screen width and 1/10 screen height.
 + btn = app.CreateButton( "Press Me", 0.3, 0.1 );
 + btn.SetMargins( 0, 0.05, 0, 0 );
 + lay.AddChild( btn );
 +
 + //Set function to call when button pressed.
 + btn.SetOnTouch( btn_OnTouch );
 +
 + //Add layout to app.
 + app.AddLayout( lay );
 +}
 +
 +//Called when user touches our button.
 +function btn_OnTouch()
 +{
 +    app.Vibrate( "​0,​100,​30,​100,​50,​300"​ );
 +    setTimeout( SimTouch, 1000 );
 +}
 +
 +function SimTouch()
 +{
 +    //Method1
 +    app.SimulateTouch( btn );
 +    ​
 +    //Method2
 +    /*
 +    app.SimulateTouch( btn, 0.5, 0.5, "​Down"​ );
 +    app.Wait( 0.1 );
 +    app.SimulateTouch( btn,  0.5,  0.5, "​Up"​ );
 +    */
 +}
 +</​code>​
sample_code/simulate_touch.txt ยท Last modified: 2014/12/21 16:06 by octazid