How to use TouchDuration class of Input package

Best WinAppDriver code snippet using Input.TouchDuration

DoubleTap.cs

Source:DoubleTap.cs Github

copy

Full Screen

1using System;2using System.Collections;3using System.Collections.Generic;4using UnityEngine;5using UnityEngine.EventSystems;6using UnityEngine.UI;7public class DoubleTap : MonoBehaviour8{9 public Action onDoubleTap;10 [SerializeField] private LayerMask layerMask = default;11 [SerializeField] private GraphicRaycaster graphicRaycaster = default;12 private float touchDuration;13 private Touch touch;14 private EventSystem eventSystem;15 private int clickCount;16 private void Awake()17 {18 if (graphicRaycaster == null)19 {20 Debug.LogError("GraphicRaycaster is not assigned in the editor");21 enabled = false;22 return;23 }24 eventSystem = FindObjectOfType<EventSystem>();25 onDoubleTap += OnDoubleTap;26 }27 private void OnDestroy()28 {29 onDoubleTap -= OnDoubleTap;30 }31 private void OnDoubleTap()32 {33 PlanetsOverviewUIManager.Instance.ShowSolarSystemView();34 }35 private void Update()36 {37 if (Application.isEditor)38 {39 HandleEditorInput();40 return;41 }42 HandleTouchInput();43 }44 private void HandleEditorInput()45 {46 if (!Input.GetMouseButtonDown(0))47 {48 touchDuration = 0f;49 return;50 }51 // If we hit any UI then we do not need to make any further checks52 if (HitUI(Input.mousePosition))53 return;54 var ray = Camera.main.ScreenPointToRay(Input.mousePosition);55 var hits = Physics2D.RaycastAll(ray.origin, ray.direction, 100f, layerMask);56 if (hits.Length != 1)57 return;58 clickCount++;59 touchDuration += Time.deltaTime;60 if (clickCount == 1 && touchDuration < 0.2f)61 StartCoroutine(SingleOrDoubleEditor());62 }63 private void HandleTouchInput()64 {65 if (Input.touchCount <= 0)66 {67 touchDuration = 0f;68 return;69 }70 for (int i = 0; i < Input.touchCount; i++)71 {72 var touch = Input.GetTouch(i);73 if (touch.phase != TouchPhase.Began)74 continue;75 // If we hit any UI then we do not need to make any further checks76 if (HitUI(touch.position))77 return;78 var ray = Camera.main.ScreenPointToRay(Input.mousePosition);79 var hits = Physics2D.GetRayIntersectionAll(ray, 100f, layerMask);80 if (hits.Length != 1)81 return;82 touchDuration += Time.deltaTime;83 touch = Input.GetTouch(0);84 if (touch.phase == TouchPhase.Ended && touchDuration < 0.2f)85 StartCoroutine(SingleOrDoubleTouch());86 Debug.Log("Clicking on DoubleTapArea");87 }88 }89 private bool HitUI(Vector2 inputPos)90 {91 List<RaycastResult> results = new List<RaycastResult>();92 var pointerEventData = new PointerEventData(eventSystem);93 pointerEventData.position = inputPos;94 graphicRaycaster.Raycast(pointerEventData, results);95 return results.Count != 0;96 }97 // TODO: test this on mobile device98 private IEnumerator SingleOrDoubleTouch()99 {100 yield return new WaitForSeconds(0.3f);101 if (touch.tapCount == 1)102 Debug.Log("Single");103 else if (touch.tapCount == 2)104 {105 //this coroutine has been called twice. We should stop the next one here otherwise we get two double tap106 StopCoroutine(SingleOrDoubleTouch());107 Debug.Log("Double");108 }109 }110 private IEnumerator SingleOrDoubleEditor()111 {112 yield return new WaitForSeconds(0.3f);113 114 if (clickCount == 2)115 onDoubleTap?.Invoke();116 clickCount = 0;117 }118}...

Full Screen

Full Screen

InputHandler.cs

Source:InputHandler.cs Github

copy

Full Screen

1using UnityEngine;2using System.Collections;3public class InputHandler : AriaBehaviour4{5 private float touchDuration = 0;6 public Vector3 cameraRelativeDeltaPosition;7 public float touchSensitivity;8 public float tapSpeed;9 public bool move = false;10 public bool tap = false;11 #region Input stuff.12#if UNITY_EDITOR13 Vector2 originMousePosition;14 Vector2 currentMousePosition;15 Vector2 lastMousePosition;16 Vector2 deltaMousePosition;17#endif18#if (UNITY_IPHONE || UNITY_ANDROID) && !UNITY_EDITOR19 Vector2 originTouchPosition;20#endif21 #endregion22 void Start ()23 {24 }25 void Update ()26 {27 #region PC Input28#if UNITY_EDITOR29 if (Input.GetMouseButtonDown(0))30 {31 touchDuration = 0f;32 originMousePosition = currentMousePosition = lastMousePosition = Input.mousePosition;33 deltaMousePosition = Vector2.zero;34 }35 else if (Input.GetMouseButton(0))36 {37 touchDuration += Time.deltaTime;38 currentMousePosition = Input.mousePosition;39 deltaMousePosition = currentMousePosition - lastMousePosition;40 lastMousePosition = currentMousePosition;41 cameraRelativeDeltaPosition = screenXY2CameraXZ(deltaMousePosition);42 if(move == false && Vector2.Distance(currentMousePosition, originMousePosition) >= touchSensitivity)43 {44 move = true;45 }46 }47 else if (Input.GetMouseButtonUp(0))48 {49 if(move == false && touchDuration <= tapSpeed)50 {51 tap = true;52 }53 }54 else55 {56 tap = false;57 move = false;58 touchDuration = 0f;59 }60#endif61 #endregion62 #region Mobile Input63#if (UNITY_IPHONE || UNITY_ANDROID) && !UNITY_EDITOR 64 if (Input.touchCount > 0)65 {66 Touch touch = Input.GetTouch(0);67 if (touch.phase == TouchPhase.Began)68 {69 touchDuration = 0f;70 originTouchPosition = touch.position;71 }72 if (touch.phase == TouchPhase.Moved)73 {74 touchDuration += Time.deltaTime;75 cameraRelativeDeltaPosition = screenXY2CameraXZ(touch.deltaPosition);76 if (move == false && Vector2.Distance(touch.position, originTouchPosition) >= touchSensitivity)77 {78 move = true;79 }80 }81 if (touch.phase == TouchPhase.Canceled || touch.phase == TouchPhase.Ended)82 {83 if (move == false && touchDuration <= tapSpeed)84 {85 tap = true;86 }87 }88 }89 else90 {91 tap = false;92 move = false;93 touchDuration = 0f;94 }95#endif96 #endregion97 }98 Vector3 screenXY2CameraXZ(Vector2 deltaPos)99 {100 Vector3 cameraXZ = Vector3.zero;101 deltaPos *= 0.05f;102 Vector3 camRight = Camera.main.transform.TransformDirection(Vector3.right);103 Vector3 camForward = new Vector3(-camRight.z, 0, camRight.x);104 cameraXZ += deltaPos.x * camRight;105 cameraXZ += deltaPos.y * camForward;106 return cameraXZ;107 }108}...

Full Screen

Full Screen

DoubleTapHandler.cs

Source:DoubleTapHandler.cs Github

copy

Full Screen

1using System.Collections;2using System.Collections.Generic;3using UnityEngine;4using UnityEngine.EventSystems;5public class DoubleTapHandler : MonoBehaviour6{7 float touchDuration;8 Touch touch;9 void Update()10 {11 if (Input.touchCount > 0)12 { //if there is any touch13 touchDuration += Time.deltaTime;14 touch = Input.GetTouch(0);15 if (touch.phase == TouchPhase.Ended && touchDuration < 0.2f) //making sure it only check the touch once && it was a short touch/tap and not a dragging.16 StartCoroutine("singleOrDouble");17 }18 else19 touchDuration = 0.0f;20 }21 IEnumerator singleOrDouble()22 {23 yield return new WaitForSeconds(0.3f);24 if (touch.tapCount == 1)25 Debug.Log("Single");26 else if (touch.tapCount == 2)27 {28 //this coroutine has been called twice. We should stop the next one here otherwise we get two double tap29 StopCoroutine("singleOrDouble");30 Debug.Log("Double");31 RaycastHit hit;32 Ray ray = Camera.main.ScreenPointToRay(touch.position);33 if (Physics.Raycast(ray, out hit, Mathf.Infinity, LayerMask.GetMask("Environment")))34 {35 Vector3 position = hit.point;36 position.y = 1f;37 EventManager.instance.DoubleClickTrigger(position);38 }39 40 }41 }42}...

Full Screen

Full Screen

TouchDuration

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6{7 {8 static void Main(string[] args)9 {10 TouchDuration td = new TouchDuration();11 td.Start();12 Console.ReadKey();13 td.Stop();14 Console.WriteLine("Duration: {0}", td.Duration);15 Console.ReadKey();16 }17 }18}19using System;20using System.Collections.Generic;21using System.Linq;22using System.Text;23using System.Threading.Tasks;24{25 {26 static void Main(string[] args)27 {28 TouchDuration td = new TouchDuration();29 td.Start();30 Console.ReadKey();31 td.Stop();32 Console.WriteLine("Duration: {0}", td.Duration);33 Console.ReadKey();34 }35 }36}37using System;38using System.Collections.Generic;39using System.Linq;40using System.Text;41using System.Threading.Tasks;42{43 {44 static void Main(string[] args)45 {46 TouchDuration td = new TouchDuration();47 td.Start();48 Console.ReadKey();49 td.Stop();50 Console.WriteLine("Duration: {0}", td.Duration);51 Console.ReadKey();52 }53 }54}55using System;56using System.Collections.Generic;57using System.Linq;58using System.Text;59using System.Threading.Tasks;60{61 {62 static void Main(string[] args)63 {64 TouchDuration td = new TouchDuration();65 td.Start();66 Console.ReadKey();67 td.Stop();68 Console.WriteLine("Duration: {0}", td.Duration);69 Console.ReadKey();70 }71 }72}73using System;74using System.Collections.Generic;75using System.Linq;76using System.Text;77using System.Threading.Tasks;78{79 {80 static void Main(string[] args)81 {82 TouchDuration td = new TouchDuration();83 td.Start();84 Console.ReadKey();85 td.Stop();86 Console.WriteLine("Duration: {0}", td.Duration);87 Console.ReadKey();88 }89 }90}

Full Screen

Full Screen

TouchDuration

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Windows.Foundation;7using Windows.UI;8using Windows.UI.Input;9using Windows.UI.Xaml;10using Windows.UI.Xaml.Controls;11using Windows.UI.Xaml.Input;12using Windows.UI.Xaml.Media;13using Windows.UI.Xaml.Shapes;14{15 {16 Rectangle rect;17 public MainPage()18 {19 this.InitializeComponent();20 rect = new Rectangle();21 rect.Width = 100;22 rect.Height = 100;23 rect.Fill = new SolidColorBrush(Colors.Red);24 rect.PointerPressed += rect_PointerPressed;25 rect.PointerReleased += rect_PointerReleased;26 rect.PointerMoved += rect_PointerMoved;27 rect.PointerExited += rect_PointerExited;28 LayoutRoot.Children.Add(rect);29 }30 void rect_PointerExited(object sender, PointerRoutedEventArgs e)31 {32 rect.Fill = new SolidColorBrush(Colors.Red);33 }34 void rect_PointerMoved(object sender, PointerRoutedEventArgs e)35 {36 rect.Fill = new SolidColorBrush(Colors.Green);37 }38 void rect_PointerReleased(object sender, PointerRoutedEventArgs e)39 {40 rect.Fill = new SolidColorBrush(Colors.Yellow);41 }42 void rect_PointerPressed(object sender, PointerRoutedEventArgs e)43 {44 rect.Fill = new SolidColorBrush(Colors.Blue);45 }46 }47}

Full Screen

Full Screen

TouchDuration

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6{7 {8 static void Main(string[] args)9 {10 Console.WriteLine("Enter the number of Touches");11 int n = int.Parse(Console.ReadLine());12 var t = new TouchDuration();13 Console.WriteLine("Enter the duration of touches");14 for (int i = 0; i < n; i++)15 {16 t.AddTouch(int.Parse(Console.ReadLine()));17 }18 Console.WriteLine("The Average Touch Duration is {0}", t.AverageTouchDuration());19 Console.ReadKey();20 }21 }22}23using System;24using System.Collections.Generic;25using System.Linq;26using System.Text;27using System.Threading.Tasks;28{29 {30 static void Main(string[] args)31 {32 Console.WriteLine("Enter the number of Touches");33 int n = int.Parse(Console.ReadLine());34 var t = new TouchDuration();35 Console.WriteLine("Enter the duration of touches");36 for (int i = 0; i < n; i++)37 {38 t.AddTouch(int.Parse(Console.ReadLine()));39 }40 Console.WriteLine("The Average Touch Duration is {0}", t.AverageTouchDuration());41 Console.ReadKey();42 }43 }44}

Full Screen

Full Screen

TouchDuration

Using AI Code Generation

copy

Full Screen

1using UnityEngine;2using UnityEngine.InputSystem;3{4 void Update()5 {6 if (Input.touchCount == 1)7 {8 Touch touch = Input.GetTouch(0);9 if (touch.phase == TouchPhase.Began)10 {11 Debug.Log("Touch Started");12 }13 if (touch.phase == TouchPhase.Ended)14 {15 Debug.Log("Touch Ended");16 }17 }18 }19}20using UnityEngine;21using UnityEngine.InputSystem;22{23 void Update()24 {25 if (Input.touchCount == 1)26 {27 Touch touch = Input.GetTouch(0);28 if (touch.phase == TouchPhase.Began)29 {30 Debug.Log("Touch Started");31 }32 if (touch.phase == TouchPhase.Ended)33 {34 Debug.Log("Touch Ended");35 }36 }37 }38}39using UnityEngine;40using UnityEngine.InputSystem;41{42 void Update()43 {44 if (Input.touchCount == 1)45 {46 Touch touch = Input.GetTouch(0);47 if (touch.phase == TouchPhase.Began)48 {49 Debug.Log("Touch Started");50 }51 if (touch.phase == TouchPhase.Ended)52 {53 Debug.Log("Touch Ended");54 }55 }56 }57}58using UnityEngine;59using UnityEngine.InputSystem;60{61 void Update()62 {63 if (Input.touchCount == 1)64 {65 Touch touch = Input.GetTouch(0);66 if (touch.phase == TouchPhase.Began)67 {68 Debug.Log("Touch Started");69 }70 if (touch.phase == TouchPhase.Ended)71 {72 Debug.Log("Touch Ended");73 }74 }75 }76}77using UnityEngine;78using UnityEngine.InputSystem;79{

Full Screen

Full Screen

TouchDuration

Using AI Code Generation

copy

Full Screen

1using UnityEngine;2using UnityEngine.InputSystem;3{4 private void Update()5 {6 if (Input.GetTouch(0).phase == TouchPhase.Began)7 {8 Debug.Log("Touch Began");9 }10 else if (Input.GetTouch(0).phase == TouchPhase.Stationary)11 {12 Debug.Log("Touch Stationary");13 }14 else if (Input.GetTouch(0).phase == TouchPhase.Moved)15 {16 Debug.Log("Touch Moved");17 }18 else if (Input.GetTouch(0).phase == TouchPhase.Ended)19 {20 Debug.Log("Touch Ended");21 }22 else if (Input.GetTouch(0).phase == TouchPhase.Canceled)23 {24 Debug.Log("Touch Canceled");25 }26 }27}28using UnityEngine;29using UnityEngine.InputSystem;30{31 private void Update()32 {33 if (Touchscreen.current.primaryTouch.press.isPressed)34 {35 Debug.Log("Touch Pressed");36 }37 else if (Touchscreen.current.primaryTouch.press.wasPressedThisFrame)38 {39 Debug.Log("Touch Pressed This Frame");40 }41 else if (Touchscreen.current.primaryTouch.press.wasReleasedThisFrame)42 {43 Debug.Log("Touch Released This Frame");44 }45 }46}47using UnityEngine;48using UnityEngine.InputSystem;49{50 private void Update()51 {52 if (Touchscreen.current.primaryTouch.press.isPressed)53 {54 Debug.Log("Touch Pressed");55 }56 else if (Touchscreen.current.primaryTouch.press.wasPressedThisFrame)57 {58 Debug.Log("Touch Pressed This Frame");59 }60 else if (Touchscreen.current.primaryTouch.press.wasReleasedThisFrame)61 {62 Debug.Log("Touch Released This Frame");63 }64 }65}66using UnityEngine;67using UnityEngine.InputSystem;68{69 private void Update()70 {71 if (Touchscreen.current.primaryTouch.press.isPressed)72 {73 Debug.Log("Touch Pressed");74 }75 else if (Touchscreen.current

Full Screen

Full Screen

TouchDuration

Using AI Code Generation

copy

Full Screen

1using Input;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7using Windows.UI.Xaml.Controls;8using Windows.UI.Xaml.Input;9using Windows.UI.Xaml.Media;10using Windows.UI.Xaml.Shapes;11{12 {13 public static int GetDuration(DependencyObject obj)14 {15 return (int)obj.GetValue(DurationProperty);16 }17 public static void SetDuration(DependencyObject obj, int value)18 {19 obj.SetValue(DurationProperty, value);20 }21 DependencyProperty.RegisterAttached("Duration", typeof(int), typeof(TouchDuration), new PropertyMetadata(0));22 public static void OnDurationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)23 {24 var element = d as FrameworkElement;25 if (element != null)26 {27 element.PointerPressed += Element_PointerPressed;28 element.PointerReleased += Element_PointerReleased;29 }30 }31 private static void Element_PointerReleased(object sender, PointerRoutedEventArgs e)32 {33 var element = sender as FrameworkElement;34 var duration = (int)element.GetValue(DurationProperty);35 duration += (int)e.GetCurrentPoint(element).Properties.Pressure;36 element.SetValue(DurationProperty, duration);37 }38 private static void Element_PointerPressed(object sender, PointerRoutedEventArgs e)39 {40 var element = sender as FrameworkElement;41 var duration = (int)element.GetValue(DurationProperty);42 duration += (int)e.GetCurrentPoint(element).Properties.Pressure;43 element.SetValue(DurationProperty, duration);44 }45 }46}47using Input;48using System;49using System.Collections.Generic;50using System.Linq;51using System.Text;52using System.Threading.Tasks;53using Windows.UI.Xaml;54using Windows.UI.Xaml.Controls;55using Windows.UI.Xaml.Input;56using Windows.UI.Xaml.Media;57using Windows.UI.Xaml.Shapes;58{59 {60 public MainPage()61 {62 this.InitializeComponent();63 }64 private void Button_PointerPressed(object sender, PointerRoutedEventArgs e)65 {66 var duration = TouchDuration.GetDuration(sender as FrameworkElement);67 duration += (int)e.GetCurrentPoint(sender as FrameworkElement).Properties.P

Full Screen

Full Screen

TouchDuration

Using AI Code Generation

copy

Full Screen

1using UnityEngine;2using UnityEngine.InputSystem;3{4 private Touch touch;5 private float startTime, endTime;6 private void Update()7 {8 if (Input.touchCount > 0)9 {10 touch = Input.GetTouch(0);11 if (touch.phase == TouchPhase.Began)12 {13 startTime = Time.time;14 }15 if (touch.phase == TouchPhase.Ended)16 {17 endTime = Time.time;18 Debug.Log(endTime - startTime);19 }20 }21 }22}23using UnityEngine;24using UnityEngine.InputSystem;25{26 private Touch touch;27 private float startTime, endTime;28 private void Update()29 {30 if (Input.touchCount > 0)31 {32 touch = Input.GetTouch(0);33 if (touch.phase == TouchPhase.Began)34 {35 startTime = Time.time;36 }37 if (touch.phase == TouchPhase.Ended)38 {39 endTime = Time.time;40 Debug.Log(endTime - startTime);41 }42 }43 }44}45using UnityEngine;46using UnityEngine.InputSystem;47{48 private Touch touch;49 private float startTime, endTime;50 private void Update()51 {52 if (Input.touchCount > 0)53 {54 touch = Input.GetTouch(0);55 if (touch.phase == TouchPhase.Began)56 {57 startTime = Time.time;58 }59 if (touch.phase == TouchPhase.Ended)60 {61 endTime = Time.time;62 Debug.Log(endTime - startTime);63 }64 }65 }66}67using UnityEngine;68using UnityEngine.InputSystem;69{70 private Touch touch;71 private float startTime, endTime;72 private void Update()73 {74 if (Input.touchCount > 0)75 {76 touch = Input.GetTouch(0);77 if (touch.phase == TouchPhase.Began)78 {79 startTime = Time.time;80 }81 if (touch.phase == TouchPhase.Ended)82 {83 endTime = Time.time;84 Debug.Log(endTime - startTime);85 }86 }87 }88}

Full Screen

Full Screen

TouchDuration

Using AI Code Generation

copy

Full Screen

1using UnityEngine;2using UnityEngine.InputSystem;3{4 private Touch touch;5 private float startTime;6 private float endTime;7 private float timeHeld;8 private bool isHeld;9 void Update()10 {11 if (Input.touchCount > 0)12 {13 touch = Input.GetTouch(0);14 if (touch.phase == TouchPhase.Began)15 {16 startTime = Time.time;17 isHeld = true;18 }19 if (touch.phase == TouchPhase.Ended)20 {21 endTime = Time.time;22 timeHeld = endTime - startTime;23 isHeld = false;24 }25 }26 }27}28using UnityEngine;29using UnityEngine.InputSystem;30{31 private Touch touch;32 private float startTime;33 private float endTime;34 private float timeHeld;35 private bool isHeld;36 void Update()37 {38 if (Input.touchCount > 0)39 {40 touch = Input.GetTouch(0);41 if (touch.phase == TouchPhase.Began)42 {43 startTime = Time.time;44 isHeld = true;45 }46 if (touch.phase == TouchPhase.Ended)47 {48 endTime = Time.time;49 timeHeld = endTime - startTime;50 isHeld = false;51 }52 }53 }54}55using UnityEngine;56using UnityEngine.InputSystem;57{58 private Touch touch;59 private float startTime;60 private float endTime;61 private float timeHeld;62 private bool isHeld;63 void Update()64 {65 if (Input.touchCount > 0)66 {67 touch = Input.GetTouch(0);68 if (touch.phase == TouchPhase.Began)69 {70 startTime = Time.time;71 isHeld = true;72 }73 if (touch.phase == TouchPhase.Ended)74 {75 endTime = Time.time;76 timeHeld = endTime - startTime;77 isHeld = false;78 }79 }80 }81}82using UnityEngine;83using UnityEngine.InputSystem;84{

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run WinAppDriver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful