# Documentation on man keyd or at https://github.com/rvaiya/keyd/blob/master/docs/keyd.scdoc

include layouts/br


[global]
#default_layout = br
# allow this many milliseconds between each press when detecting a chord
chord_timeout = 300
# after this many ms without a key press, deactivate the oneshot modifier
oneshot_timeout = 1000
# if this many ms elapse, do not consider a tap in overload
overload_tap_timeout = 500
# Do not surround release of unused modifier hold with control
disable_modifier_guard = 1


[ids]
# Restrict matching of key events to all keyboards except those listed with a leading -
258a:002a:75e84400
258a:002a:65e37239


[main]
# default/base layer

# make capslock, menu and altgr more useful
capslock     = overload(capslayer, esc)
compose      = overload(composelayer, compose)
rightalt     = overload(altgr, backspace)

# arrows & backspace with pinky base
leftcontrol  = overload(control, left)
rightcontrol = overload(control, right)

# one-shot control
leftalt      = overload(alt, oneshot(control))

# one-shot combined modifiers (C-S & C-S-A)
leftmeta                 = overload(meta, oneshot(cs))
leftcontrol+rightcontrol = oneshot(csa)

# one shot shift
leftshift                = oneshot(shift)
rightshift               = oneshot(shift)

# "\" if tapped, else " || "
102nd = timeout(102nd, 300, macro(space S-102nd S-102nd space))

