-
Notifications
You must be signed in to change notification settings - Fork 0
/
layout_switcher.py
executable file
·126 lines (73 loc) · 2.26 KB
/
layout_switcher.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
126
#!/usr/bin/env python3
from pynput import keyboard
import sys
import os
# Current pressed keys
key_pressed = set()
# Keys for toggle layout keyboard switch
toggle_switch = { keyboard.Key.ctrl, keyboard.Key.shift }
# Toggle layout keyboard switch commands
toggle_ru = "/usr/bin/setxkbmap -layout ru,us -option ''"
toggle_us = "/usr/bin/setxkbmap -layout us -option ''"
toggle_next = toggle_ru
# Clear keyboard layout
os.system("/usr/bin/setxkbmap -layout us -option ''")
def any_equal(s1, s2):
for k in s1:
if k in s2:
return True
return False
def in_equal(s1, s2):
for k in s1:
if not (k in s2):
return False
return True
def is_set_equal(s1, s2):
if not in_equal(s1, s2):
return False
if not in_equal(s2, s1):
return False
return True
def toggle():
global toggle_next
if is_set_equal(key_pressed, toggle_switch):
print (toggle_next)
os.system(toggle_next)
if (toggle_next == toggle_ru): toggle_next = toggle_us
else: toggle_next = toggle_ru
pass
def on_press(key):
valid_key = None
if isinstance(key, keyboard.Key):
if key == keyboard.Key.shift_l or key == keyboard.Key.shift_r or key == keyboard.Key.shift:
valid_key = keyboard.Key.shift
elif key == keyboard.Key.ctrl_l or key == keyboard.Key.ctrl_r or key == keyboard.Key.ctrl:
valid_key = keyboard.Key.ctrl
if valid_key == None:
valid_key = key
if valid_key != None:
if not(valid_key in key_pressed):
key_pressed.add(valid_key)
#print(key_pressed)
if not any_equal(key_pressed, toggle_switch):
key_pressed.clear()
pass
def on_release(key):
toggle()
valid_key = None
if isinstance(key, keyboard.Key):
if key == keyboard.Key.shift_l or key == keyboard.Key.shift_r or key == keyboard.Key.shift:
valid_key = keyboard.Key.shift
elif key == keyboard.Key.ctrl_l or key == keyboard.Key.ctrl_r or key == keyboard.Key.ctrl:
valid_key = keyboard.Key.ctrl
elif isinstance(key, keyboard._xorg.KeyCode):
if key.vk == 65032:
valid_key = keyboard.Key.shift
if valid_key == None:
valid_key = key
if valid_key in key_pressed:
key_pressed.remove(valid_key)
#print(key_pressed)
pass
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()