Merge pull request #1 from Hamedrz004/feature/interactive-shortcut

Feature/interactive shortcut
This commit is contained in:
Hamed
2025-08-10 11:03:44 +03:30
committed by GitHub
3 changed files with 77 additions and 3 deletions

20
config&run.bat Normal file
View File

@@ -0,0 +1,20 @@
@echo off
REM run.bat
REM Activates the virtual environment and runs your main Python script.
REM Activate the virtual environment
call venv\Scripts\activate.bat
REM run the config script
python src\config.py
REM Check if the activation was successful
if errorlevel 1 (
echo Failed to activate the virtual environment.
pause
exit /b 1
)
REM Run your main Python script (replace main.py with your script name if different)
start "OopsWrongLang" /B pythonw src\main.pyw
echo application is running in the background. you can close this window.
pause

52
src/config.py Normal file
View File

@@ -0,0 +1,52 @@
import os
import keyboard
import time
CONFIG_FILE = "shortcut.txt"
def get_shortcut():
"""
Retrieves the shortcut from the config file.
If the file doesn't exist, it prompts the user to create one by pressing the desired keys.
"""
if not os.path.exists(CONFIG_FILE):
print("It seems this is the first time you are running the application.")
while True:
print("Please press the key combination you want to use as a shortcut and then press ESC.")
shortcut = keyboard.read_hotkey(suppress=False)
print(f"You pressed: {shortcut}")
confirm = input("Is this correct? (y/n): ").lower()
if confirm == 'y':
with open(CONFIG_FILE, "w") as f:
f.write(shortcut)
return shortcut
else:
print("Let's try again.")
else:
with open(CONFIG_FILE, "r") as f:
return f.read().strip()
if __name__ == "__main__":
shortcut = get_shortcut()
print(f"Your shortcut is set to: {shortcut}")
print("You can now use this shortcut in the application.")
print('do you want to change the shortcut? (y/n): ', end='')
if input().lower() == 'y':
while True:
time.sleep(0.2)
print("Please press the key combination you want to use as a shortcut and then press ESC.")
new_shortcut = keyboard.read_hotkey(suppress=False)
print(f"You pressed: {new_shortcut}")
time.sleep(0.5)
confirm = input("Is this correct? (y/n): ").lower()
if confirm.lower() == 'y':
with open(CONFIG_FILE, "w") as f:
f.write(new_shortcut)
print("Shortcut updated successfully.")
break
else:
print("Let's try again.")
else:
print("No changes made to the shortcut.")
print("Exiting without changes.")
input("Press Enter to exit.")

View File

@@ -8,6 +8,7 @@ import threading
import pystray import pystray
from PIL import Image from PIL import Image
import re import re
from config import get_shortcut
def convert_en_characters(input_str): def convert_en_characters(input_str):
""" """
@@ -136,9 +137,10 @@ def main():
tray_thread = threading.Thread(target=setup_tray, daemon=True) tray_thread = threading.Thread(target=setup_tray, daemon=True)
tray_thread.start() tray_thread.start()
# Register the keyboard shortcut (Ctrl + Space) # Register the keyboard shortcut
keyboard.add_hotkey('ctrl+space', on_shortcut) shortcut = get_shortcut()
print("Press Ctrl + Space to convert text to Persian.") keyboard.add_hotkey(shortcut, on_shortcut)
print(f"Press {shortcut} to convert text to Persian.")
# Keep the script running # Keep the script running
keyboard.wait() keyboard.wait()