function and media keys

This commit is contained in:
mintey 2021-03-03 18:57:54 +02:00
parent df9d926770
commit 3f393b3fa3

View file

@ -6,7 +6,7 @@
// using my own masks here since the inbuilt ones conflict with arrow keys etc.
#define MASK_SHIFT 0x100
#define MASK_ALTGR 0x200
#define MASK_CTRL 0x400
#define MASK_CTRL 0x800
const byte PIN_A = 33;
const byte PIN_B = 32;
@ -44,6 +44,9 @@ const int button_characters[6] = { KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F };
const int button_characters_symbol[6] = {
KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6
};
const int button_characters_function[6] = {
KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6
};
/*
backspace, space (keep those two first since they overlap with the rest )
@ -88,40 +91,58 @@ const int chord_targets_symbol[32] = {
// ) ] > }
KEY_0 + MASK_SHIFT, KEY_RIGHT_BRACE, KEY_PERIOD + MASK_SHIFT, KEY_RIGHT_BRACE + MASK_SHIFT,
};
const int chord_targets_function[32] = {
KEY_MEDIA_PLAY_PAUSE, 0, KEY_MEDIA_PREV_TRACK, 0,
KEY_MEDIA_MUTE, 0, KEY_MEDIA_NEXT_TRACK, 0,
KEY_F7, KEY_F8, KEY_F9, KEY_F10,
KEY_F11, KEY_F12, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
/*
dash, backslash, slash, apostrophe, comma, exclamation point, question mark,
period, up, down, page up, page down, shift, symbol shift, switch keyset,
escape, control, alt, delete, insert, tab, enter
escape, control, alt, delete, function, gui, tab, enter
*/
// TODO: add the middle specials for function keys instead of the weird symbols
const byte specials[22] = {
const byte specials[23] = {
17, 51, 30, 10, 20, 12, 33, 34, 9, 36, 27,
54, 18, 45, 63, 31, 47, 55, 62, 43, 61, 59
54, 18, 45, 63, 31, 47, 55, 62, 29, 43, 61, 59
};
enum Modifier { Shift, SymbolShift, Keyset, Control, Alt, Gui };
const int special_action_targets[22] = {
enum Modifier { Shift, SymbolShift, Keyset, Control, Alt, Gui, Function };
const int special_action_targets[23] = {
// - \ / '
KEY_MINUS, KEY_MINUS + MASK_ALTGR, KEY_SLASH, KEY_QUOTE,
// , ! ? .
KEY_COMMA, KEY_1 + MASK_SHIFT, KEY_SLASH + MASK_SHIFT, KEY_PERIOD,
KEY_UP, KEY_DOWN, KEY_PAGE_UP, KEY_PAGE_DOWN,
Modifier::Shift, Modifier::SymbolShift, Modifier::Keyset,
KEY_ESC, Modifier::Control, Modifier::Alt, KEY_DELETE, Modifier::Gui,
KEY_TAB, KEY_ENTER
KEY_ESC, Modifier::Control, Modifier::Alt, KEY_DELETE,
Modifier::Function, Modifier::Gui, KEY_TAB, KEY_ENTER
};
const int special_action_targets_symbol[22] = {
const int special_action_targets_symbol[23] = {
// _ ` ´ "
KEY_MINUS + MASK_SHIFT, KEY_TILDE, KEY_SEMICOLON + MASK_ALTGR, KEY_2 + MASK_SHIFT,
// ; | ~ :
KEY_SEMICOLON, KEY_TILDE + MASK_ALTGR, KEY_BACKSLASH + MASK_SHIFT, KEY_SEMICOLON + MASK_SHIFT,
0, 0, 0, 0,
0, 0, 0,
0, 0, 0, 0, 0,
0, 0
0, 0, 0, 0,
0, 0, 0, 0
};
const byte special_action_target_types[22] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0
const int special_action_targets_function[23] = {
0, 0, 0, 0,
0, 0, 0, 0,
KEY_MEDIA_VOLUME_INC, KEY_MEDIA_VOLUME_DEC, 0, 0,
0, 0, 0,
0, 0, 0, KEY_PRINTSCREEN,
0, 0, 0, 0
};
const byte special_action_target_types[23] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0
};
byte key_pressed = 0;
@ -137,6 +158,7 @@ bool mod_control = false;
bool mod_alt = false;
bool mod_altgr = false;
bool mod_gui = false;
bool mod_function = false;
IntervalTimer trackball_timer;
volatile bool trackball_update = false;
@ -320,10 +342,12 @@ void key_released(byte key) {
return;
target = special_action_targets[special];
if (mod_shift || mod_shift_lock || mod_symbol || mod_symbol_lock) {
if (special_action_targets_symbol[special] != 0)
target = special_action_targets_symbol[special];
}
if ((mod_shift || mod_shift_lock || mod_symbol || mod_symbol_lock) &&
special_action_targets_symbol[special] != 0)
target = special_action_targets_symbol[special];
if (mod_function &&
special_action_targets_function[special] != 0)
target = special_action_targets_function[special];
switch (special_action_target_types[special]) {
case ActionType::PressKey:
@ -359,6 +383,9 @@ void key_released(byte key) {
case Modifier::Gui:
mod_gui = !mod_gui;
return;
case Modifier::Function:
mod_function = !mod_function;
return;
}
}
return;
@ -383,9 +410,12 @@ void key_released(byte key) {
if (!chorded_pressed && key_pressed == 0 &&
key_pressed_total == chords[chord]) {
target = chord_targets[chord * 4];
if (mod_symbol || mod_symbol_lock)
if (chord_targets_symbol[chord * 4] != 0)
target = chord_targets_symbol[chord * 4];
if ((mod_symbol || mod_symbol_lock) &&
chord_targets_symbol[chord * 4] != 0)
target = chord_targets_symbol[chord * 4];
if (mod_function &&
chord_targets_function[chord * 4] != 0)
target = chord_targets_function[chord * 4];
press_key(target);
return;
@ -394,9 +424,12 @@ void key_released(byte key) {
for (byte b = 0; b < 3; b++) {
if (key == chord_buttons[chord * 3 + b]) {
target = chord_targets[chord * 4 + 1 + b];
if (mod_symbol || mod_symbol_lock)
if (chord_targets_symbol[chord * 4 + 1 + b] != 0)
target = chord_targets_symbol[chord * 4 + 1 + b];
if ((mod_symbol || mod_symbol_lock) &&
chord_targets_symbol[chord * 4 + 1 + b] != 0)
target = chord_targets_symbol[chord * 4 + 1 + b];
if (mod_function &&
chord_targets_function[chord * 4 + 1 + b] != 0)
target = chord_targets_function[chord * 4 + 1 + b];
// erase key from total so you can hold the chord down for
// multiple chorded keypresses
@ -413,7 +446,9 @@ void key_released(byte key) {
REGULAR KEYS
*/
// keypress
if (mod_symbol || mod_symbol_lock)
if (mod_function && button_characters_function[key] != 0)
press_key(button_characters_function[key]);
else if (mod_symbol || mod_symbol_lock)
press_key(button_characters_symbol[key]);
else
press_key(button_characters[key]);
@ -421,6 +456,9 @@ void key_released(byte key) {
}
void press_key(int key) {
Serial.print("press key ");
Serial.println(key);
// check if modifiers need to be forced
if (key & MASK_SHIFT) {
mod_shift = true;
@ -464,13 +502,14 @@ void press_key(int key) {
Keyboard.release(KEY_LEFT_GUI);
// clear temporary modifiers
mod_shift = mod_symbol = mod_control = mod_alt = mod_altgr = mod_gui = false;
mod_shift = mod_symbol = mod_control = mod_alt = mod_altgr =
mod_gui = mod_function = false;
}
void update_leds() {
digitalWrite(PIN_LED1, mod_shift || mod_shift_lock);
digitalWrite(PIN_LED2, mod_symbol || mod_symbol_lock);
digitalWrite(PIN_LED3, mod_control || mod_alt || mod_gui);
digitalWrite(PIN_LED3, mod_control || mod_alt || mod_gui || mod_function);
}
int sign(int num) {