From eca4bf087e28d6f4c91f06edf08f5f611682c890 Mon Sep 17 00:00:00 2001 From: Josh Klar Date: Mon, 8 Oct 2018 03:50:55 -0700 Subject: [PATCH 1/2] Ability to create emote/emoji macros from strings directly, on-board. --- kmk/common/macros/unicode.py | 14 ++++- kmk/common/util.py | 7 +++ .../kdb424/handwire_planck_pyboard.py | 8 +-- .../klardotsh/feather_m4_express/fourfour.py | 4 +- .../itsybitsy_m4_express/threethree.py | 60 ++++++++++++------- .../klardotsh/threethree_matrix_pyboard.py | 4 +- 6 files changed, 67 insertions(+), 30 deletions(-) diff --git a/kmk/common/macros/unicode.py b/kmk/common/macros/unicode.py index e9d04d5..a8d58e2 100644 --- a/kmk/common/macros/unicode.py +++ b/kmk/common/macros/unicode.py @@ -3,6 +3,7 @@ from kmk.common.event_defs import (hid_report_event, keycode_down_event, keycode_up_event) from kmk.common.keycodes import Common, Macro, Modifiers from kmk.common.macros.simple import simple_key_sequence +from kmk.common.util import get_wide_ordinal IBUS_KEY_COMBO = Modifiers.KC_LCTRL(Modifiers.KC_LSHIFT(Common.KC_U)) @@ -24,7 +25,18 @@ def generate_codepoint_keysym_seq(codepoint): return seq -def unicode_sequence(codepoints): +def unicode_string_sequence(unistring): + ''' + Allows sending things like (╯°□°)╯︵ ┻━┻ directly, without + manual conversion to Unicode codepoints. + ''' + return unicode_codepoint_sequence( + hex(get_wide_ordinal(s))[2:] + for s in unistring + ) + + +def unicode_codepoint_sequence(codepoints): def _unicode_sequence(state): if state.unicode_mode == UnicodeModes.IBUS: yield from _ibus_unicode_sequence(codepoints, state) diff --git a/kmk/common/util.py b/kmk/common/util.py index 2ff06d1..fa7f608 100644 --- a/kmk/common/util.py +++ b/kmk/common/util.py @@ -1,3 +1,10 @@ +def get_wide_ordinal(char): + if len(char) != 2: + return ord(char) + + return 0x10000 + (ord(char[0]) - 0xD800) * 0x400 + (ord(char[1]) - 0xDC00) + + def flatten_dict(d): items = {} diff --git a/user_keymaps/kdb424/handwire_planck_pyboard.py b/user_keymaps/kdb424/handwire_planck_pyboard.py index 9975c45..c49cef1 100644 --- a/user_keymaps/kdb424/handwire_planck_pyboard.py +++ b/user_keymaps/kdb424/handwire_planck_pyboard.py @@ -1,6 +1,6 @@ from kmk.common.consts import DiodeOrientation, LeaderMode, UnicodeModes from kmk.common.keycodes import KC -from kmk.common.macros.unicode import unicode_sequence +from kmk.common.macros.unicode import unicode_codepoint_sequence from kmk.common.pins import Pin as P from kmk.entrypoints.handwire.pyboard import main @@ -17,7 +17,7 @@ leader_timeout = 2000 DEBUG_ENABLE = True # -------------------------------Macros ----------------------------------------------- -FLIP = unicode_sequence([ +FLIP = unicode_codepoint_sequence([ "28", "30ce", "ca0", @@ -35,7 +35,7 @@ FLIP = unicode_sequence([ LEADER_DICTIONARY = { (KC.F, KC.L, KC.I, KC.P): - unicode_sequence([ + unicode_codepoint_sequence([ "28", "30ce", "ca0", @@ -49,7 +49,7 @@ LEADER_DICTIONARY = { "253b", ]), (KC.C, KC.H, KC.E, KC.E, KC.R): - unicode_sequence([ + unicode_codepoint_sequence([ '002B', 'FF61', '003A', diff --git a/user_keymaps/klardotsh/feather_m4_express/fourfour.py b/user_keymaps/klardotsh/feather_m4_express/fourfour.py index 11d084e..68b062d 100644 --- a/user_keymaps/klardotsh/feather_m4_express/fourfour.py +++ b/user_keymaps/klardotsh/feather_m4_express/fourfour.py @@ -1,7 +1,7 @@ from kmk.common.consts import DiodeOrientation, UnicodeModes from kmk.common.keycodes import KC from kmk.common.macros.simple import send_string, simple_key_sequence -from kmk.common.macros.unicode import unicode_sequence +from kmk.common.macros.unicode import unicode_codepoint_sequence from kmk.common.pins import Pin as P from kmk.entrypoints.handwire.feather_m4_express import main from kmk.firmware import Firmware @@ -31,7 +31,7 @@ MACRO_TEST_SIMPLE = simple_key_sequence([ MACRO_TEST_STRING = send_string("Hello! from, uhhhh, send_string | and some other WEIRD STUFF` \\ like this' \"\t[]") -ANGRY_TABLE_FLIP = unicode_sequence([ +ANGRY_TABLE_FLIP = unicode_codepoint_sequence([ "28", "30ce", "ca0", diff --git a/user_keymaps/klardotsh/itsybitsy_m4_express/threethree.py b/user_keymaps/klardotsh/itsybitsy_m4_express/threethree.py index af8746f..e8243aa 100644 --- a/user_keymaps/klardotsh/itsybitsy_m4_express/threethree.py +++ b/user_keymaps/klardotsh/itsybitsy_m4_express/threethree.py @@ -1,8 +1,9 @@ from kmk.common.consts import DiodeOrientation, UnicodeModes from kmk.common.keycodes import KC from kmk.common.macros.simple import send_string, simple_key_sequence -from kmk.common.macros.unicode import unicode_sequence +from kmk.common.macros.unicode import unicode_string_sequence from kmk.common.pins import Pin as P +from kmk.common.types import AttrDict from kmk.entrypoints.handwire.itsybitsy_m4_express import main from kmk.firmware import Firmware @@ -12,7 +13,40 @@ rows = (P.D12, P.D11, P.D10) diode_orientation = DiodeOrientation.COLUMNS unicode_mode = UnicodeModes.LINUX -MACRO_TEST_SIMPLE = simple_key_sequence([ +emoticons = AttrDict({ + # Emojis + 'BEER': r'🍺', + 'BEER_TOAST': r'🍻', + 'FACE_CUTE_SMILE': r'😊', + 'FACE_HEART_EYES': r'😍', + 'FACE_JOY': r'😂', + 'FACE_SWEAT_SMILE': r'😅', + 'FACE_THINKING': r'🤔', + 'FIRE': r'🔥', + 'FLAG_CA': r'🇨🇦', + 'FLAG_US': r'🇺🇸', + 'HAND_CLAP': r'👏', + 'HAND_HORNS': r'🤘', + 'HAND_OK': r'👌', + 'HAND_THUMB_DOWN': r'👎', + 'HAND_THUMB_UP': r'👍', + 'HAND_WAVE': r'👋', + 'HEART': r'❤️', + 'MAPLE_LEAF': r'🍁', + 'POOP': r'💩', + 'TADA': r'🎉', + + # Emoticons, but fancier + 'ANGRY_TABLE_FLIP': r'(ノಠ痊ಠ)ノ彡┻━┻', + 'CELEBRATORY_GLITTER': r'+。:.゚ヽ(´∀。)ノ゚.:。+゚゚+。:.゚ヽ(*´∀)ノ゚.:。+゚', + 'SHRUGGIE': r'¯\_(ツ)_/¯', + 'TABLE_FLIP': r'(╯°□°)╯︵ ┻━┻', +}) + +for k, v in emoticons.items(): + emoticons[k] = unicode_string_sequence(v) + +MACRO_HELLO_WORLD = simple_key_sequence([ KC.LSHIFT(KC.H), KC.E, KC.L, @@ -29,22 +63,6 @@ MACRO_TEST_SIMPLE = simple_key_sequence([ KC.EXCLAIM, ]) -MACRO_TEST_STRING = send_string("Hello! from, uhhhh, send_string | and some other WEIRD STUFF` \\ like this' \"\t[]") - -ANGRY_TABLE_FLIP = unicode_sequence([ - "28", - "30ce", - "ca0", - "75ca", - "ca0", - "29", - "30ce", - "5f61", - "253b", - "2501", - "253b", -]) - keymap = [ [ [KC.GESC, KC.A, KC.RESET], @@ -57,8 +75,8 @@ keymap = [ [KC.F, KC.G, KC.H], ], [ - [KC.VOLU, KC.MUTE, ANGRY_TABLE_FLIP], - [KC.NO, KC.PIPE, MACRO_TEST_SIMPLE], - [KC.TRNS, KC.P, MACRO_TEST_STRING], + [emoticons.CELEBRATORY_GLITTER, emoticons.SHRUGGIE, emoticons.ANGRY_TABLE_FLIP], + [emoticons.BEER, emoticons.FLAG_CA, emoticons.FLAG_US], + [KC.TRNS, KC.P, MACRO_HELLO_WORLD], ], ] diff --git a/user_keymaps/klardotsh/threethree_matrix_pyboard.py b/user_keymaps/klardotsh/threethree_matrix_pyboard.py index b334317..0ee5682 100644 --- a/user_keymaps/klardotsh/threethree_matrix_pyboard.py +++ b/user_keymaps/klardotsh/threethree_matrix_pyboard.py @@ -1,7 +1,7 @@ from kmk.common.consts import DiodeOrientation, UnicodeModes from kmk.common.keycodes import KC from kmk.common.macros.simple import send_string, simple_key_sequence -from kmk.common.macros.unicode import unicode_sequence +from kmk.common.macros.unicode import unicode_codepoint_sequence from kmk.common.pins import Pin as P from kmk.entrypoints.handwire.pyboard import main @@ -30,7 +30,7 @@ MACRO_TEST_SIMPLE = simple_key_sequence([ MACRO_TEST_STRING = send_string("Hello! from, uhhhh, send_string | and some other WEIRD STUFF` \\ like this' \"\t[]") -ANGRY_TABLE_FLIP = unicode_sequence([ +ANGRY_TABLE_FLIP = unicode_codepoint_sequence([ "28", "30ce", "ca0", From aa423e369013fe8bf938df296685ba14096ba906 Mon Sep 17 00:00:00 2001 From: Josh Klar Date: Mon, 8 Oct 2018 03:59:16 -0700 Subject: [PATCH 2/2] Fix an exhausted generator bug by converting to list --- kmk/common/macros/unicode.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kmk/common/macros/unicode.py b/kmk/common/macros/unicode.py index a8d58e2..4416459 100644 --- a/kmk/common/macros/unicode.py +++ b/kmk/common/macros/unicode.py @@ -30,10 +30,10 @@ def unicode_string_sequence(unistring): Allows sending things like (╯°□°)╯︵ ┻━┻ directly, without manual conversion to Unicode codepoints. ''' - return unicode_codepoint_sequence( + return unicode_codepoint_sequence([ hex(get_wide_ordinal(s))[2:] for s in unistring - ) + ]) def unicode_codepoint_sequence(codepoints):