process_unicode.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "process_unicode.h"
  2. static uint8_t input_mode;
  3. uint16_t hex_to_keycode(uint8_t hex)
  4. {
  5. if (hex == 0x0) {
  6. return KC_0;
  7. } else if (hex < 0xA) {
  8. return KC_1 + (hex - 0x1);
  9. } else {
  10. return KC_A + (hex - 0xA);
  11. }
  12. }
  13. void set_unicode_input_mode(uint8_t os_target)
  14. {
  15. input_mode = os_target;
  16. }
  17. void unicode_input_start (void) {
  18. switch(input_mode) {
  19. case UC_OSX:
  20. register_code(KC_LALT);
  21. break;
  22. case UC_LNX:
  23. register_code(KC_LCTL);
  24. register_code(KC_LSFT);
  25. register_code(KC_U);
  26. unregister_code(KC_U);
  27. unregister_code(KC_LSFT);
  28. unregister_code(KC_LCTL);
  29. break;
  30. case UC_WIN:
  31. register_code(KC_LALT);
  32. register_code(KC_PPLS);
  33. unregister_code(KC_PPLS);
  34. break;
  35. }
  36. }
  37. void unicode_input_finish (void) {
  38. switch(input_mode) {
  39. case UC_OSX:
  40. case UC_WIN:
  41. unregister_code(KC_LALT);
  42. break;
  43. case UC_LNX:
  44. register_code(KC_SPC);
  45. unregister_code(KC_SPC);
  46. break;
  47. }
  48. }
  49. void register_hex(uint16_t hex) {
  50. for(int i = 3; i >= 0; i--) {
  51. uint8_t digit = ((hex >> (i*4)) & 0xF);
  52. register_code(hex_to_keycode(digit));
  53. unregister_code(hex_to_keycode(digit));
  54. }
  55. }
  56. bool process_unicode(uint16_t keycode, keyrecord_t *record) {
  57. if (keycode > QK_UNICODE && record->event.pressed) {
  58. uint16_t unicode = keycode & 0x7FFF;
  59. unicode_input_start();
  60. register_hex(unicode);
  61. unicode_input_finish();
  62. }
  63. return true;
  64. }