# Auto-shift some keys when held
` = timeout(`, 200, S-`)
- = timeout(-, 200, S--)
= = timeout(=, 200, S-=)
] = timeout(], 200, S-])
' = timeout(', 200, S-')
[ = timeout([, 200, macro(S-[ space))
backslash = timeout(backslash, 200, S-backslash)
, = timeout(comma, 300, S-comma)
. = timeout(., 300, S-.)
/ = timeout(/, 300, S-/)
ro = timeout(ro, 300, S-ro)

# auto-shift for alphanumeric keys
1 = overloadi(1, timeout(1, 200, S-1), 150)
2 = overloadi(2, timeout(2, 200, S-2), 150)
3 = overloadi(3, timeout(3, 200, S-3), 150)
4 = overloadi(4, timeout(4, 200, S-4), 150)
5 = overloadi(5, timeout(5, 200, S-5), 150)
6 = overloadi(6, timeout(6, 200, S-6), 150)
7 = overloadi(7, timeout(7, 200, S-7), 150)
8 = overloadi(8, timeout(8, 200, S-8), 150)
9 = overloadi(9, timeout(9, 200, S-9), 150)
0 = overloadi(0, timeout(0, 200, S-0), 150)

q = overloadi(q, timeout(q, 200, S-q), 150)
w = overloadi(w, timeout(w, 200, S-w), 150)
e = overloadi(e, timeout(e, 200, S-e), 150)
r = overloadi(r, timeout(r, 200, S-r), 150)
t = overloadi(t, timeout(t, 200, S-t), 150)
y = overloadi(y, timeout(y, 200, S-y), 150)
u = overloadi(u, timeout(u, 200, S-u), 150)
i = overloadi(i, timeout(i, 200, S-i), 150)
o = overloadi(o, timeout(o, 200, S-o), 150)
p = overloadi(p, timeout(p, 200, S-p), 150)

a = overloadi(a, timeout(a, 200, S-a), 150)
s = overloadi(s, timeout(s, 200, S-s), 150)
d = overloadi(d, timeout(d, 200, S-d), 150)
f = overloadi(f, timeout(f, 200, S-f), 150)
g = overloadi(g, timeout(g, 200, S-g), 150)
h = overloadi(h, timeout(h, 200, S-h), 150)
j = overloadi(j, timeout(j, 200, S-j), 150)
k = overloadi(k, timeout(k, 200, S-k), 150)
l = overloadi(l, timeout(l, 200, S-l), 150)

z = overloadi(z, timeout(z, 200, S-z), 150)
x = overloadi(x, timeout(x, 200, S-x), 150)
c = overloadi(c, timeout(c, 200, S-c), 150)
v = overloadi(v, timeout(v, 200, S-v), 150)
b = overloadi(b, timeout(b, 200, S-b), 150)
n = overloadi(n, timeout(n, 200, S-n), 150)
m = overloadi(m, timeout(m, 200, S-m), 150)

[altgr]

#  movement
tab = C-S-tab
d = C-left
g = C-right
f = macro(up 20ms up 20ms up 20ms up 20ms up)
v = macro(down 20ms down 20ms down 20ms down 20ms down)

# mouse movement with warpd
r = A-M-c
h = macro(A-M-g 50ms u 50ms esc 50ms A-M-c)
j = macro(A-M-g 50ms j 50ms esc 50ms A-M-c)
k = macro(A-M-g 50ms i 50ms esc 50ms A-M-c)
l = macro(A-M-g 50ms k 50ms esc 50ms A-M-c)

j = togglem(mouselayer, macro(A-M-g 50ms u 50ms k 50ms j 50ms j 50ms j 50ms j 50ms esc 50ms A-M-c))
i = togglem(mouselayer, macro(A-M-g 50ms u 50ms i 50ms k 50ms k 50ms k 50ms k 50ms esc 50ms A-M-c))
k = togglem(mouselayer, macro(A-M-g 50ms j 50ms i 50ms k 50ms k 50ms k 50ms k 50ms esc 50ms A-M-c))
l = togglem(mouselayer, macro(A-M-g 50ms i 50ms k 50ms j 50ms j 50ms j 50ms j 50ms esc 50ms A-M-c))

j+i = togglem(mouselayer,  macro(A-M-g 50ms u 50ms esc 50ms A-M-c))
j+k = togglem(mouselayer,  macro(A-M-g 50ms j 50ms esc 50ms A-M-c))
l+i = togglem(mouselayer,  macro(A-M-g 50ms i 50ms esc 50ms A-M-c))
l+k = togglem(mouselayer,  macro(A-M-g 50ms k 50ms esc 50ms A-M-c))

# act as alias for M-n and M-S-n
1 = timeout(M-1, 500, M-S-1)
2 = timeout(M-2, 500, M-S-2)
3 = timeout(M-3, 500, M-S-3)
4 = timeout(M-4, 500, M-S-4)
5 = timeout(M-5, 500, M-S-5)
6 = timeout(M-6, 500, M-S-6)
7 = timeout(M-7, 500, M-S-7)
8 = timeout(M-8, 500, M-S-8)
9 = timeout(M-9, 500, M-S-9)
0 = timeout(M-0, 500, M-S-0)


[mouselayer]

# disable layer when warpd exits
n = togglem(mouselayer, n)
esc = togglem(mouselayer, esc)

# Turn-off auto-shift for arrow keys
j = j
k = k
i = i
l = l


[meta]

# behave as in main layer, but increase timeout to prevent accidentally moving windows tto other desktops
1 = timeout(M-1, 500, M-S-1)
2 = timeout(M-2, 500, M-S-2)
3 = timeout(M-3, 500, M-S-3)
4 = timeout(M-4, 500, M-S-4)
5 = timeout(M-5, 500, M-S-5)
6 = timeout(M-6, 500, M-S-6)
7 = timeout(M-7, 500, M-S-7)
8 = timeout(M-8, 500, M-S-8)
9 = timeout(M-9, 500, M-S-9)
0 = timeout(M-0, 500, M-S-0)


[capslayer]

# chords
#f+j = home
#f+l = end
#f+i = pageup
#f+k = pagedown

# emit keys
#   '····1······2······3······4·····5······6······7······8······9·······0·····-····=     # number row
#       C-Aq1 C-Aq2  C-Aq3  C-Aq4  C-Aq5  C-Aq4  C-Aq4  C-Aq4  C-Aq4  C-Aq0  _-_  _==_
#   tab··q······w······e······r·····t······y·······u·····i·····o·······p······`····[     # upper row
# C-tab C-aq  C-bsp  C-del  S-ent   "      '       (     )    _+_    C-S-p    `   <>←
#       C-a   C-S-s   M-d    C-f    -      ←       ↓     ↑     →      _+=_    ~    }
#········a······s······d······f·····g······h·······j·····k·····l·······ç······~····]     # home row
#········z······x······c······v·····b······n·······m·····,·····.·······;······/          # lower row
#       C-a z  C-x   C-S-c  C-S-v   _     bsp     del  _<=_  _>=_     _:_    _?_

# replace rightalt = right with __←
rightalt = macro(space space left)

# keypad
left       = ,
right      = .
up         = 0
delete     = 1
end        = 2
pagedown   = 3
insert     = 4
home       = 5
pageup     = 6
sysrq      = 7
scrolllock = 8
pause      = 9

# number row single key remaps
# split tmux horizontally
` = macro(C-a 50ms S-`)
# go to tmux pane by number
1 = timeout(macro(space != space), 300,           macro(C-a 50ms q 200ms 1))
2 = timeout(S-2, 300,                             macro(C-a 50ms q 200ms 2))
3 = timeout(S-3, 300,                             macro(C-a 50ms q 200ms 3))
4 = timeout(macro(S-4 S-] S-backslash left), 300, macro(C-a 50ms q 200ms 4))
5 = timeout(macro(space %% left), 300,            macro(C-a 50ms q 200ms 5))
6 = timeout(S-5, 300,                             macro(C-a 50ms q 200ms 6))
7 = timeout(macro(space && space), 300,           macro(C-a 50ms q 200ms 7))
8 = timeout(macro(space * space),  300,           macro(C-a 50ms q 200ms 8))
9 = timeout(macro(S-9 S-0 left), 300,             macro(C-a 50ms q 200ms 9))
0 = timeout(macro(S-4 S-9 S-0 left), 300,         macro(C-a 50ms q 200ms 0))
- = timeout(macro(space - space), 300, macro(space -= space))
= = timeout(macro(space = space), 300, macro(space == space))

