making a HID Keyboard style gamming device

hello … first project ever!

Hardware: Arduino Leonardo + a few components (buttons, resistors, LED’s, ect)

Description: press one of (lets say 4) buttons and have the board emulate a combination key press such “Ctrl F1”

Want to augment gameplay in much the way a Razer Tartarus but on a platform over the mouse hand

(used for less used functions in an online RPG) (Win 8.1 PC w/ Genaric keyboard, Logitech wireless

mouse, Razer Tartarus)

Problem: When I connect board via USB, and it interferes with real keyboard

Question: Can the Leonardo do this?

Does the implemtation of the Leonardo’s USB override the other keyboard and make it not work right?

is there a better board for this? Adafruit blueboard, teezy, ect?

Any help, Sugestions, Info, ect ? (Thankyou!)

Arduino C program:

The setup is mostly for show … blinking of lights (Works well enough)… Keyboard begin/end are how they are

because I read somewhere that leaving keyboard.begin “on” could cause interference on USB.

did not find a lot on sending non-printing keys like “F1” (wanted to try something like ascii(121) )

/* 
 Project Mousehole
 By: Douglas A. Buck
 Hardware:  Arduino Leonardo
 
 This program will detect if a button has been pressed and send a 
 keyboard.press() signal back to host computer to emulate a 
 predetermined value (HID keyboard emulation). 
 */

// Functions declarations

void blink_LED_all();
void led1On();
void led2On();
void led3On();
void led4On();
void all_led_off();
void cycle_led();
void razelDazle(); 

// Global Constants

const int buttonPin1 = 13;                   // assign Buttton1 to pin 13 GPIO
const int buttonPin2 = 12;                   // assign Buttton2 to pin 12 GPIO
const int buttonPin3 = 11;                   // assign Buttton3 to pin 11 GPIO
const int buttonPin4 = 10;                   // assign Buttton4 to pin 10 GPIO

const int ledPin1 = 9;                       // assign LED 1 to pin 9 GPIO
const int ledPin2 = 8;                       // assign LED 2 to pin 8 GPIO
const int ledPin3 = 7;                       // assign LED 3 to pin 7 GPIO
const int ledPin4 = 6;                       // assign LED 4 to pin 6 GPIO

const int fourSecond = 4000;                 // = 4 seconds
const int oneSecond = 1000;                  // = 1 second
const int halfSecond = 500;                  // = 1/2 second

// Global Integers

int button1state = 0;                        // variable to detect if Button 1 has been pressed
int button2state = 0;                        // variable to detect if Button 2 has been pressed
int button3state = 0;                        // variable to detect if Button 3 has been pressed
int button4state = 0;                        // variable to detect if Button 4 has been pressed

// ------------------------------------------------------------------------------------

void blink_LED_all()                         // loop to blink all LEDs 4 times
{
  int n = 1;  
  for (n=1; n<5; n++)
  {
    digitalWrite (ledPin1, HIGH);            // turn on led1
    digitalWrite (ledPin2, HIGH);            // turn on led2
    digitalWrite (ledPin3, HIGH);            // turn on led3
    digitalWrite (ledPin4, HIGH);            // turn on led4
    delay(oneSecond);                            // delay 1 sec (1000ms)

    digitalWrite (ledPin1, LOW);             // turn off led1
    digitalWrite (ledPin2, LOW);             // turn off led2
    digitalWrite (ledPin3, LOW);             // turn off led3
    digitalWrite (ledPin4, LOW);             // turn off led4
    delay(oneSecond);                            // delay 1 sec (1000ms)
  }
}

void led1On() 
{
  digitalWrite (ledPin1, HIGH);              // turn on led1
}

void led2On() 
{
  digitalWrite (ledPin2, HIGH);              // turn on led2
}

void led3On() 
{
  digitalWrite (ledPin3, HIGH);              // turn on led3
}

void led4On()
{
  digitalWrite (ledPin4, HIGH);              // turn on led4
}

void all_led_off()                           // turn all led's off 
{
  digitalWrite(ledPin1, LOW);                // turn off led1
  digitalWrite(ledPin2, LOW);                // turn off led2
  digitalWrite(ledPin3, LOW);                // turn off led3
  digitalWrite(ledPin4, LOW);                // turn off led4
}

