Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add custom keymap to other file #30

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 109 additions & 75 deletions code.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

from keyboard import *

import my_keymap

keyboard = Keyboard()

Expand All @@ -17,87 +17,115 @@
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
(
'`', 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:
Expand All @@ -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()
24 changes: 19 additions & 5 deletions keyboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
33 changes: 32 additions & 1 deletion keyboard/model/m60.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
Loading