Use Kanata to control the mouse cursor with your keyboard


December 30, 2024

Last updated: December 30, 2024

Move your mouse cursor using the keyboard with Kanata.

I recently came across a tool called Kanata which is a cross-platform software package that allows you to program your keyboard in all sorts of ways.


I discovered Kanata after using a similar tool called KMonad, which I used to create an extra keyboard layer to use the i,j,k,l keys as arrow keys.

While KMonad was great and did what it had to do, I found Kanata to be more modern and flexible. One advantage of Kanata is that it allows you to control the mouse cursor with your keyboard.


In this post I will show you some example keyboard configurations that you can use to control the mouse cursor. This includes moving the cursor in all directions at multiple speeds, scroll up and down, and do left right and middle mouse clicks.


In the end I will show you a keyboard configuration that combines multiple layers, one for controlling the arrow keys and another for controlling the mouse cursor, all using the i,j,k and l keys.

Let's take a look at the following config. It allows you to use the CapsLock key to enable the mouse layer.

(defsrc
esc f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12
grv 1 2 3 4 5 6 7 8 9 0 - = bspc
tab q w e r t y u i o p [ ] \
caps a s d f g h j k l ; ' ret
lsft z x c v b n m , . / rsft up
lctl lmet lalt spc ralt rmet rctl left down rght
)
(deflayer qwerty
esc f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12
grv 1 2 3 4 5 6 7 8 9 0 - = bspc
tab q w e r t y u i o p [ ] \
@mse a s d f g h j k l ; ' ret
lsft z x c v b n m , . / rsft up
lctl lmet lalt spc ralt rmet rctl left down rght
)
(defalias
mse (layer-while-held mouse)
)
(defalias
ms↑ (movemouse-up 4 4)
ms← (movemouse-left 4 4)
ms↓ (movemouse-down 4 4)
ms→ (movemouse-right 4 4)
)
(deflayer mouse
_ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ @ms↑ _ _ _ _ _
_ _ _ _ _ _ _ @ms← @ms↓ @ms→ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
)

In the above configuration, the @mse alias is used to enable the mouse layer when the CapsLock key is held down. The mouse layer is defined in the mouse layer, which allows you to move the cursor in all directions using the i,j,k and l keys.

To add mouse click functionality, simply add the existing mrgt mmid mlft aliases to the mouse layer. I like to use f key for left click, the d key for middle click, and the s key for right click.

(deflayer mouse
_ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ @ms↑ _ _ _ _ _
_ _ mrgt mmid mlft _ _ @ms← @ms↓ @ms→ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
)

Adding scroll functionality requires some extra aliases:

(defalias
mwu (mwheel-up 50 120)
mwd (mwheel-down 50 120)
mwl (mwheel-left 50 120)
mwr (mwheel-right 50 120)
ms↑ (movemouse-up 4 4)
ms← (movemouse-left 4 4)
ms↓ (movemouse-down 4 4)
ms→ (movemouse-right 4 4)
)
(deflayer mouse
_ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ @mwu @mwl @ms↑ @mwr _ _ _ _
_ _ mrgt mmid mlft _ @mwd @ms← @ms↓ @ms→ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
)

With the above configuration you can scroll up and down using the y and h keys, and scroll left and right using the u and o keys.

While the above configuration is great, I personally wanted more control over the cursor to click on smaller elements. I could simply lower the speed to have more control, but that also means that it takes much more time to move the cursor across the screen.


Note: The mouse speed is set in pixels. The ms↑ (movemouse-up 4 4) command in the above config means that the cursor will move every 4 milliseconds (first 4), and will move 4 pixels each time (second 4). The cursor movement must be set to whole pixels, the minimum value is 1.


To solve this problem, I added two keys (w and e) to the mouse layer, which when held down, lower or increase the speed of the cursor.

(defalias
fst (movemouse-speed 200)
slw (movemouse-speed 50)
vsl (movemouse-speed 25)
mwu (mwheel-up 50 120)
mwd (mwheel-down 50 120)
mwl (mwheel-left 50 120)
mwr (mwheel-right 50 120)
ms↑ (movemouse-up 4 4)
ms← (movemouse-left 4 4)
ms↓ (movemouse-down 4 4)
ms→ (movemouse-right 4 4)
)
(deflayer mouse
_ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ @vsl @vsl _ _ @mwu @mwl @ms↑ @mwr _ _ _ _
_ _ mrgt mmid mlft _ @mwd @ms← @ms↓ @ms→ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
)

I really enjoy moving the cursor accross the screen quickly, and then slow it down when it gets close to where I want to click.

One downside to this approach is that not all keyboards support holding down many keys at the same time. For example, suppose that you would like to move the cursor at the slowest speed, and move it diagonally. The total key combination would be something like Caps + w + i + l which apparently is too much for some keyboards to handle. This means that on some keyboards, while using the slow speed, you might not be able to move the cursor diagonally.

I have not yet found a solution to this problem. Please comment if you have any ideas.

It is possible to extend the above config to use the CapsLock key as an escape key and to enable the mouse layer when held down. Whenever it is held down for more than 200ms, the mouse layer is enabled. This is plenty of time to just tap the CapsLock key to use it as an escape key.

I personally love using the CapsLock key as the escape key, as it is much easier to reach than the default escape key.

Here is the complete configuration:

(defsrc
esc f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12
grv 1 2 3 4 5 6 7 8 9 0 - = bspc
tab q w e r t y u i o p [ ] \
caps a s d f g h j k l ; ' ret
lsft z x c v b n m , . / rsft up
lctl lmet lalt spc ralt rmet rctl left down rght
)
(deflayer qwerty
esc f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12
grv 1 2 3 4 5 6 7 8 9 0 - = bspc
tab q w e r t y u i o p [ ] \
@moe a s d f g h j k l ; ' ret
lsft z x c v b n m , . / rsft up
lctl lmet lalt spc ralt rmet rctl left down rght
)
(defalias
mse (layer-while-held mouse)
)
(defalias
;; mouse or escape
moe (tap-hold 200 200 esc @mse)
)
(defalias
fst (movemouse-speed 200)
slw (movemouse-speed 50)
vsl (movemouse-speed 25)
mwu (mwheel-up 50 120)
mwd (mwheel-down 50 120)
mwl (mwheel-left 50 120)
mwr (mwheel-right 50 120)
ms↑ (movemouse-up 4 4)
ms← (movemouse-left 4 4)
ms↓ (movemouse-down 4 4)
ms→ (movemouse-right 4 4)
ma↑ (movemouse-accel-up 1 1000 1 5)
ma← (movemouse-accel-left 1 1000 1 5)
ma↓ (movemouse-accel-down 1 1000 1 5)
ma→ (movemouse-accel-right 1 1000 1 5)
)
(deflayer mouse
_ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ @vsl @vsl _ _ @mwu @mwl @ms↑ @mwr _ _ _ _
_ _ mrgt mmid mlft _ @mwd @ms← @ms↓ @ms→ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
)

There you have it, a way to control the mouse cursor, arrow keys, and backspace and delete, with keys that are always close to your fingers.


I hope you found this post useful and that you can use it to improve your workflow. To me it has been a game changer, and I cannot imagine going back to a default keyboard layout.