diff --git a/code.py b/code.py index d792863..2fc7dec 100644 --- a/code.py +++ b/code.py @@ -1,6 +1,6 @@ from keyboard import * - +import my_keymap keyboard = Keyboard() @@ -17,75 +17,18 @@ SCC = MODS_TAP(MODS(RCTRL), ';') SINS = MODS_KEY(MODS(SHIFT), INSERT) -keyboard.keymap = ( - # layer 0 - ( - ESC, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, '-', '=', BACKSPACE, - TAB, Q, W, E, R, T, Y, U, I, O, P, '[', ']', '|', - CAPS, A, S, L2D, F, G, H, J, K, L, SCC, '"', ENTER, - LSFT4, Z, X, C, V, L3B, N, M, ',', '.', '/', RSFT4, - LCTRL, LGUI, LALT, SPACE, RALT, MENU, L1, RCTRL - ), - - # layer 1 - ( - '`', F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, DEL, - ___, ___, UP, ___, ___, ___, ___, ___, ___, ___,SUSPEND,___,___,___, - ___,LEFT,DOWN,RIGHT,___, ___, ___, ___, ___, ___, ___, ___, ___, - ___, ___, ___, ___, ___,BOOT, ___,MACRO(0), ___, ___, ___, ___, - ___, ___, ___, ___, ___, ___, ___, ___ - ), - - # layer 2 - ( - '`', F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, DEL, - ___, ___, ___, ___, ___, ___,HOME,PGUP, ___, ___,SINS,AUDIO_VOL_DOWN,AUDIO_VOL_UP,AUDIO_MUTE, - ___, ___, ___, ___, ___, ___,LEFT,DOWN, UP,RIGHT, ___, ___, ___, - ___, ___, ___, ___, ___, ___,PGDN,END, ___, ___, ___, ___, - ___, ___, ___, ___, ___, ___, ___, ___ - ), - - # layer 3 - ( - BT_TOGGLE,BT1,BT2, BT3,BT4,BT5,BT6,BT7, BT8, BT9, BT0, ___, ___, ___, - RGB_MOD, ___, ___, ___, ___, ___,___,USB_TOGGLE,___,___,___,___,___, ___, - RGB_TOGGLE,HUE_RGB,RGB_HUE,SAT_RGB,RGB_SAT,___,___,___,___,___,___,___, ___, - ___, ___, ___, ___, ___, ___, ___, ___,VAL_RGB,RGB_VAL, ___, ___, - ___, ___, ___, ___, ___, ___, ___, ___ - ), - - # layer 4 - ( - '`', ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, - ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, - ___, ___, ___, D, ___, ___, ___, ___, ___, ___, ';', ___, ___, - ___, ___, ___, ___, ___, B, ___, ___, ___, ___, ___, ___, - ___, ___, ___, ___, ___, ___, ___, ___ - ), - - # layer 5 - ( - ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, - ___, ___, ___, ___, ___, ___,MS_W_UP,MS_UL,MS_UP,MS_UR, ___, ___, ___, ___, - ___, ___, ___, ___, ___, ___,MS_BTN1,MS_LT,MS_DN,MS_RT,MS_BTN2, ___, ___, - ___, ___, ___, ___, ___, ___,MS_W_DN,MS_DL,MS_DN,MS_DR, ___, ___, - ___, ___, ___, ___, ___, ___, ___, ___ - ), -) +if my_keymap.custom_keymap and len(my_keymap.custom_keymap) > 0: + keyboard.keymap = my_keymap.custom_keymap -# Use different keymaps on different connections -# Valid keys are "USB" and "BT0"-"BT9" -# Connection not in this map will use default keymap defined above. -keyboard.profiles = { - # For example, BT8 is connected to a Mac - "BT8": ( +else: + keyboard.keymap = ( # layer 0 ( ESC, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, '-', '=', BACKSPACE, TAB, Q, W, E, R, T, Y, U, I, O, P, '[', ']', '|', - CAPS, A, S, D, F, G, H, J, K, L, SCC, '"', ENTER, - LSHIFT,Z, X, C, V, B, N, M, ',', '.', '/', RSHIFT, - LCTRL, LALT, LGUI, SPACE, MENU, RALT, L1, RCTRL + CAPS, A, S, L2D, F, G, H, J, K, L, SCC, '"', ENTER, + LSFT4, Z, X, C, V, L3B, N, M, ',', '.', '/', RSFT4, + LCTRL, LGUI, LALT, SPACE, RALT, MENU, L1, RCTRL ), # layer 1 @@ -93,11 +36,96 @@ '`', F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, DEL, ___, ___, UP, ___, ___, ___, ___, ___, ___, ___,SUSPEND,___,___,___, ___,LEFT,DOWN,RIGHT,___, ___, ___, ___, ___, ___, ___, ___, ___, - ___, ___, ___, ___, ___,BOOT, ___,MACRO(1), ___, ___, ___, ___, + ___, ___, ___, ___, ___,BOOT, ___,MACRO(0), ___, ___, ___, ___, + ___, ___, ___, ___, ___, ___, ___, ___ + ), + + # layer 2 + ( + '`', F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, DEL, + ___, ___, ___, ___, ___, ___,HOME,PGUP, ___, ___,SINS,AUDIO_VOL_DOWN,AUDIO_VOL_UP,AUDIO_MUTE, + ___, ___, ___, ___, ___, ___,LEFT,DOWN, UP,RIGHT, ___, ___, ___, + ___, ___, ___, ___, ___, ___,PGDN,END, ___, ___, ___, ___, + ___, ___, ___, ___, ___, ___, ___, ___ + ), + + # layer 3 + ( + BT_TOGGLE,BT1,BT2, BT3,BT4,BT5,BT6,BT7, BT8, BT9, BT0, ___, ___, ___, + RGB_MOD, ___, ___, ___, ___, ___,___,USB_TOGGLE,___,___,___,___,___, ___, + RGB_TOGGLE,HUE_RGB,RGB_HUE,SAT_RGB,RGB_SAT,___,___,___,___,___,___,___, ___, + ___, ___, ___, ___, ___, ___, ___, ___,VAL_RGB,RGB_VAL, ___, ___, + ___, ___, ___, ___, ___, ___, ___, ___ + ), + + # layer 4 + ( + '`', ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, + ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, + ___, ___, ___, D, ___, ___, ___, ___, ___, ___, ';', ___, ___, + ___, ___, ___, ___, ___, B, ___, ___, ___, ___, ___, ___, + ___, ___, ___, ___, ___, ___, ___, ___ + ), + + # layer 5 + ( + ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, + ___, ___, ___, ___, ___, ___,MS_W_UP,MS_UL,MS_UP,MS_UR, ___, ___, ___, ___, + ___, ___, ___, ___, ___, ___,MS_BTN1,MS_LT,MS_DN,MS_RT,MS_BTN2, ___, ___, + ___, ___, ___, ___, ___, ___,MS_W_DN,MS_DL,MS_DN,MS_DR, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___ ), ) -} + + +# Use different keymaps on different connections +# Valid keys are "USB" and "BT0"-"BT9" +# Connection not in this map will use default keymap defined above. +if my_keymap.profiles and len(my_keymap.profiles) > 0: + keyboard.profiles = my_keymap.profiles +else: + keyboard.profiles = { + # For example, BT8 is connected to a Mac + "BT8": ( + # layer 0 + ( + ESC, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, '-', '=', BACKSPACE, + TAB, Q, W, E, R, T, Y, U, I, O, P, '[', ']', '|', + CAPS, A, S, D, F, G, H, J, K, L, SCC, '"', ENTER, + LSHIFT,Z, X, C, V, B, N, M, ',', '.', '/', RSHIFT, + LCTRL, LALT, LGUI, SPACE, MENU, RALT, L1, RCTRL + ), + + # layer 1 + ( + '`', F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, DEL, + ___, ___, UP, ___, ___, ___, ___, ___, ___, ___,SUSPEND,___,___,___, + ___,LEFT,DOWN,RIGHT,___, ___, ___, ___, ___, ___, ___, ___, ___, + ___, ___, ___, ___, ___,BOOT, ___,MACRO(1), ___, ___, ___, ___, + ___, ___, ___, ___, ___, ___, ___, ___ + ), + ) + } + + +# add default keymap to profiles +default_keymap_count = len(keyboard.keymap) +for i in keyboard.profiles.keys(): + profile_key_maps = keyboard.profiles.get(i) + profile_layer_count = len(profile_key_maps) + + # insert default + keymap_profile = list(profile_key_maps) + for x in range(profile_layer_count, default_keymap_count): + default_keymap = keyboard.keymap[x] + keymap_profile.append(default_keymap) + + keyboard.profiles[i] = tuple(keymap_profile) + + +#print(keyboard.profiles) +print('keyboard.keymap = #{}'.format(len(keyboard.keymap))) +print('keyboard.profiles = #{}'.format(len(keyboard.profiles))) def macro_handler(dev, n, is_down): if is_down: @@ -109,18 +137,24 @@ def pairs_handler(dev, n): dev.send_text('You just triggered pair keys #{}\n'.format(n)) -keyboard.macro_handler = macro_handler -keyboard.pairs_handler = pairs_handler -# ESC(0) 1(1) 2(2) 3(3) 4(4) 5(5) 6(6) 7(7) 8(8) 9(9) 0(10) -(11) =(12) BACKSPACE(13) -# TAB(27) Q(26) W(25) E(24) R(23) T(22) Y(21) U(20) I(19) O(18) P(17) [(16) ](15) \(14) -# CAPS(28) A(29) S(30) D(31) F(32) G(33) H(34) J(35) K(36) L(37) ;(38) "(39) ENTER(40) -#LSHIFT(52) Z(51) X(50) C(49) V(48) B(47) N(46) M(45) ,(44) .(43) /(42) RSHIFT(41) -# LCTRL(53) LGUI(54) LALT(55) SPACE(56) RALT(57) MENU(58) Fn(59) RCTRL(60) +if my_keymap.macro_handler: + keyboard.macro_handler = my_keymap.macro_handler +else: + keyboard.macro_handler = macro_handler + +if my_keymap.pairs_handler: + keyboard.pairs_handler = my_keymap.pairs_handler +else: + keyboard.pairs_handler = pairs_handler # Pairs: J & K, U & I -keyboard.pairs = [{35, 36}, {20, 19}] +if my_keymap.pairs is None: + keyboard.pairs = [{35, 36}, {20, 19}] +else: + keyboard.pairs = my_keymap.pairs # keyboard.verbose = False +print('keyboard.pairs = #{}'.format(len(keyboard.pairs))) keyboard.run() diff --git a/keyboard/__init__.py b/keyboard/__init__.py index 847124f..b22ec8c 100644 --- a/keyboard/__init__.py +++ b/keyboard/__init__.py @@ -193,7 +193,14 @@ def get_key_sequence_info(self, start, end): else: desc += " / " t1 = matrix.get_keyup_time(key) - dt = matrix.ms(t1 - t0) + + dt = 0 + try: + dt = matrix.ms(t1 - t0) + + except OverflowError: + print('An exception flew by!') + t0 = t1 t.append(dt) @@ -267,7 +274,7 @@ def set_bt_id(self, n): address = _bleio.Address(uid, _bleio.Address.RANDOM_STATIC) try: self.ble._adapter.address = address - name = "PYKB {}".format(n) + name = "galen's PYKB {}".format(n) self.advertisement.complete_name = name self.ble.name = name self.ble_id = n @@ -559,9 +566,16 @@ def run(self): if self.verbose: keydown_time = matrix.get_keydown_time(key) - dt = ms(matrix.time() - keydown_time) - dt2 = ms(keydown_time - last_time) - last_time = keydown_time + dt = 0 + dt2 = 0 + try: + dt = ms(matrix.time() - keydown_time) + dt2 = ms(keydown_time - last_time) + except OverflowError: + print('An exception flew by!') + finally: + last_time = keydown_time + print( "{} {} \\ {} latency {} | {}".format( key, key_name(key), hex(action_code), dt, dt2 diff --git a/keyboard/model/m60.py b/keyboard/model/m60.py index 7ebcb83..11ba950 100644 --- a/keyboard/model/m60.py +++ b/keyboard/model/m60.py @@ -166,7 +166,8 @@ def __init__(self): self.blackhole, self.pinwheel, self.beacon, - self.beacon2 + self.beacon2, + self.flow ) self.set_mode(6) self.enabled = False @@ -222,6 +223,36 @@ def toggle(self): self.set_mode(self.mode) else: self.off() +# ESC(0) 1(1) 2(2) 3(3) 4(4) 5(5) 6(6) 7(7) 8(8) 9(9) 0(10) -(11) =(12) BACKSPACE(13) +# TAB(27) Q(26) W(25) E(24) R(23) T(22) Y(21) U(20) I(19) O(18) P(17) [(16) ](15) \(14) +# CAPS(28) A(29) S(30) D(31) F(32) G(33) H(34) J(35) K(36) L(37) ;(38) "(39) ENTER(40) +#LSHIFT(52) Z(51) X(50) C(49) V(48) B(47) N(46) M(45) ,(44) .(43) /(42) RSHIFT(41) +# LCTRL(53) LGUI(54) LALT(55) SPACE(56) RALT(57) MENU(58) Fn(59) RCTRL(60) +#No.61 and No.62 are under the space key. No.63 is at the back of keyboard. + + + def flow(self): + stepby = 10 + #print(self.n) + i = self.n + j = int(i/stepby) + self.pixel(j, 0xff, 0xff, 0xff) + self.pixel(j-1 if j-1>=0 else 60, 0, 0, 0) + if j == 56: + self.pixel(61, 0xff, 0xff, 0xff) + self.pixel(62, 0xff, 0xff, 0xff) + else: + self.pixel(61, 0, 0, 0) + self.pixel(62, 0, 0, 0) + + + self.update() + #self.n = (i + 1) & (60*stepby+stepby-1) + self.n = i + 1 + if self.n > (60*stepby+stepby-1): + self.n = 0 + + return True def mono(self): self.on(*hsv_to_rgb(*self.hsv)) diff --git a/my_keymap.py b/my_keymap.py new file mode 100644 index 0000000..3f7a178 --- /dev/null +++ b/my_keymap.py @@ -0,0 +1,142 @@ +from keyboard import * + +___ = TRANSPARENT +BOOT = BOOTLOADER +L1 = LAYER_TAP(1) +L2D = LAYER_TAP(2, D) +L3B = LAYER_TAP(3, B) +LSFT4 = LAYER_MODS(4, MODS(LSHIFT)) +RSFT4 = LAYER_MODS(4, MODS(RSHIFT)) +L5S = LAYER_TAP(5, S) + +# Semicolon & Ctrl +SCC = MODS_TAP(MODS(RCTRL), ';') +SINS = MODS_KEY(MODS(SHIFT), INSERT) + +custom_keymap = ( + # layer 0 + ( + ESC, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, '-', '=', BACKSPACE, + TAB, Q, W, E, R, T, Y, U, I, O, P, '[', ']', '|', + CAPS, A, L5S, L2D, F, G, H, J, K, L, SCC, '"', ENTER, + LSFT4, Z, X, C, V, L3B, N, M, ',', '.', '/', RSFT4, + LCTRL, LGUI, LALT, SPACE, RALT, MENU, RCTRL, L1 + ), + + # layer 1 + ( + '`', F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, DEL, + ___, ___, UP, ___, ___, ___, ___, ___, ___, ___,SUSPEND,___,___,___, + ___,LEFT,DOWN,RIGHT,___, ___, ___, ___, ___, ___, ___, ___, ___, + ___, ___, ___, ___, ___,BOOT, ___,MACRO(0), ___, ___, UP, ___, + ___, ___, ___, ___, LEFT, DOWN, RIGHT, ___ + ), + + # layer 2 + ( + '`', F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, DEL, + ___, ___, ___, ___, ___, ___,HOME,PGUP, ___, ___,SINS,AUDIO_VOL_DOWN,AUDIO_VOL_UP,AUDIO_MUTE, + ___, ___, ___, ___, ___, ___,LEFT,DOWN, UP,RIGHT, ___, ___, ___, + ___, ___, ___, ___, ___, ___,PGDN,END, ___, ___, ___, ___, + ___, ___, ___, ___, ___, ___, ___, ___ + ), + + # layer 3 + ( + BT_TOGGLE,BT1,BT2, BT3,BT4,BT5,BT6,BT7, BT8, BT9, BT0, VAL_RGB, RGB_VAL, ___, + RGB_MOD, ___, ___, ___, ___, ___,___,USB_TOGGLE,___,___,MACRO(4),MACRO(2),MACRO(3), ___, + RGB_TOGGLE,HUE_RGB,RGB_HUE,SAT_RGB,RGB_SAT,___,___,___,___,___,___,___, ___, + ___, ___, ___, ___, ___, ___, ___, ___,___,___, ___, ___, + ___, ___, ___, ___, ___, ___, ___, ___ + ), + + # layer 4 + ( + '`', ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, + ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, + ___, ___, ___, D, ___, ___, ___, ___, ___, ___, ';', ___, ___, + ___, ___, ___, ___, ___, B, ___, ___, ___, ___, ___, ___, + ___, ___, ___, ___, ___, ___, ___, ___ + ), + + # layer 5 + ( + ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, + ___, ___, ___, ___, ___, ___,MS_W_UP,MS_UL,MS_UP,MS_UR, ___, ___, ___, ___, + ___, ___, ___, ___, ___, ___,MS_BTN1,MS_LT,MS_DN,MS_RT,MS_BTN2, ___, ___, + ___, ___, ___, ___, ___, ___,MS_W_DN,MS_DL,MS_DN,MS_DR, ___, ___, + ___, ___, ___, ___, ___, ___, ___, ___ + ), +) + +profiles = { + # For example, BT8 is connected to a Mac + "USB": ( + # layer 0 + ( + ESC, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, '-', '=', BACKSPACE, + TAB, Q, W, E, R, T, Y, U, I, O, P, '[', ']', '|', + CAPS, A, L5S, L2D, F, G, H, J, K, L, SCC, '"', ENTER, + LSHIFT,Z, X, C, V, L3B, N, M, ',', '.', '/', RSHIFT, + LCTRL, LALT, LGUI, SPACE, MENU, RALT, RCTRL, L1 + ), + ), + "BT1": ( + # layer 0 + ( + ESC, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, '-', '=', BACKSPACE, + TAB, Q, W, E, R, T, Y, U, I, O, P, '[', ']', '|', + CAPS, A, L5S, L2D, F, G, H, J, K, L, SCC, '"', ENTER, + LSHIFT,Z, X, C, V, L3B, N, M, ',', '.', '/', RSHIFT, + LCTRL, LALT, LGUI, SPACE, MENU, RALT, RCTRL, L1 + ), + + # layer 1 + ( + '`', F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, DEL, + ___, ___, UP, ___, ___, ___, ___, ___, ___, ___,SUSPEND,___,___,___, + ___,LEFT,DOWN,RIGHT,___, ___, ___, ___, ___, ___, ___, ___, ___, + ___, ___, ___, ___, ___,BOOT, ___,MACRO(1), ___, ___, UP, ___, + ___, ___, ___, ___, LEFT, DOWN, RIGHT, ___ + ), + ) +} + +def macro_handler(dev, n, is_down): + if n == 2: + if is_down: + dev.backlight.set_brightness(0) + + elif n == 3: + if is_down: + dev.backlight.set_brightness(255) + + #elif n == 4: + # if is_down: + # print('keyboard.battery.level #{}'.format(keyboard.battery.level)) + + #if is_down: + # dev.send_text('You pressed macro #{}\n'.format(n)) + #else: + # dev.send_text('You released macro #{}\n'.format(n)) + +def pairs_handler(dev, n): + #dev.send_text('You just triggered pair keys #{}\n'.format(n)) + if n == 0: + dev.send_text('ls -alh\n') + elif n == 1: + dev.send_text('du -sh ./*\n') + elif n == 2: + dev.send_text('ifconfig') + +# ESC(0) 1(1) 2(2) 3(3) 4(4) 5(5) 6(6) 7(7) 8(8) 9(9) 0(10) -(11) =(12) BACKSPACE(13) +# TAB(27) Q(26) W(25) E(24) R(23) T(22) Y(21) U(20) I(19) O(18) P(17) [(16) ](15) \(14) +# CAPS(28) A(29) S(30) D(31) F(32) G(33) H(34) J(35) K(36) L(37) ;(38) "(39) ENTER(40) +#LSHIFT(52) Z(51) X(50) C(49) V(48) B(47) N(46) M(45) ,(44) .(43) /(42) RSHIFT(41) +# LCTRL(53) LGUI(54) LALT(55) SPACE(56) RALT(57) MENU(58) Fn(59) RCTRL(60) + +# 1. ls +# 2. du +# 3. ip +#pairs = [{37, 30}, {31, 20}, {19, 17}, {35, 36}, {20, 19}] +pairs = []