Digistump Forums

The Digispark => Digispark Project Ideas => Topic started by: Reon.Balisty on June 14, 2014, 01:45:53 pm

Title: Programming keyboard presses?
Post by: Reon.Balisty on June 14, 2014, 01:45:53 pm
using digital inputs, I want to make button presses do certain buttons, IE 0='v'  1='SPACE'

anyone can help please?  No help from searching in this site, and want to finish this project soon. 
Title: Re: Programming keyboard presses?
Post by: dougal on June 20, 2014, 05:36:16 am
Try something like...
Code: [Select]
#include "DigiKeyboard.h"

// Buttons... pins 3 and 4 are used for USB. You can use any other pins.
uint8_t button1 = 0; // pin number for first button
uint8_t button2 = 1; // pin number for second button

// We'll assume that your button inputs are normally LOW, and go to HIGH when pressed
uint8_t value1 = LOW;
uint8_t value2 = LOW;

void setup() {
  DigiKeyboard.update();
  DigiKeyboard.sendKeyStroke(0); // Some systems seem to need this. You might not.

  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
}


void loop() {
  DigiKeyboard.update();

  value1 = digitalRead(button1);
  value2 = digitalRead(button2);

  if (value1 == HIGH) {
    DigiKeyboard.print("v");
  }

  if (value2 == HIGH) {
    DigiKeyboard.sendKeyStroke(KEY_SPACE);
  }

  // Optional: small delay to allow for button debounce time.
  DigiKeyboard.delay(100);
}

Note: this is completely untested, but I did at least verify that it compiles.

I gleaned this from searching the forums. I found threads like these:

http://digistump.com/board/index.php/topic,319.0.html
http://digistump.com/board/index.php/topic,366.0.html
http://digistump.com/board/index.php/topic,894.0.html