# Button Selection

**URL:** <https://forums.solar2d.com/t/button-selection/338744>\
**Category:** Apple TV and tvOS\
**Created:** [April 3, 2016, 6:38am UTC](https://forums.solar2d.com/t/button-selection/338744 "2016-04-03T06:38:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcos\_Martini](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/m/b487fb/32.png) [@Marcos\_Martini](https://forums.solar2d.com/u/Marcos_Martini)\
**Post date:** [April 3, 2016, 6:38am UTC](https://forums.solar2d.com/t/button-selection/338744/1 "2016-04-03T06:38:04Z")

</div>

Hi guys,

I’m having a hard time figuring out something that perhaps is simple but I thought I could reach out to you:

Let’s say I have a screen with 2 buttons that I select by swiping the apple remote. When the scene is loaded, I would like the button on the left to be selected, and then when the user swipes right, I’d like to select the button on the right. I get this working, but the thing is that before I can select the button on the right, when running the app for the first time I have to swipe twice. It seems like the app can’t detect the first swipe… I’m attaching the code below and if you could try it out just install the app on the apple tv and try swiping right… you will notice that it doesn;t work, unless you swipe again. Is that something I’m missing?

Cheers,

Marcos

```
local buttons = {} local selectedButton &nbsp; local button1 = display.newRect( &nbsp;0, 0, 100, 100 ) button1.x =100 button1.y = 100 &nbsp; local button2 = display.newRect( &nbsp;0, 0, 100, 100 ) button2.x =300 button2.y = 100 &nbsp; buttons[1] = button1 buttons[2] = button2 &nbsp; selectedButton = 1 buttons[selectedButton]:setFillColor( 0,1,1 ) &nbsp; &nbsp; local resetAt0 = true local function onAxisEvent( event ) &nbsp; local previousButton = selectedButton &nbsp; local mappedEvent = { name = "key", phase = "up", keyName = "" } local delta = event.normalizedValue &nbsp; if not event.axis or (event.axis.type ~= 'y' and event.axis.type ~= 'x') then return end &nbsp; if resetAt0 and delta ~= 0 then return end resetAt0 = false &nbsp; if math.abs(delta) \> 0.5 then if event.axis.type == "x" then if delta \< 0 then mappedEvent.keyName = "left" selectedButton = 1 elseif delta \> 0 then selectedButton = 2 mappedEvent.keyName = "right" end end buttons[previousButton]:setFillColor( 1 ) buttons[selectedButton]:setFillColor( 0,1,1 ) resetAt0 = true end &nbsp; &nbsp; &nbsp; if mappedEvent.keyName ~= "" then Runtime:dispatchEvent( mappedEvent ) print( "DISPATCHED: " .. mappedEvent.keyName ) &nbsp; end end &nbsp; Runtime:addEventListener( "axis", onAxisEvent )
```

---

<div class="post-metadata">

**Author:** ![Marcos\_Martini](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/m/b487fb/32.png) [@Marcos\_Martini](https://forums.solar2d.com/u/Marcos_Martini)\
**Post date:** [April 6, 2016, 12:58am UTC](https://forums.solar2d.com/t/button-selection/338744/2 "2016-04-06T00:58:57Z")

</div>

Anyone?

---

<div class="post-metadata">

**Author:** ![JonPM](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@JonPM](https://forums.solar2d.com/u/JonPM)\
**Post date:** [April 6, 2016, 1:13pm UTC](https://forums.solar2d.com/t/button-selection/338744/3 "2016-04-06T13:13:45Z")

</div>

Perhaps try this?

```
... if math.abs(delta) \> 0.5 then if event.axis.type == "x" then if delta \< 0 then mappedEvent.keyName = "left" selectedButton = 1 elseif delta \> 0 then selectedButton = 2 mappedEvent.keyName = "right" end end resetAt0 = true end if mappedEvent.keyName ~= "" then buttons[previousButton]:setFillColor( 1 ) buttons[selectedButton]:setFillColor( 0,1,1 ) Runtime:dispatchEvent( mappedEvent ) print( "DISPATCHED: " .. mappedEvent.keyName ) end ...
```

---

<div class="post-metadata">

**Author:** ![Marcos\_Martini](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/m/b487fb/32.png) [@Marcos\_Martini](https://forums.solar2d.com/u/Marcos_Martini)\
**Post date:** [April 7, 2016, 7:15am UTC](https://forums.solar2d.com/t/button-selection/338744/4 "2016-04-07T07:15:28Z")

</div>

Thanks!!!

---

<div class="post-metadata">

**Author:** ![Marcos\_Martini](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/m/b487fb/32.png) [@Marcos\_Martini](https://forums.solar2d.com/u/Marcos_Martini)\
**Post date:** [April 6, 2016, 12:58am UTC](https://forums.solar2d.com/t/button-selection/338744/5 "2016-04-06T00:58:57Z")

</div>

Anyone?

---

<div class="post-metadata">

**Author:** ![JonPM](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@JonPM](https://forums.solar2d.com/u/JonPM)\
**Post date:** [April 6, 2016, 1:13pm UTC](https://forums.solar2d.com/t/button-selection/338744/6 "2016-04-06T13:13:45Z")

</div>

Perhaps try this?

```
... if math.abs(delta) \> 0.5 then if event.axis.type == "x" then if delta \< 0 then mappedEvent.keyName = "left" selectedButton = 1 elseif delta \> 0 then selectedButton = 2 mappedEvent.keyName = "right" end end resetAt0 = true end if mappedEvent.keyName ~= "" then buttons[previousButton]:setFillColor( 1 ) buttons[selectedButton]:setFillColor( 0,1,1 ) Runtime:dispatchEvent( mappedEvent ) print( "DISPATCHED: " .. mappedEvent.keyName ) end ...
```

---

<div class="post-metadata">

**Author:** ![Marcos\_Martini](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/m/b487fb/32.png) [@Marcos\_Martini](https://forums.solar2d.com/u/Marcos_Martini)\
**Post date:** [April 7, 2016, 7:15am UTC](https://forums.solar2d.com/t/button-selection/338744/7 "2016-04-07T07:15:28Z")

</div>

Thanks!!!
