# Using joystick buttons in Lua scripts

**URL:** <https://discuss.bluerobotics.com/t/using-joystick-buttons-in-lua-scripts/14727>\
**Category:** ArduSub\
**Created:** [July 17, 2023, 7:23pm UTC](https://discuss.bluerobotics.com/t/using-joystick-buttons-in-lua-scripts/14727 "2023-07-17T19:23:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![clyde](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.bluerobotics.com/clyde/32/8563_2.png) [@clyde](https://discuss.bluerobotics.com/u/clyde)\
**Post date:** [July 17, 2023, 7:23pm UTC](https://discuss.bluerobotics.com/t/using-joystick-buttons-in-lua-scripts/14727/1 "2023-07-17T19:23:43Z")

</div>

Hi all, I have been experimenting with Lua scripts and wanted a way to access the joystick buttons to trigger functionality.

Here is a way to create up to 4 “script buttons”. These buttons will keep track of how many times they have been pressed, and you can query the current state of the button and the number of button presses inside a Lua script. There’s an example Lua script to show how they work.

> **[Comparing ArduPilot:master...clydemcqueen:clyde\_sub\_script\_buttons ·...](https://github.com/ArduPilot/ardupilot/compare/master...clydemcqueen:ardupilot:clyde_sub_script_buttons)**
>
> ArduPlane, ArduCopter, ArduRover, ArduSub source. Contribute to ArduPilot/ardupilot development by creating an account on GitHub.

If this is generally useful I’ll submit a PR to continue the discussion. If there is a better way to access the joystick buttons, please let me know!

/Clyde

---

<div class="post-metadata">

**Author:** ![williangalvani](https://avatars.discourse-cdn.com/v4/letter/w/6f9a4e/32.png) [@williangalvani](https://discuss.bluerobotics.com/u/williangalvani)\
**Post date:** [July 18, 2023, 12:24am UTC](https://discuss.bluerobotics.com/t/using-joystick-buttons-in-lua-scripts/14727/2 "2023-07-18T00:24:05Z")

</div>

Hi @clyde

That is great! I like it as is. Please open a PR =]

---

<div class="post-metadata">

**Author:** ![clyde](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.bluerobotics.com/clyde/32/8563_2.png) [@clyde](https://discuss.bluerobotics.com/u/clyde)\
**Post date:** [July 18, 2023, 3:17pm UTC](https://discuss.bluerobotics.com/t/using-joystick-buttons-in-lua-scripts/14727/3 "2023-07-18T15:17:14Z")

</div>

Cool, thx. [Sub: Lua scripts can query joystick button state by clydemcqueen · Pull Request #24347 · ArduPilot/ardupilot · GitHub](https://github.com/ArduPilot/ardupilot/pull/24347)

---

<div class="post-metadata">

**Author:** ![moptop219](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@moptop219](https://discuss.bluerobotics.com/u/moptop219)\
**Post date:** [January 9, 2025, 12:19am UTC](https://discuss.bluerobotics.com/t/using-joystick-buttons-in-lua-scripts/14727/4 "2025-01-09T00:19:17Z")

</div>

I have been trying to find a way to use joystick button presses in my lua scripts. Are you able to give me an idea of how i can get this working? I have manual\_control sending joystick axis and button presses to my OrangeCube. What code needs to be implemented in my lua script to check the status of these buttons? Thanks in advance for your time.

---

<div class="post-metadata">

**Author:** ![tony-white](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.bluerobotics.com/tony-white/32/18385_2.png) [@tony-white](https://discuss.bluerobotics.com/u/tony-white)\
**Post date:** [January 9, 2025, 6:13pm UTC](https://discuss.bluerobotics.com/t/using-joystick-buttons-in-lua-scripts/14727/6 "2025-01-09T18:13:22Z")

</div>

Hi @moptop219 -  
Welcome to the forums!

You can see some examples of how to read buttons via Lua script [here](https://github.com/vshie/luascript_library_BlueRobotics/blob/main/EngineStart.lua), however I’m not sure if lua scripts work with BlueOS and the OrangeCube - they don’t with the Pixhawk! Are you running ArduSub 4.5?

---

<div class="post-metadata">

**Author:** ![moptop219](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@moptop219](https://discuss.bluerobotics.com/u/moptop219)\
**Post date:** [January 9, 2025, 8:47pm UTC](https://discuss.bluerobotics.com/t/using-joystick-buttons-in-lua-scripts/14727/7 "2025-01-09T20:47:39Z")

</div>

Thanks Tony for sending that link through, will give it a go.  
Yes I am currently trialling ArduSub 4.5.
