keymap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 QMK_KEYBOARD_H
  4. #include "pointing_device.h"
  5. // Each layer gets a name for readability, which is then used in the keymap matrix below.
  6. // The underscores don't mean anything - you can have a layer called STUFF or any other name.
  7. // Layer names don't all need to be of the same length, obviously, and you can also skip them
  8. // entirely and just use numbers.
  9. enum dichotemy_layers
  10. {
  11. _BS,
  12. _SF,
  13. _NM,
  14. _NS,
  15. _MS
  16. };
  17. #define LONGPRESS_COUNT 4
  18. enum dichotemy_keycodes
  19. {
  20. CK_1G = SAFE_RANGE,
  21. CK_BSPE,
  22. CK_QE,
  23. 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.
  24. NS_HYPH,
  25. NS_EQU,
  26. NUMKEY,
  27. SFTKEY,
  28. MOUSE,
  29. MS_BTN1,
  30. MS_BTN2
  31. //MS_BTN3
  32. };
  33. // Macro definitions for readability
  34. enum dichotemy_macros
  35. {
  36. VOLU,
  37. VOLD,
  38. ESCM
  39. };
  40. #define LONGPRESS_DELAY 150
  41. #define MAX_TOGGLE_LENGTH 300
  42. #define TAPPING_TOGGLE 1
  43. // Fillers to make layering more clear
  44. #define _______ KC_TRNS
  45. #define XXXXXXX KC_NO
  46. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  47. [_BS] = LAYOUT( /* Base layout, nearly qwerty but with modifications because it's not a full keyboard. Obviously. */
  48. CK_TE, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_BSPC,
  49. NUMKEY, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, CK_QE,
  50. SFTKEY, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, MOUSE,
  51. KC_LCTL, KC_LALT, KC_LGUI, KC_RGUI, KC_RALT, KC_RCTL,
  52. KC_LBRC, KC_LPRN, KC_QUOT, KC_SPC, KC_RPRN, KC_RBRC
  53. ),
  54. [_SF] = LAYOUT( /* Shifted layout, small changes (because angle brackets have been moved to thumb cluster buttons) */
  55. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  56. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  57. _______, _______, _______, _______, _______, _______, _______, _______, NS_HYPH, KC_UNDS, _______, _______,
  58. _______, _______, _______, _______, _______, _______,
  59. _______, KC_LABK, _______, _______, KC_RABK, _______
  60. ),
  61. [_NM] = LAYOUT( /* Number layout, basically the main function layer */
  62. _______, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, _______,
  63. _______, CK_1G, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, CK_BSPE,
  64. _______, KC_F11, KC_F12, KC_F13, KC_F14, KC_F15, KC_F16, KC_F17, KC_F18, KC_F19, KC_F20, _______,
  65. _______, _______, _______, _______, _______, _______,
  66. _______, _______, _______, _______, _______, _______
  67. ),
  68. [_NS] = LAYOUT( /* Shifted number/function layout, for per-key control. Only active when shift is held, and number is toggled or held */
  69. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  70. _______, _______, _______, _______, _______, _______, _______, _______, _______, KC_PLUS, NS_EQU, _______,
  71. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  72. _______, _______, _______, _______, _______, _______,
  73. _______, _______, _______, _______, _______, _______
  74. ),
  75. [_MS] = LAYOUT( /* Mouse layer, including buttons for clicking. */
  76. _______, _______, _______, _______, _______, _______, KC_VOLU, KC_HOME, KC_PGUP, _______, _______, _______,
  77. _______, _______, _______, _______, _______, _______, _______, MS_BTN1, MS_BTN2, _______, _______, _______,
  78. _______, _______, _______, _______, _______, _______, KC_VOLD, KC_END, KC_PGDN, _______, _______, _______,
  79. _______, _______, _______, _______, KC_UP, _______,
  80. _______, _______, _______, KC_LEFT, KC_DOWN, KC_RGHT
  81. )
  82. };
  83. const uint16_t PROGMEM fn_actions[] = {
  84. };
  85. static uint16_t special_timers[LONGPRESS_COUNT] = {0xFFFF,0xFFFF,0xFFFF,0xFFFF};
  86. static bool special_key_states[LONGPRESS_COUNT] = {0,0,0,0};
  87. static uint16_t shift_timer;
  88. static uint16_t num_timer;
  89. static uint16_t mouse_timer;
  90. static bool shift_singular_key = false;
  91. static bool number_singular_key = false;
  92. static bool mouse_singular_key = false;
  93. static bool shift_held = false;
  94. static bool shift_suspended = false;
  95. report_mouse_t currentReport = {};
  96. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  97. //uint8_t layer;
  98. //layer = biton32(layer_state); // get the current layer //Or don't, I didn't use it.
  99. //custom layer handling for tri_layer,
  100. switch (keycode) {
  101. case NUMKEY:
  102. if (record->event.pressed) {
  103. num_timer = timer_read();
  104. number_singular_key = true;
  105. layer_invert(_NM);
  106. } else {
  107. if (timer_elapsed(num_timer) < MAX_TOGGLE_LENGTH && number_singular_key) {
  108. //do nothing, the layer has already been inverted
  109. } else {
  110. layer_invert(_NM);
  111. }
  112. }
  113. update_tri_layer(_NM, _SF, _NS);
  114. return false;
  115. break;
  116. //SHIFT is handled as LSHIFT in the general case - 'toggle' shoudl activate caps, while the layer is only active when shift is held.
  117. case SFTKEY:
  118. if (record->event.pressed) {
  119. shift_held = true;
  120. shift_suspended = false;
  121. shift_timer = timer_read();
  122. shift_singular_key = true;
  123. layer_on(_SF);
  124. register_code(KC_LSFT);
  125. } else {
  126. shift_held = false;
  127. if (timer_elapsed(shift_timer) < MAX_TOGGLE_LENGTH && shift_singular_key) {
  128. //this was basically a toggle, so activate/deactivate caps lock.
  129. SEND_STRING(SS_TAP(X_CAPSLOCK));
  130. }
  131. layer_off(_SF);
  132. unregister_code(KC_LSFT);
  133. }
  134. update_tri_layer(_NM, _SF, _NS);
  135. return false;
  136. break;
  137. //MOUSE layer needs to be handled the same way as NUMKEY, but differently from shift
  138. case MOUSE:
  139. if (record->event.pressed) {
  140. mouse_timer = timer_read();
  141. mouse_singular_key = true;
  142. layer_invert(_MS);
  143. } else {
  144. if (timer_elapsed(mouse_timer) < MAX_TOGGLE_LENGTH && number_singular_key){
  145. //do nothing, it was a toggle (and it's already been toggled)
  146. } else {
  147. layer_invert(_MS);
  148. }
  149. }
  150. return false;
  151. break;
  152. //Custom macros for strange keys with different long-tap behavior
  153. case CK_1G:
  154. if (shift_held && shift_suspended){
  155. register_code(KC_LSFT);
  156. shift_suspended = false;
  157. }
  158. shift_singular_key = false;
  159. number_singular_key = false;
  160. mouse_singular_key = false;
  161. if (record->event.pressed) {
  162. special_timers[CK_1G-SAFE_RANGE] = timer_read();
  163. } else {
  164. if (special_key_states[CK_1G-SAFE_RANGE]){
  165. //key was activated after longpress_delay, need to close those keycodes
  166. special_key_states[CK_1G-SAFE_RANGE] = 0;
  167. unregister_code(KC_GRAVE);
  168. } else {
  169. //key was not activated, return macro activating proper, pre-long-tap key
  170. SEND_STRING(SS_TAP(X_1));
  171. }
  172. special_timers[CK_1G-SAFE_RANGE] = 0xFFFF;
  173. }
  174. break;
  175. case CK_BSPE:
  176. if (shift_held && shift_suspended){
  177. register_code(KC_LSFT);
  178. shift_suspended = false;
  179. }
  180. shift_singular_key = false;
  181. number_singular_key = false;
  182. mouse_singular_key = false;
  183. if (record->event.pressed) {
  184. special_timers[CK_BSPE-SAFE_RANGE] = timer_read();
  185. } else {
  186. if (special_key_states[CK_BSPE-SAFE_RANGE]){
  187. //key was activated after longpress_delay, need to close those keycodes
  188. special_key_states[CK_BSPE-SAFE_RANGE] = 0;
  189. unregister_code(KC_ENTER);
  190. } else {
  191. //key was not activated, return macro activating proper, pre-long-tap key
  192. SEND_STRING(SS_TAP(X_BSLASH));
  193. }
  194. special_timers[CK_BSPE-SAFE_RANGE] = 0xFFFF;
  195. }
  196. break;
  197. case CK_QE:
  198. if (shift_held && shift_suspended){
  199. register_code(KC_LSFT);
  200. shift_suspended = false;
  201. }
  202. shift_singular_key = false;
  203. number_singular_key = false;
  204. mouse_singular_key = false;
  205. if (record->event.pressed) {
  206. special_timers[CK_QE-SAFE_RANGE] = timer_read();
  207. } else {
  208. if (special_key_states[CK_QE-SAFE_RANGE]){
  209. //key was activated after longpress_delay, need to close those keycodes
  210. special_key_states[CK_QE-SAFE_RANGE] = 0;
  211. unregister_code(KC_ENTER);
  212. } else {
  213. //key was not activated, return macro activating proper, pre-long-tap key
  214. SEND_STRING(SS_TAP(X_QUOTE));
  215. }
  216. special_timers[CK_QE-SAFE_RANGE] = 0xFFFF;
  217. }
  218. break;
  219. case CK_TE:
  220. if (shift_held && shift_suspended){
  221. register_code(KC_LSFT);
  222. shift_suspended = false;
  223. }
  224. if (record->event.pressed) {
  225. special_timers[CK_TE-SAFE_RANGE] = timer_read();
  226. } else {
  227. if (special_key_states[CK_TE-SAFE_RANGE]){
  228. //key was activated after longpress_delay, need to close those keycodes
  229. special_key_states[CK_TE-SAFE_RANGE] = 0;
  230. unregister_code(KC_ENTER);
  231. } else {
  232. //key was not activated, return macro activating proper, pre-long-tap key
  233. SEND_STRING(SS_TAP(X_TAB));
  234. }
  235. special_timers[CK_TE-SAFE_RANGE] = 0xFFFF;
  236. }
  237. break;
  238. //No-shift keys, they unregister the KC_LSFT code so they can send
  239. //unshifted values - but they don't change the bool. if any other
  240. //key is pressed and the bool is set, KC_LSFT is registered again.
  241. case NS_HYPH:
  242. if (record->event.pressed) {
  243. shift_suspended = true;
  244. unregister_code(KC_LSFT);
  245. register_code(KC_MINS);
  246. } else {
  247. unregister_code(KC_MINS);
  248. if (shift_held && shift_suspended){
  249. register_code(KC_LSFT);
  250. shift_suspended = false;
  251. }
  252. }
  253. break;
  254. case NS_EQU:
  255. if (record->event.pressed) {
  256. shift_suspended = true;
  257. unregister_code(KC_LSFT);
  258. register_code(KC_EQUAL);
  259. } else {
  260. unregister_code(KC_EQUAL);
  261. if (shift_held && shift_suspended){
  262. register_code(KC_LSFT);
  263. shift_suspended = false;
  264. }
  265. }
  266. break;
  267. //mouse buttons, for 1-3, to update the mouse report:
  268. case MS_BTN1:
  269. currentReport = pointing_device_get_report();
  270. if (record->event.pressed) {
  271. if (shift_held && shift_suspended){
  272. register_code(KC_LSFT);
  273. shift_suspended = false;
  274. }
  275. //update mouse report here
  276. currentReport.buttons |= MOUSE_BTN1; //MOUSE_BTN1 is a const defined in report.h
  277. } else {
  278. //update mouse report here
  279. currentReport.buttons &= ~MOUSE_BTN1;
  280. }
  281. pointing_device_set_report(currentReport);
  282. break;
  283. case MS_BTN2:
  284. currentReport = pointing_device_get_report();
  285. if (record->event.pressed) {
  286. if (shift_held && shift_suspended){
  287. register_code(KC_LSFT);
  288. shift_suspended = false;
  289. }
  290. //update mouse report here
  291. currentReport.buttons |= MOUSE_BTN2; //MOUSE_BTN2 is a const defined in report.h
  292. } else {
  293. //update mouse report here
  294. }
  295. pointing_device_set_report(currentReport);
  296. break;
  297. //there is a case for button 3, but that's handled in dichotemy.c, and this is being
  298. //disabled to avoid any conflict.
  299. /*case MS_BTN3:
  300. currentReport = pointing_device_get_report();
  301. if (record->event.pressed) {
  302. if (shift_held && shift_suspended){
  303. register_code(KC_LSFT);
  304. shift_suspended = false;
  305. }
  306. //update mouse report here
  307. currentReport.buttons |= MOUSE_BTN3; //MOUSE_BTN2 is a const defined in report.h
  308. } else {
  309. //update mouse report here
  310. }
  311. pointing_device_set_report(currentReport);
  312. break;*/
  313. //If any other key was pressed during the layer mod hold period,
  314. //then the layer mod was used momentarily, and should block latching
  315. //Additionally, if NS_ keys are in use, then shift may be held (but is
  316. //disabled for the unshifted keycodes to be send. Check the bool and
  317. //register shift as necessary.
  318. default:
  319. if (shift_held){
  320. register_code(KC_LSFT);
  321. }
  322. shift_singular_key = false;
  323. number_singular_key = false;
  324. mouse_singular_key = false;
  325. break;
  326. }
  327. return true;
  328. };
  329. void matrix_scan_user(void) {
  330. uint8_t layer = biton32(layer_state);
  331. for (uint8_t i = 0; i<LONGPRESS_COUNT; i++){
  332. if (timer_elapsed(special_timers[i]) >= LONGPRESS_DELAY && !special_key_states[i]){
  333. switch (i + SAFE_RANGE){
  334. case CK_1G:
  335. register_code(KC_GRAVE);
  336. break;
  337. case CK_BSPE:
  338. register_code(KC_ENTER);
  339. break;
  340. case CK_QE:
  341. register_code(KC_ENTER);
  342. break;
  343. case CK_TE:
  344. register_code(KC_ESCAPE);
  345. break;
  346. }
  347. special_key_states[i] = 1;
  348. }
  349. }
  350. switch (layer) {
  351. case _BS:
  352. set_led_off;
  353. break;
  354. case _NM:
  355. set_led_blue;
  356. break;
  357. case _SF:
  358. set_led_red;
  359. break;
  360. case _NS:
  361. set_led_green;
  362. break;
  363. default:
  364. break;
  365. }
  366. };