# upper row single key remaps
tab = macro(C-a 50ms %)
q = macro(C-a 50ms q)
w = C-backspace
e = C-delete
r = S-enter
t = S-`
y = `
u = (
o = macro(space + space)
p = C-S-p
[ = timeout(macro(S-[ space  20ms S-[ space left), 300, macro(S-[ space S-[ space S-[ space S-[ backspace S-] S-backslash enter enter S-[ space S-[ space S-[ space S-[ space backspace up up end left))
] = macro(<> left)

# arrow keys
j = left
k = down
i = up
l = right

# home row single key remaps
a = C-a
s = C-S-s
d = M-d
f = C-f
g = -
h = left
; = macro(space S-= = space)
#' = timeout(macro(' '), 300, macro(S-' S-'))
' = macro(S-' S-')
\ = macro(S-] S-\ left)

# lower row single key remaps
z = macro(C-a 50ms z)
x = C-x
c = C-S-c
v = C-S-v
b = _
n = backspace
m = delete
, = timeout(macro(space < space), 300, macro(space <= space))
. = timeout(macro(space > space), 300, macro(space >= space))
/ = timeout(macro(S-/ space), 300, macro(space S-/ space))
ro = timeout(macro(space ro space), 300, macro(space S-ro space))


[composelayer]

# emit keys                         keyboard keys
# '········1······2······3····4·····5·····6·······7······8·····9·····0····-···=     # upper row
#         M-1    M-2    M-3  M-4   M-5   M-6     M-7    M-8   M-9   M-0
# tab······q······w······e····r·····t·····y·······u······i·····o·····p····`···[     # upper row
# C-S-tab        :wa
#          C-S-a :s/    hom  pgu   end 
#··········a·······s·····d····f·····g·····h······j·······k·····l·····ç····~···]     # home row
#··········z·······x·····c····v·····b·····n······m·······,·····.·····;····/         # lower row
#                            pgd


w = macro(esc 200ms S-/ 50ms wa enter)

a = C-S-a
s = macro(S-/ 50ms s ro ro ro g left left left)
d = home
f = pageup
v = pagedown
g = end


[cs]

# number row
` = C-S-`
1 = C-S-1
2 = C-S-2
3 = C-S-3
4 = C-S-4
5 = C-S-5
6 = C-S-6
7 = C-S-7
8 = C-S-8
9 = C-S-9
0 = C-S-0
- = C-S--
= = C-S-=
backspace = C-S-backspace

# upper row
tab = C-S-tab
q = C-S-q
w = C-S-w
e = C-S-e
r = C-S-r
t = C-S-t
y = C-S-y
u = C-S-u
i = C-S-i
o = C-S-o
p = C-S-p
] = C-S-]

# home row
a = C-S-a
s = C-S-s
d = C-S-d
f = C-S-f
g = C-S-g
h = C-S-h
j = C-S-j
k = C-S-k
l = C-S-l
; = C-S-;
\ = C-S-\
enter = C-S-enter

# lower row
z = C-S-z
x = C-S-x
c = C-S-c
v = C-S-v
b = C-S-b
n = C-S-n
m = C-S-m
, = C-S-,
. = C-S-.
/ = C-S-/
ro = C-S-ro
space = C-S-space


[csa]

# number row
` = C-S-A-`
1 = C-S-A-1
2 = C-S-A-2
3 = C-S-A-3
4 = C-S-A-4
5 = C-S-A-5
6 = C-S-A-6
7 = C-S-A-7
8 = C-S-A-8
9 = C-S-A-9
0 = C-S-A-0
- = C-S-A--
= = C-S-A-=
backspace = C-S-A-backspace

# upper row
tab = C-S-A-tab
q = C-S-A-q
w = C-S-A-w
e = C-S-A-e
r = C-S-A-r
t = C-S-A-t
y = C-S-A-y
u = C-S-A-u
i = C-S-A-i
o = C-S-A-o
p = C-S-A-p
] = C-S-A-]

# home row
a = C-S-A-a
s = C-S-A-s
d = C-S-A-d
f = C-S-A-f
g = C-S-A-g
h = C-S-A-h
j = C-S-A-j
k = C-S-A-k
l = C-S-A-l
; = C-S-A-;
\ = C-S-A-\
enter = C-S-A-enter

# lower row
z = C-S-A-z
x = C-S-A-x
c = C-S-A-c
v = C-S-A-v
b = C-S-A-b
n = C-S-A-n
m = C-S-A-m
, = C-S-A-,
. = C-S-A-.
/ = C-S-A-/
ro = C-S-A-ro
space = C-S-A-space

