🔍

Arduino as a simple keyboard interface

simple arduino cicuit with pushbutton and led on pin3 and pin4
#include "Keyboard.h"

int prev_btn_state = HIGH;

void setup() {

  pinMode( 3 , OUTPUT); // define pin3 as output
  pinMode( 4 , INPUT_PULLUP);
  Keyboard.begin();

  Serial.begin(9600); // initate serial communication for debugging

}

void loop() {

    int curr_btn_state = digitalRead(4); // just read the current btn state and save in curr_btn_state 

    if( prev_btn_state !=  curr_btn_state ){
      // just go here when button state is different

       
        if( curr_btn_state == 0){
            Serial.println("gedrueckt!!!!");
            // Keyboard.print("Lorem ipsum, sorem lipsum. Bla Blubb.");
            delay(100);    
            Keyboard.press(KEY_LEFT_CTRL); // Press Ctrl key
            Keyboard.press('s');           // Press 's' key
            delay(100);                     // Delay to ensure key press is registered
            Keyboard.releaseAll(); 



        }else{
           Serial.println("losgelassen");
        }


       prev_btn_state = curr_btn_state;
    }

  delay(20);

}