void cycle_led() 
{ 
  led1On();                                  // turn 1 on  
  delay( halfSecond );                  // delay 1/2 sec (250ms)
  all_led_off();  // turn 1 off
  delay(500);
  
  led2On();                                  // turn 2 on
  delay( halfSecond );                  // delay 1/2 sec (250ms)
  all_led_off();                             // turn 2 off
  delay( halfSecond );  
  
  led3On();                                  // turn 3 on
  delay( halfSecond );                   // delay 1/2 sec (250ms)
  all_led_off();                             // turn 3 off
  delay( halfSecond );                   
  
  led4On();                                  // turn 4 on
  delay(500);                                // delay 1/2 sec (250ms)
  all_led_off();                             // turn 4 off
  delay(500);
}

void read_buttons()                          // poll each button to determined if pressed 
{
  button1state = digitalRead(buttonPin1);
  button2state = digitalRead(buttonPin2);
  button3state = digitalRead(buttonPin3);
  button4state = digitalRead(buttonPin4);
}

void razelDazle()
{
  blink_LED_all();
  cycle_led();
  cycle_led();
}

// -------------------------------------------------------------------------------------------

void setup() 
{
  // Setting up LED GPIO Pins for output

  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);

  // Setting up Button GPIO Pins for input

  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);

// allow Host Computer to Boot

  razelDazle();                              // Create a LED light show
  delay(fourSecond);                         // delay to allow computer to boot
}

// -------------------------------------------------------------------------------------------

void loop()                                  // put your main code here, to run repeatedly:

{
  read_buttons();
  if (button1state == HIGH) 
  { 
    Keyboard.begin();                            // commented out till get Leonardo
    all_led_off();    
    digitalWrite(ledPin1, HIGH);             // turns on 1st LED if 1st button pressed

    Keyboard.press(KEY_LEFT_CTRL);      // this section emulates pressing 
    Keyboard.press('F1');                         // 'Control F1' key combination,
    Keyboard.releaseAll();                      // when the 1st button is pressed 
    
    Keyboard.end();                               // commented out till get Leonardo
  }
  else if (button2state == HIGH) 
  { 
    Keyboard.begin();
    all_led_off();    
    digitalWrite(ledPin2, HIGH);             // turns on 2nd LED if 2nd button pressed

    Keyboard.press(KEY_LEFT_CTRL);           // this section emulates pressing 
    Keyboard.press('F1');                    // 'Control F2' key combination,
    Keyboard.releaseAll();                   // when the 2nd button is pressed 
  
    Keyboard.end();  
  }
  else if (button3state == HIGH) 
  {  
    Keyboard.begin();
    all_led_off();    
    digitalWrite(ledPin3, HIGH);             // turns on 3rd LED if 3rd button pressed

    Keyboard.press(KEY_LEFT_CTRL);           // this section emulates pressing 
    Keyboard.press('F1');                    // 'Control F3' key combination,
    Keyboard.releaseAll();                   // when the 3rd button is pressed
    Keyboard.end(); 
  }
  else if (button4state == HIGH) 
  {
    Keyboard.begin();
    all_led_off();    
    digitalWrite(ledPin4, HIGH);             // turns on 4th LED if 4th button pressed

    Keyboard.press(KEY_LEFT_CTRL);           // this section emulates pressing 
    Keyboard.press('F1');                    // 'Control F4' key combination,
    Keyboard.releaseAll();                   // when the 4th button is pressed
    
    Keyboard.end();
  }

  else;

}

Tiyak :slight_smile:

I’m not sure what you mean by overriding the other keyboard. Most OS input stacks handle multiple keyboards by having a pressed key override a non-pressed key, otherwise if you had two keyboards plugged in, neither would work unless you pressed the same key on both at the same time. As for mice, it does the same for button states, and the movement vectors are simply applied on a first-come-first-served basis. What exactly is the problem you’re experiencing?

Edit: This

Keyboard.press('F1');

is not going to do what you want. You want

Keyboard.write(KEY_F1);

Same for the other non-ASCII keys. Read the docs, [Keyboard.press takes an ASCII character as its parameter, you need [Keyboard.write for special keys.](Keyboard.write() - Arduino Reference)](Keyboard.press() - Arduino Reference)