Arduino leonardo - Making pc pedals

Hello everyone :slight_smile:

So my project is as follows:

I want to make pedal buttons for my pc. I am design engineer and work with Solidworks program.
I want to use 3D mouse with Solidworks, but in order to do it effectively I need some button pedals that would for example: One of the pedals would be ctrl and when it is pressed and hold down it would work just like with keyboard and while I hold down this pedal/ctrl I can press other keys one the keyboard and it will understand that it should stop pressing the ctrl button when I release the pedal.

I went trough the tutorials for the Arduino Leonardo and got so far that the when I press the pedal i get a key stroke like for example page down or page up. But I just can't get it to work just like the keyboard when key is pressed and hold it outputs that key all the time.

So the question, is it even possible to do what I want with this Leonardo?

I believe so...

Can you post your code/example?

I thought there were 'several' ways to write (send) data to the PC.. one being that the 'key-up' (release) was done for you.. and another way where you had to manually do so.. (which is what I think you are after)

xl97:
I believe so...

Can you post your code/example?

I thought there were 'several' ways to write (send) data to the PC.. one being that the 'key-up' (release) was done for you.. and another way where you had to manually do so.. (which is what I think you are after)

#include "Keyboard.h"

const int buttonPin = 4; // input pin for pushbutton
int previousButtonState = HIGH; // for checking the state of a pushButton
int counter = 0; // button push counter

void setup() {
// make the pushButton pin an input:
pinMode(buttonPin, INPUT);
// initialize control over the keyboard:
Keyboard.begin();
}

void loop() {
// read the pushbutton:
int buttonState = digitalRead(buttonPin);
// if the button state has changed,
if ((buttonState != previousButtonState)
// and it's currently pressed:
&& (buttonState == HIGH)) {

Keyboard.press(KEY_LEFT_CTRL);
delay(9999);
Keyboard.releaseAll();
}
// save the current button state for comparison next time:
previousButtonState = buttonState;
}

I am getting somewhere, so far I have figured out that I need to implement if statement after keyboard press, that cheeks if the button is still pressed and if so then keep pressing the Ctrl button, but if it has been released then keyboard.releseAll. So could some one give me this one paragraph that would work?

It seams that managed to achieve the goal :slight_smile:

#include "Keyboard.h"
//the character we want to type (SPACE)
char myKey = KEY_LEFT_CTRL;
//saving the state of the push button
int buttonState = 0;

void setup() {
//button is on 4
pinMode(4, INPUT);
//start emulating keyboard
Keyboard.begin();
//start serial port for debug (open it with tools > serial monitor)
Serial.begin(9600);
}

void loop() {

//something changed
if(buttonState != digitalRead(4))
{
//wait
delay(10);
//read it again

//still different
if(buttonState != digitalRead(4))
{
Serial.println("Stable state");

//then save on the buttonState variable
buttonState = digitalRead(4);

if(buttonState == HIGH)
{
Keyboard.press(myKey);
}
if(buttonState == LOW)
{
Keyboard.release(myKey);
}
}
}
}