-
Notifications
You must be signed in to change notification settings - Fork 3
/
virtual_keybird.py
88 lines (73 loc) · 2.82 KB
/
virtual_keybird.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from talon import actions, cron, Context, Module, ctrl
from typing import Callable, Tuple, TypedDict
from dataclasses import dataclass, asdict
from talon.screen import Screen, main_screen
@dataclass
class GridKeys:
topleft: Callable[[float], None]
topmiddle: Callable[[float], None]
topright: Callable[[float], None]
centerleft: Callable[[float], None]
centermiddle: Callable[[float], None]
centerright: Callable[[float], None]
bottomleft: Callable[[float], None]
bottommiddle: Callable[[float], None]
bottomright: Callable[[float], None]
def noop_key(ts: float):
pass
def print_key(key: str):
return lambda ts: print("Pressed key " + key)
def action_key(action):
return lambda ts: action()
def keypress_key(key):
return lambda ts: actions.key(key)
class VirtualKeybird:
screen: Screen
def __init__(self):
keyboard = GridKeys(
noop_key, noop_key, noop_key,
noop_key, noop_key, noop_key,
noop_key, noop_key, noop_key
)
self.screen = main_screen()
self.keyboard = asdict(keyboard)
def press(self, ts: float):
"""Press one of the defined keys """
key_cb = self.find_key(ctrl.mouse_pos())
key_cb(ts)
def find_key(self, coord: Tuple[int, int]):
"""Find the key to press based on the given screen position"""
width = self.screen.width
height = self.screen.height
segment_width = width / 3
segment_height = height / 3
x = "left"
y = "top"
if coord[0] > segment_width:
x = "middle" if coord[0] < segment_width * 2 else "right"
if coord[1] > segment_height:
y = "center" if coord[1] < segment_height * 2 else "bottom"
return self.keyboard[y+x]
def set_keyboard(self, kb: GridKeys):
"""Set the current keyboard"""
self.keyboard = asdict(kb)
vkb = VirtualKeybird()
debug_keyboard = GridKeys(
print_key("topleft"), print_key("topmiddle"), print_key("topright"),
print_key("centerleft"), print_key("centermiddle"), print_key("centerright"),
print_key("bottomleft"), print_key("bottommiddle"), print_key("bottomright")
)
navigation_keyboard = GridKeys(
action_key(actions.app.tab_previous), keypress_key("alt-tab"), action_key(actions.app.tab_next),
action_key(actions.app.tab_previous), keypress_key("alt-tab"), action_key(actions.app.tab_next),
action_key(actions.app.tab_previous), keypress_key("alt-tab"), action_key(actions.app.tab_next)
)
vkb.set_keyboard(navigation_keyboard)
ctx = Context()
mod = Module()
@mod.action_class
class Actions:
def press_virtual_keybird_key(ts: float):
"""Activate the actoin related to the virtual keyboard key mapped on the main screen"""
global vkb
vkb.press(ts)