Reputation: 151
I am attempting to make a simple button on an arduino board execute a ctrl+z command (for undo) when pressed. I know the modifier for ctrl in Arduino is KEY_LEFT_CTRL
My buttons are structured as below and sending single letters works fine, but KEY_LEFT_CTRL
doesn't seem to apply. I've tried a few attempts, current code below which compiles but simply sends Z
enum { sw0=3, sw1=4, sw2=5, sw3=6, sw4=7, sw5=8, sw6=9, sw7=10}; // Switchbutton lines
enum { nSwitches=8, bounceMillis=42}; // # of switches; debounce delay
struct ButtonStruct {
unsigned long int bounceEnd; // Debouncing-flag and end-time
// Switch number, press-action, release-action, and prev reading
byte swiNum, swiActP, swiActR, swiPrev;
};
struct ButtonStruct buttons[nSwitches] = {
{0, sw0, 'A'},
{0, sw1, 'B'},
{0, sw2, 'C'},
{0, sw3, 'D'},
{0, sw4, 'E'},
{0, sw5, 'F'},
{0, sw6, 'G'},
{0, sw7, 'H'}};
//--------------------------------------------------------
void setup() {
for (int i=0; i<nSwitches; ++i)
pinMode(buttons[i].swiNum, INPUT_PULLUP);
Keyboard.begin();
}
//--------------------------------------------------------
byte readSwitch (byte swiNum) {
// Following inverts the pin reading (assumes pulldown = pressed)
return 1 - digitalRead(swiNum);
}
//--------------------------------------------------------
void doAction(byte swin, char code, char action) {
Keyboard.write(action);
}
//--------------------------------------------------------
void doButton(byte bn) {
struct ButtonStruct *b = buttons + bn;
if (b->bounceEnd) { // Was button changing?
// It was changing, see if debounce time is done.
if (b->bounceEnd < millis()) {
b->bounceEnd = 0; // Debounce time is done, so clear flag
// See if the change was real, or a glitch
if (readSwitch(b->swiNum) == b->swiPrev) {
// Current reading is same as trigger reading, so do it
if (b->swiPrev) {
doAction(b->swiNum, 'P', b->swiActP);
} else {
doAction(b->swiNum, 'R', b->swiActR);
}
}
}
} else { // It wasn't changing; but see if it's changing now
if (b->swiPrev != readSwitch(b->swiNum)) {
b->swiPrev = readSwitch(b->swiNum);
b->bounceEnd = millis()+bounceMillis; // Set the Debounce flag
}
}
}
//--------------------------------------------------------
long int seconds, prevSec=0;
void loop() {
for (int i=0; i<nSwitches; ++i)
doButton(i);
}
Question restated: I currently have this code which works to debounce a set of simple push buttons so when pressed they simply send keystronkes (a, b, c, etc respectively)
I have two questions
• I was shown this method of setting button output which works really well for a 3rd party program I'm writing to configure the buttons. The problem is I don't know how to send key modifiers (like adding ctrl to 'z' to perform an undo command) with the way it currently is. I know the arduino modifier is KEY_LEFT_CTRL. Say I wanted to change the button below that delivers 'A' to act as ctrl+z instead. I am looking to swap out {0, sw0, 'A'} for something like {0, sw0, KEY_LEFT_CTRL + 'z'} (which doesn't work obviously) to make this happen in order to keep this array format in tact for easy configuring. Is this possible? I looked for a way to inject the hex even like 0x80+z but that was also a no go.
• The current method delivers one keystroke per press but stops output even if you hold the button down. I'd like to change it to act like a keyboard (press and hold delivers one keystroke, waits a second, then starts repeating the keystroke until you let go.) I don't know where to begin with that.
Upvotes: 1
Views: 2451
Reputation: 487
In Official Arduino's Tutorial web you have an example of combinations with ctrl
LINK
#include "Keyboard.h"
// use this option for OSX.
// Comment it out if using Windows or Linux:
char ctrlKey = KEY_LEFT_GUI;
// use this option for Windows and Linux.
// leave commented out if using OSX:
// char ctrlKey = KEY_LEFT_CTRL;
/*
*SETUP CODE
*/
Keyboard.press(ctrlKey);
Keyboard.press('n');
delay(100);
Keyboard.releaseAll();
Upvotes: 1