forked from matipojo/baafucha
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
93 lines (76 loc) · 2.93 KB
/
main.py
File metadata and controls
93 lines (76 loc) · 2.93 KB
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
import keyboard
import pyperclip
from pynput import keyboard as pynput_keyboard
import time
from PIL import Image
import threading
import os
from core.tray import SystemTrayApp
# Keyboard mapping for English to Hebrew and vice versa
en_to_he = {
'q': '/', 'w': "'", 'e': 'ק', 'r': 'ר', 't': 'א', 'y': 'ט', 'u': 'ו', 'i': 'ן', 'o': 'ם', 'p': 'פ',
'a': 'ש', 's': 'ד', 'd': 'ג', 'f': 'כ', 'g': 'ע', 'h': 'י', 'j': 'ח', 'k': 'ל', 'l': 'ך',
'z': 'ז', 'x': 'ס', 'c': 'ב', 'v': 'ה', 'b': 'נ', 'n': 'מ', 'm': 'צ',
',': 'ת', '.': 'ץ', ';': 'ף'
}
he_to_en = {v: k for k, v in en_to_he.items()}
def is_hebrew(text):
"""Check if the text contains Hebrew characters."""
return any('\u0590' <= c <= '\u05FF' for c in text)
def convert_text(text, mapping):
"""Convert text using the provided mapping."""
return ''.join(mapping.get(char.lower(), char) for char in text)
def auto_convert(select_all=False):
"""Automatically detect and convert text between English and Hebrew."""
# Save original clipboard content
original_clipboard = pyperclip.paste()
if select_all:
keyboard.press_and_release('ctrl+a')
time.sleep(0.1)
# Try to copy selected text
keyboard.press_and_release('ctrl+c')
time.sleep(0.1)
selected_text = pyperclip.paste()
# Check if text was selected
text_was_selected = selected_text != original_clipboard
# If no text was selected and select_all is False, do nothing
if not text_was_selected and not select_all:
pyperclip.copy(original_clipboard)
return
# Choose appropriate mapping based on text language
if is_hebrew(selected_text):
converted_text = convert_text(selected_text, he_to_en)
else:
converted_text = convert_text(selected_text, en_to_he)
# Copy converted text to clipboard and paste
pyperclip.copy(converted_text)
keyboard.press_and_release('ctrl+v')
# Restore original clipboard content
time.sleep(0.1)
pyperclip.copy(original_clipboard)
def on_key_press(key):
"""Handle key press events."""
try:
if key == pynput_keyboard.Key.f8:
# Check if shift is pressed
if keyboard.is_pressed('control'):
auto_convert(select_all=True)
else:
auto_convert(select_all=False)
except AttributeError:
pass
# Create keyboard listener
listener = pynput_keyboard.Listener(on_press=on_key_press)
def stop_listener():
"""Stop the keyboard listener."""
listener.stop()
if __name__ == "__main__":
# Start keyboard listener in a separate thread
listener_thread = threading.Thread(target=listener.run)
listener_thread.start()
print("Baafucha is running.")
print("Press F8 to convert selected text between English and Hebrew.")
print("Press Ctrl+F8 to select all text and convert it.")
# Run system tray icon
tray = SystemTrayApp(stop_listener)
tray.run()