-
Notifications
You must be signed in to change notification settings - Fork 1
/
APLKeyboard.py
125 lines (103 loc) · 3.25 KB
/
APLKeyboard.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import ui
import keyboard
layout1 = '`1234567890qwertyuiopasdfghjklzxcvbnm,.'
layout2 = '⋄¨¯<≤=≥>≠∨∧ ⍵∊⍴~↑↓⍳○*⍺⌈⌊_∇∆∘\'⎕⊂⊃∩∪⊥⊤|⍪⍠'
layout3 = '⍬!@#$%^&*() /\\⌿⍀←→{}⍞×÷≡≢⌹\'"[]-+⍎⍕⊣⊢;:?'
layout1s = '~1234567890QWERTYUIOPASDFGHJKLZXCVBNM!?'
layout2s = '⌺⌶⍫⍒⍋⌽⍉⊖⍟⍱⍲ ⍹⍷⍴⍨←→⍸⍥⍣⍶⌈⌊⍛⍢⍙⍤⌸⌷⊆⊇∩∪⊣⊢∥⍝⍠'
layoutNames = ['ABC','⍺⍴⍳','⍬+≡']
keybg = '#8c7052'
brdbg = '#232323'
tint = '#ffffff'
shftkey = ['⇧','⇪']
class APLKeyboardView( ui.View ):
def __init__(self,*a,**kw):
super().__init__(*a,**kw)
self.background_color = brdbg
self.tint_color = tint
self.shift = False
self.shiftLock = False
self.layouts = [[layout1,layout2,layout3],
[layout1s,layout2s,layout3]]
self.layout = 0
self.layoutTrans = [[1,0,1],
[2,2,0]]
def did_load(self):
self.setKeyLayout()
for i in range(len(layout1)):
self[f'b{i}'].action = self.out_action
self[f'b{i}'].background_color = keybg
#self['space'].action = self.out_action
for side in 'lr':
self[f'shft{side}'].action = self.shft_action
self[f'alt{side}'].action = self.alt_action
self[f'shft{side}'].background_color = keybg
self[f'alt{side}'].background_color = keybg
self['go'].action = self.go_action
self['go'].background_color = keybg
#self['bksp'].action = self.bksp_action
#self['down'].action = self.down_action
def setKeyLayout(self):
shft = 1 if (self.shiftLock or self.shift) else 0
layout = self.layouts[shft][self.layout]
for i in range(len(layout1)):
self[f'b{i}'].title = layout[i]
self.setAltLabels()
for side in 'lr':
self[f'shft{side}'].title = shftkey[self.shiftLock]
def out_action(self,sender):
text = sender.title
self.shift = False
self.setKeyLayout()
if keyboard.is_keyboard():
keyboard.play_input_click()
keyboard.insert_text(text)
else:
print('Keyboard input:', text)
def bksp_action(self,sender):
keyboard.backspace(times=1)
def shft_action(self,sender):
if self.shift:
if not self.shiftLock:
self.shiftLock = True
else:
self.shiftLock = self.shift = False
elif self.shiftLock:
self.shiftLock = False
else:
self.shift = True
self.setKeyLayout()
def down_action(self,sender):
pass
def go_action(self,sender):
text = '\n'
if keyboard.is_keyboard():
keyboard.play_input_click()
keyboard.insert_text(text)
else:
print('Keyboard input:', text)
def alt_action(self,sender):
side = 0 if sender.name[-1] == 'l' else 1
trans = self.layoutTrans
self.layout = trans[side][self.layout]
# reset shift state when changing layouts
self.shift = False
self.shiftLock = False
self.setKeyLayout()
def setAltLabels(self):
nxtl,nxtr = list(zip(*self.layoutTrans))[self.layout]
self['altl'].title = layoutNames[nxtl]
self['altr'].title = layoutNames[nxtr]
def main():
#v = CharsView(frame = (0, 0, 320, 40))
#if keyboard.is_keyboard():
# keyboard.set_view(v, 'current')
v = ui.load_view('APLKeyboard.pyui')
if keyboard.is_keyboard():
keyboard.set_view(v, 'expanded')
else:
# For debugging in the main app:
v.name = 'APLKeyboardView'
v.present('sheet')
if __name__ == '__main__':
main()