Key.readEvent()

From Microduino Wiki
Jump to: navigation, search

Used to detect the [long press] and [short press] actions of digital switches, analog switches and virtual switches.

Digital switches: Key. readEvent(time)

Description

Identify digital sensors’ [long press] and [short press] actions. Digital sensors must be used together with DigitalKey() structure.

Parameter

  • time: duration of long press, the default is 1000ms.

Return

uint8_t type. Short press: SHORT_PRESS, long press: LONG_PRESS

Analog switch: Key. readEvent(MIN,MAX,time)

Description

Identify analog sensors’ [long press] and [short press] within corresponding sections. Analog switches must be used together with the structure AnalogKey().

Parameters

  • MIN: the minimum of analog section;
  • MAX: the maximum of analog section;
  • time: duration of long press, and the default is 1000ms.

Return

uint8_t type. Short press: SHORT_PRESS, long press: LONG_PRESS

Virtual switch: Key. readEvent(bool,time)

Description

Identify the [long press] and [short press] actions of analog switches’ corresponding states. Virtual switches must be used together with structure VirtualKey().

Parameters

  • bool: true and false values of virtual switches;
  • time: the duration of long press, and the default is 1000ms.

Return

uint8_t type. Short press: SHORT_PRESS, long press: LONG_PRESS

Sample

  • Identify analog sensors’ [long press] and [short press] actions within corresponding sections.
#include <Microduino_Key.h>

AnalogKey keyAnalog(A0);

void setup() {
  Serial.begin(9600);
  keyAnalog.begin(INPUT);
}

void loop() {
  switch (keyAnalog.readEvent(0, 50)) {   //(analog minimum, analog maximum)
    case SHORT_PRESS:
      Serial.println("KEY (analog) SHORT_PRESS");   //Short press 
      break;
    case LONG_PRESS:
      Serial.println("KEY (analog) LONG_PRESS");    //Long press
      break;
  }

  delay(50);
}


  • Identify digital sensors’ [long press] and [short press] actions within corresponding sections.
#include <Microduino_Key.h>

DigitalKey keyDigital(A0);

void setup() {
  Serial.begin(9600);
  keyDigital.begin(INPUT_PULLUP);
}

void loop() {
  switch (keyDigital.readEvent()) {
    case SHORT_PRESS:
      Serial.println("KEY (digital) SHORT_PRESS");   //Short press 
      break;
    case LONG_PRESS:
      Serial.println("KEY (digital) LONG_PRESS");    //Long press 
      break;
  }

  delay(50);
}


  • Identify virtual sensors’ [long press] and [short press] actions of corresponding states.
#include <Microduino_Key.h>

VirtualKey keyVirtual;

void setup() {
  Serial.begin(9600);
  keyVirtual.begin();
  pinMode(A0, INPUT_PULLUP);
}

void loop() {
  bool val = !digitalRead(A0);  //val any bool variables 
  switch (keyVirtual.readEvent(val,1500)) {   //(a variable, the time of long press) the unit of the time is ms, and the default is 1000
    case SHORT_PRESS:
      Serial.println("KEY (Virtual) SHORT_PRESS");   //Short press 
      break;
    case LONG_PRESS:
      Serial.println("KEY (Virtual) LONG_PRESS");    //Long press 
      break;
  }

  delay(50);
}

Others



Return to Sensor_Key_Reference