keymap.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // this is the style you want to emulate.
  2. // This is the canonical layout file for the Quantum project. If you want to add another keyboard,
  3. #include "dichotomy.h"
  4. // Each layer gets a name for readability, which is then used in the keymap matrix below.
  5. // The underscores don't mean anything - you can have a layer called STUFF or any other name.
  6. // Layer names don't all need to be of the same length, obviously, and you can also skip them
  7. // entirely and just use numbers.
  8. enum dichotomy_layers {
  9. _BS,
  10. _SF,
  11. _NM,
  12. _NS,
  13. _MS
  14. };
  15. #define LONGPRESS_COUNT 4
  16. enum dichotomy_keycodes
  17. {
  18. CK_1G = SAFE_RANGE,
  19. CK_BSPE,
  20. CK_QE,
  21. CK_TE, //these 4 CK_XXXX keys are special "alternate long-press" keys controlled with unique timers. Make sure you understand them before you mess with them.
  22. NS_HYPH,
  23. NS_EQU,
  24. NUMKEY,
  25. SFTKEY,
  26. MOUKEY,
  27. MS_BTN1,
  28. MS_BTN2,
  29. MS_BTN3
  30. };
  31. #define CUSTOM_LONGPRESS 150
  32. #define CUSTOM_TOGGLE_TIME 300
  33. #define RED_BRIGHTNESS 3
  34. #define GREEN_BRIGHTNESS 2
  35. #define BLUE_BRIGHTNESS 2
  36. // Fillers to make layering more clear
  37. #define _______ KC_TRNS
  38. #define XXXXXXX KC_NO
  39. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  40. [_BS] = LAYOUT( /* Base layout, nearly qwerty but with modifications because it's not a full keyboard. Obviously. */
  41. CK_TE, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_BSPC,
  42. NUMKEY, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, CK_QE,
  43. SFTKEY, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, MOUKEY,
  44. KC_LCTL, KC_LALT, KC_LGUI, KC_RGUI, KC_RALT, KC_RCTL,
  45. MS_BTN3, KC_LBRC, KC_LPRN, KC_SPC, KC_SPC, KC_RPRN, KC_RBRC, MS_BTN3
  46. ),
  47. [_SF] = LAYOUT( /* Shifted layout, small changes (because angle brackets have been moved to thumb cluster buttons) */
  48. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  49. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  50. _______, _______, _______, _______, _______, _______, _______, _______, NS_HYPH, KC_UNDS, _______, _______,
  51. _______, _______, _______, _______, _______, _______,
  52. _______, _______, KC_LABK, _______, _______, KC_RABK, _______, _______
  53. ),
  54. [_NM] = LAYOUT( /* Number layout, basically the main function layer */
  55. _______, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, _______,
  56. _______, CK_1G, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, CK_BSPE,
  57. _______, KC_F11, KC_F12, KC_F13, KC_F14, KC_F15, KC_F16, KC_F17, KC_F18, KC_F19, KC_F20, _______,
  58. _______, _______, _______, _______, _______, _______,
  59. _______, _______, _______, _______, _______, _______, _______, _______
  60. ),
  61. [_NS] = LAYOUT( /* Shifted number/function layout, for per-key control. Only active when shift is held, and number is toggled or held */
  62. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  63. _______, _______, _______, _______, _______, _______, _______, _______, _______, KC_PLUS, NS_EQU, _______,
  64. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  65. _______, _______, _______, _______, _______, _______,
  66. _______, _______, _______, _______, _______, _______, _______, _______
  67. ),
  68. [_MS] = LAYOUT( /* Mouse layer, including buttons for clicking. */
  69. _______, _______, _______, _______, _______, _______, KC_VOLU, KC_HOME, KC_PGUP, _______, _______, _______,
  70. _______, _______, _______, _______, _______, _______, _______, MS_BTN1, MS_BTN2, _______, _______, _______,
  71. _______, _______, _______, _______, _______, _______, KC_VOLD, KC_END, KC_PGDN, _______, _______, _______,
  72. _______, _______, _______, _______, KC_UP, _______,
  73. _______, _______, _______, _______, KC_LEFT, KC_DOWN, KC_RGHT, _______
  74. )
  75. };
  76. const uint16_t PROGMEM fn_actions[] = {
  77. };
  78. static uint16_t special_timers[LONGPRESS_COUNT] = {0xFFFF,0xFFFF,0xFFFF,0xFFFF};
  79. static bool special_key_states[LONGPRESS_COUNT] = {0,0,0,0};
  80. static bool special_key_pressed[LONGPRESS_COUNT] = {0,0,0,0};
  81. static uint16_t shift_timer;
  82. static uint16_t num_timer;
  83. static uint16_t mouse_timer;
  84. static uint8_t red_timer;
  85. static uint8_t green_timer;
  86. static uint8_t blue_timer;
  87. static bool shift_singular_key = false;
  88. static bool number_singular_key = false;
  89. static bool mouse_singular_key = false;
  90. static bool capsLED = false;
  91. static bool shiftLED = false;
  92. static bool numLED = false;
  93. static bool mouseLED = false;
  94. static bool shift_held = false;
  95. static bool shift_suspended = false;
  96. report_mouse_t currentReport = {};
  97. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  98. //uint8_t layer;
  99. //layer = biton32(layer_state); // get the current layer //Or don't, I didn't use it.
  100. bool returnVal = true; //this is to determine if more key processing is needed.
  101. //custom layer handling for tri_layer,
  102. switch (keycode) {
  103. case NUMKEY:
  104. if (record->event.pressed) {
  105. num_timer = timer_read();
  106. number_singular_key = true;
  107. layer_invert(_NM);
  108. numLED = !numLED;
  109. } else {
  110. if (timer_elapsed(num_timer) < CUSTOM_TOGGLE_TIME && number_singular_key) {
  111. //do nothing, the layer has already been inverted
  112. } else {
  113. layer_invert(_NM);
  114. numLED = !numLED;
  115. }
  116. }
  117. update_tri_layer(_NM, _SF, _NS);
  118. returnVal = false;
  119. break;
  120. //SHIFT is handled as LSHIFT in the general case - 'toggle' shoudl activate caps, while the layer is only active when shift is held.
  121. case SFTKEY:
  122. if (record->event.pressed) {
  123. shift_held = true;
  124. shiftLED = true;
  125. shift_suspended = false;
  126. shift_timer = timer_read();
  127. shift_singular_key = true;
  128. layer_on(_SF);
  129. register_code(KC_LSFT);
  130. } else {
  131. shift_held = false;
  132. shiftLED = false;
  133. if (timer_elapsed(shift_timer) < CUSTOM_TOGGLE_TIME && shift_singular_key) {
  134. //this was basically a toggle, so activate/deactivate caps lock.
  135. SEND_STRING(SS_TAP(X_CAPSLOCK));
  136. capsLED = !capsLED;
  137. }
  138. layer_off(_SF);
  139. unregister_code(KC_LSFT);
  140. }
  141. update_tri_layer(_NM, _SF, _NS);
  142. returnVal = false;
  143. break;
  144. //MOUSE layer needs to be handled the same way as NUMKEY, but differently from shift
  145. case MOUKEY:
  146. if (record->event.pressed) {
  147. mouse_timer = timer_read();
  148. mouse_singular_key = true;
  149. layer_invert(_MS);
  150. mouseLED = !mouseLED;
  151. } else {
  152. if (timer_elapsed(mouse_timer) < CUSTOM_TOGGLE_TIME && mouse_singular_key){
  153. //do nothing, it was a toggle (and it's already been toggled)
  154. } else {
  155. layer_invert(_MS);
  156. mouseLED = !mouseLED;
  157. }
  158. }
  159. returnVal = false;
  160. break;
  161. //Custom macros for strange keys with different long-tap behavior
  162. case CK_1G:
  163. if (shift_held && shift_suspended){
  164. register_code(KC_LSFT);
  165. shift_suspended = false;
  166. }
  167. if (record->event.pressed) {
  168. special_timers[CK_1G-SAFE_RANGE] = timer_read();
  169. special_key_pressed[CK_1G-SAFE_RANGE] = 1;
  170. } else {
  171. if (special_key_states[CK_1G-SAFE_RANGE]){
  172. //key was activated after custom_longpress, need to close those keycodes
  173. special_key_states[CK_1G-SAFE_RANGE] = 0;
  174. unregister_code(KC_GRAVE);
  175. } else {
  176. if (special_key_pressed[CK_1G-SAFE_RANGE]){
  177. //key was not activated, return macro activating proper, pre-long-tap key
  178. SEND_STRING(SS_TAP(X_1));
  179. special_key_pressed[CK_1G-SAFE_RANGE] = 0;
  180. } else {
  181. //the short key was already sent, because another key was pressed.
  182. //Do nothing.
  183. }
  184. }
  185. }
  186. returnVal = false;
  187. break;
  188. case CK_BSPE:
  189. if (shift_held && shift_suspended){
  190. register_code(KC_LSFT);
  191. shift_suspended = false;
  192. }
  193. if (record->event.pressed) {
  194. special_timers[CK_BSPE-SAFE_RANGE] = timer_read();
  195. special_key_pressed[CK_BSPE-SAFE_RANGE] = 1;
  196. } else {
  197. if (special_key_states[CK_BSPE-SAFE_RANGE]){
  198. //key was activated after custom_longpress, need to close those keycodes
  199. special_key_states[CK_BSPE-SAFE_RANGE] = 0;
  200. unregister_code(KC_ENTER);
  201. } else {
  202. if (special_key_pressed[CK_BSPE-SAFE_RANGE]){
  203. //key was not activated, return macro activating proper, pre-long-tap key
  204. SEND_STRING(SS_TAP(X_BSLASH));
  205. special_key_pressed[CK_BSPE-SAFE_RANGE] = 0;
  206. } else {
  207. //the short key was already sent, because another key was pressed.
  208. //Do nothing.
  209. }
  210. }
  211. }
  212. returnVal = false;
  213. break;
  214. case CK_QE:
  215. if (shift_held && shift_suspended){
  216. register_code(KC_LSFT);
  217. shift_suspended = false;
  218. }
  219. if (record->event.pressed) {
  220. special_timers[CK_QE-SAFE_RANGE] = timer_read();
  221. special_key_pressed[CK_QE-SAFE_RANGE] = 1;
  222. } else {
  223. if (special_key_states[CK_QE-SAFE_RANGE]){
  224. //key was activated after custom_longpress, need to close those keycodes
  225. special_key_states[CK_QE-SAFE_RANGE] = 0;
  226. unregister_code(KC_ENTER);
  227. } else {
  228. if (special_key_pressed[CK_QE-SAFE_RANGE]){
  229. //the long-press key was not activated, return macro activating proper, pre-long-tap key
  230. SEND_STRING(SS_TAP(X_QUOTE));
  231. special_key_pressed[CK_QE-SAFE_RANGE] = 0;
  232. } else {
  233. //the short key was already sent, because another key was pressed.
  234. //Do nothing.
  235. }
  236. }
  237. }
  238. returnVal = false;
  239. break;
  240. case CK_TE:
  241. if (shift_held && shift_suspended){
  242. register_code(KC_LSFT);
  243. shift_suspended = false;
  244. }
  245. if (record->event.pressed) {
  246. special_timers[CK_TE-SAFE_RANGE] = timer_read();
  247. special_key_pressed[CK_TE-SAFE_RANGE] = 1;
  248. } else {
  249. if (special_key_states[CK_TE-SAFE_RANGE]){
  250. //key was activated after custom_longpress, need to close those keycodes
  251. special_key_states[CK_TE-SAFE_RANGE] = 0;
  252. unregister_code(KC_ESCAPE);
  253. } else {
  254. if (special_key_pressed[CK_TE-SAFE_RANGE]){
  255. //the long-press key was not activated, return macro activating proper, pre-long-tap key
  256. SEND_STRING(SS_TAP(X_TAB));
  257. special_key_pressed[CK_TE-SAFE_RANGE] = 0;
  258. } else {
  259. //the short key was already sent, because another key was pressed.
  260. //Do nothing.
  261. }
  262. }
  263. }
  264. returnVal = false;
  265. break;
  266. //No-shift keys, they unregister the KC_LSFT code so they can send
  267. //unshifted values - but they don't change the bool. if any other
  268. //key is pressed and the bool is set, KC_LSFT is registered again.
  269. case NS_HYPH:
  270. if (record->event.pressed) {
  271. shift_suspended = true;
  272. unregister_code(KC_LSFT);
  273. register_code(KC_MINS);
  274. } else {
  275. unregister_code(KC_MINS);
  276. if (shift_held && shift_suspended){
  277. register_code(KC_LSFT);
  278. shift_suspended = false;
  279. }
  280. }
  281. returnVal = false;
  282. break;
  283. case NS_EQU:
  284. if (record->event.pressed) {
  285. shift_suspended = true;
  286. unregister_code(KC_LSFT);
  287. register_code(KC_EQUAL);
  288. } else {
  289. unregister_code(KC_EQUAL);
  290. if (shift_held && shift_suspended){
  291. register_code(KC_LSFT);
  292. shift_suspended = false;
  293. }
  294. }
  295. returnVal = false;
  296. break;
  297. //mouse buttons, for 1-3, to update the mouse report:
  298. case MS_BTN1:
  299. currentReport = pointing_device_get_report();
  300. if (record->event.pressed) {
  301. if (shift_held && shift_suspended){
  302. register_code(KC_LSFT);
  303. shift_suspended = false;
  304. }
  305. //update mouse report here
  306. currentReport.buttons |= MOUSE_BTN1; //MOUSE_BTN1 is a const defined in report.h
  307. } else {
  308. //update mouse report here
  309. currentReport.buttons &= ~MOUSE_BTN1;
  310. }
  311. pointing_device_set_report(currentReport);
  312. returnVal = false;
  313. break;
  314. case MS_BTN2:
  315. currentReport = pointing_device_get_report();
  316. if (record->event.pressed) {
  317. if (shift_held && shift_suspended){
  318. register_code(KC_LSFT);
  319. shift_suspended = false;
  320. }
  321. //update mouse report here
  322. currentReport.buttons |= MOUSE_BTN2; //MOUSE_BTN2 is a const defined in report.h
  323. } else {
  324. //update mouse report here
  325. currentReport.buttons &= ~MOUSE_BTN2;
  326. }
  327. pointing_device_set_report(currentReport);
  328. returnVal = false;
  329. break;
  330. case MS_BTN3:
  331. currentReport = pointing_device_get_report();
  332. if (record->event.pressed) {
  333. if (shift_held && shift_suspended){
  334. register_code(KC_LSFT);
  335. shift_suspended = false;
  336. }
  337. //update mouse report here
  338. currentReport.buttons |= MOUSE_BTN3; //MOUSE_BTN3 is a const defined in report.h
  339. } else {
  340. //update mouse report here
  341. currentReport.buttons &= ~MOUSE_BTN3;
  342. }
  343. pointing_device_set_report(currentReport);
  344. returnVal = false;
  345. break;
  346. //Additionally, if NS_ keys are in use, then shift may be held (but is
  347. //disabled for the unshifted keycodes to be send. Check the bool and
  348. //register shift as necessary.
  349. default:
  350. if (shift_held){
  351. register_code(KC_LSFT);
  352. }
  353. break;
  354. }
  355. switch (keycode){
  356. case KC_BSPC:
  357. case KC_NO:
  358. case NUMKEY:
  359. case SFTKEY:
  360. case MOUKEY:
  361. //don't want to reset single key variables
  362. break;
  363. default:
  364. //If any other key was pressed during the layer mod hold period,
  365. //then the layer mod was used momentarily, and should block latching
  366. shift_singular_key = false;
  367. number_singular_key = false;
  368. mouse_singular_key = false;
  369. break;
  370. }
  371. switch (keycode){
  372. case KC_BSPC:
  373. case KC_NO:
  374. case NUMKEY:
  375. case SFTKEY:
  376. case MOUKEY:
  377. case MOUSE_BTN1:
  378. case MOUSE_BTN2:
  379. case MOUSE_BTN3:
  380. case KC_LCTL:
  381. case KC_LALT:
  382. case KC_LGUI:
  383. case KC_RCTL:
  384. case KC_RALT:
  385. case KC_RGUI:
  386. case CK_1G:
  387. case CK_BSPE:
  388. case CK_QE:
  389. case CK_TE:
  390. //Do nothing, don't want to trigger the timer key rollover
  391. break;
  392. default:
  393. //Now we're checking to see if any of the special timer keys are pressed
  394. //if so, we need to activate their short-press features
  395. if (record->event.pressed) {
  396. for (uint8_t i = 0; i<LONGPRESS_COUNT; i++){
  397. if ((!special_key_states[i]) && special_key_pressed[i]){
  398. switch (i + SAFE_RANGE){
  399. case CK_1G:
  400. SEND_STRING(SS_TAP(X_1));
  401. break;
  402. case CK_BSPE:
  403. SEND_STRING(SS_TAP(X_BSLASH));
  404. break;
  405. case CK_QE:
  406. SEND_STRING(SS_TAP(X_QUOTE));
  407. break;
  408. case CK_TE:
  409. SEND_STRING(SS_TAP(X_TAB));
  410. break;
  411. }
  412. special_key_pressed[i] = 0;
  413. }
  414. }
  415. } else {
  416. //do nothing, we don't want to trigger short presses on key releases.
  417. }
  418. break;
  419. }
  420. return returnVal;
  421. };
  422. void matrix_scan_user(void) {
  423. //uint8_t layer = biton32(layer_state);
  424. for (uint8_t i = 0; i<LONGPRESS_COUNT; i++){
  425. if ((timer_elapsed(special_timers[i]) >= CUSTOM_LONGPRESS) && (!special_key_states[i]) && special_key_pressed[i]){
  426. switch (i + SAFE_RANGE){
  427. case CK_1G:
  428. register_code(KC_GRAVE);
  429. break;
  430. case CK_BSPE:
  431. register_code(KC_ENTER);
  432. break;
  433. case CK_QE:
  434. register_code(KC_ENTER);
  435. break;
  436. case CK_TE:
  437. register_code(KC_ESCAPE);
  438. break;
  439. }
  440. special_key_pressed[i] = 0;
  441. special_key_states[i] = 1;
  442. }
  443. }
  444. if (shiftLED || capsLED){
  445. red_timer++;
  446. if (red_timer < RED_BRIGHTNESS){
  447. red_led_on();
  448. } else {
  449. red_timer = 0;
  450. red_led_off();
  451. }
  452. } else {
  453. red_timer = 0;
  454. red_led_off();
  455. }
  456. if (numLED){
  457. green_timer++;
  458. if (green_timer < GREEN_BRIGHTNESS){
  459. grn_led_on();
  460. } else {
  461. green_timer = 0;
  462. grn_led_off();
  463. }
  464. } else {
  465. green_timer = 0;
  466. grn_led_off();
  467. }
  468. if (mouseLED){
  469. blue_timer++;
  470. if (blue_timer < BLUE_BRIGHTNESS){
  471. blu_led_on();
  472. } else {
  473. blue_timer = 0;
  474. blu_led_off();
  475. }
  476. } else {
  477. blue_timer = 0;
  478. blu_led_off();
  479. }
  480. };