Buttons
The next component we are going to work with are capacitive buttons. The Rainbow HAT has three of them, labeled A, B, and C. Buttons can be handled in two different ways, using Buttons
and using InputDrivers
.
While Buttons
are similar in behavior to the ones we place on a traditional UI, InputDrivers
are similar to HID keyboards; they allow us to integrate with the operating system and send key press events, letting us handle our button presses as standard keys.
We will look at both of them with the same example: we will turn the red LED on when button A is pressed and off when it is not.
Let's start with the button driver.
Button driver
The simplest way to interact with a button is to use the button driver. This will allow us to interact with the physical button in a way similar to the traditional OnClickListener
listener from View
.
As usual, let's begin by looking at the code:
class ButtonDriverActivity : Activity() {
private lateinit var redLed: Gpio
private lateinit var buttonA...