# Touch screen programming

**URL:** https://discussions.unity.com/t/touch-screen-programming/669874
**Category:** Scripting
**Tags:** Scripting
**Created:** [July 4, 2017, 4:43pm UTC](https://discussions.unity.com/t/touch-screen-programming/669874 "2017-07-04T16:43:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![AlexMagnei](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/alexmagnei/32/296286_2.png) [@AlexMagnei](https://discussions.unity.com/u/AlexMagnei)
#### Post date: [July 4, 2017, 4:43pm UTC](https://discussions.unity.com/t/touch-screen-programming/669874/1 "2017-07-04T16:43:57Z")

</div>

Hello there, I recently passed my Computer Games Development degree at University and I’m looking to expand upon what I learned during the course. During the course I used Unreal Engine, so I am new to Unity. I would like to know how to set up touch screen buttons that react in the same way as keyboard keys do, such that instead of pressing the Up Arrow on the keyboard, I can click on an up button displayed on the screen. I’ve looked at Unity’s tutorials and cannot find any on this topic. Thanks in advance.

Alex

---

<div class="post-metadata">

### Author: ![radwan92](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@radwan92](https://discussions.unity.com/u/radwan92)
#### Post date: [July 4, 2017, 4:54pm UTC](https://discussions.unity.com/t/touch-screen-programming/669874/2 "2017-07-04T16:54:58Z")

</div>

I think that Unity’s [standard assets](https://docs.unity3d.com/Manual/HOWTO-InstallStandardAssets.html) (precisely _CrossPlatformInput_) is a great place to start.

---

<div class="post-metadata">

### Author: ![Vipsu](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/vipsu/32/438146_2.png) [@Vipsu](https://discussions.unity.com/u/Vipsu)
#### Post date: [July 4, 2017, 7:06pm UTC](https://discussions.unity.com/t/touch-screen-programming/669874/3 "2017-07-04T19:06:07Z")

</div>

> [@AlexMagnei](#):
>
> Hello there, I recently passed my Computer Games Development degree at University and I’m looking to expand upon what I learned during the course. During the course I used Unreal Engine, so I am new to Unity. I would like to know how to set up touch screen buttons that react in the same way as keyboard keys do, such that instead of pressing the Up Arrow on the keyboard, I can click on an up button displayed on the screen. I’ve looked at Unity’s tutorials and cannot find any on this topic. Thanks in advance.
> 
> Alex

You could achieve this by attaching a script to each virtual keyboard key that implements interfaces [IPointerDownHandler](https://docs.unity3d.com/ScriptReference/EventSystems.IPointerDownHandler.html) and [IPointerUpHandler](https://docs.unity3d.com/ScriptReference/EventSystems.IPointerUpHandler.html)from UnityEngine.EventSystems namespace. They could notify parent script through events or simple method calls that they’re being pressed.

For directional movement it’s better to just create a invisible virtual analog stick to where user presses and comparing the position delta to the starting point of the press for the duration of the press.

---

<div class="post-metadata">

### Author: ![JoeStrout](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/joestrout/32/200738_2.png) [@JoeStrout](https://discussions.unity.com/u/JoeStrout)
#### Post date: [July 4, 2017, 11:24pm UTC](https://discussions.unity.com/t/touch-screen-programming/669874/4 "2017-07-04T23:24:30Z")

</div>

These answers are answering what he actually asked in a literal way, but probably not what he actually needs.

I _think_ the real question is simply, how to react to touches of on-screen buttons? And that is very easy to do, and very well covered by the [UI tutorials](https://unity3d.com/learn/tutorials/topics/user-interface-ui).

A slightly more interesting question is: how to react to keyboard events in the same way you react to UI events? For that, I use the following little script, attached to any object in the scene.

```csharp
using UnityEngine;
using UnityEngine.Events;
using System.Collections.Generic;

public class KeyTrigger : MonoBehaviour {
    [System.Serializable]
    public class Element {
        [Tooltip("Key this event is triggered by.")]
        public KeyCode keyCode;
        [Tooltip("Event to fire when the key is pressed.")]
        public UnityEvent keyDownEvent;
        [Tooltip("Event to fire when the key is released.")]
        public UnityEvent keyUpEvent;
    };

    [Tooltip("List of key/event combinations.")]
    public List<Element> elements = new List<Element>(1);

    void Update() {
        foreach (var elem in elements) {
            if (Input.GetKeyDown(elem.keyCode)) elem.keyDownEvent.Invoke();
            if (Input.GetKeyUp(elem.keyCode)) elem.keyUpEvent.Invoke();
        }
    }
}

```
