feat: Implement interactive shortcut selection
This commit enhances your experience for setting a keyboard shortcut. Instead of typing the shortcut as a string, you are now prompted to press the desired key combination directly. The `src/config.py` module has been updated to: - Use `keyboard.read_hotkey()` to capture the key combination you press. - Display the captured shortcut for confirmation. - Save the shortcut only after you confirm it is correct. This provides a more intuitive and less error-prone way for you to configure your preferred shortcut.
This commit is contained in:
2
run.bat
2
run.bat
@@ -6,6 +6,6 @@ REM Activate the virtual environment
|
||||
call venv\Scripts\activate.bat
|
||||
|
||||
REM Run your main Python script (replace main.py with your script name if different)
|
||||
start "OopsWrongLang" /B pythonw src\main.pyw
|
||||
start "OopsWrongLang" /B python src\main.pyw
|
||||
echo application is running in the background. you can close this window.
|
||||
pause
|
||||
26
src/config.py
Normal file
26
src/config.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import os
|
||||
import keyboard
|
||||
|
||||
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()
|
||||
@@ -8,6 +8,7 @@ import threading
|
||||
import pystray
|
||||
from PIL import Image
|
||||
import re
|
||||
from config import get_shortcut
|
||||
|
||||
def convert_en_characters(input_str):
|
||||
"""
|
||||
@@ -136,9 +137,10 @@ def main():
|
||||
tray_thread = threading.Thread(target=setup_tray, daemon=True)
|
||||
tray_thread.start()
|
||||
|
||||
# Register the keyboard shortcut (Ctrl + Space)
|
||||
keyboard.add_hotkey('ctrl+space', on_shortcut)
|
||||
print("Press Ctrl + Space to convert text to Persian.")
|
||||
# Register the keyboard shortcut
|
||||
shortcut = get_shortcut()
|
||||
keyboard.add_hotkey(shortcut, on_shortcut)
|
||||
print(f"Press {shortcut} to convert text to Persian.")
|
||||
|
||||
# Keep the script running
|
||||
keyboard.wait()
|
||||
|
||||
Reference in New Issue
Block a user