Reputation: 315
The project that I am working on is a typing tool for local languages using Unicode characters.
I'm trying to develop a program that, whenever a word is typed, it is converted to the relevant Unicode word. (It identifies a word after pressing 'space.')
I found out that if Ctrl+shift+u is pressed and released, then the Unicode value is typed, and then a space, it is converted to the relevant Unicode character.
e.g.
1) Ctrl+shift+u
2) 0d96
3) space
->> ඖ
So, I developed this function with pyautogui which helps to press keys whenever needed.
if needed, the 'word' below passes the value like u0dbbu0ddd (not \u0dbb\u0ddd)
import pyautogui
def type_unicode(word,lenth):
#word - converted unicode values
#length - length of the user typed word
#to erase user typed word
for x in range(lenth+1):
pyautogui.press('backspace')
for x in range(0,len(word),5):
# to press ctrl+shift+u
pyautogui.hotkey('ctrl', 'shift', 'u')
#Unicode letters for each word
pyautogui.typewrite(word[x+1:x+5])
pyautogui.press('space')
The problem I am facing here is: when the user inputs his first word it converts to Unicode nicely, But the already converted word is instantly taken as another input, and the program tries to convert it Unicode again. This happens endlessly.
I tried with a flag but I could not resolve it.
Upvotes: 0
Views: 130
Reputation: 1688
You are calling pyautogui.press('space') in your program body, so even though the user is not typing the space, the program is seeing it as a space.
I think you are on the right track with using flags. However, since we don't have the code that handles a space, I can only take a stab at what can work.
I'm going to assume your space-handling function is called space_handler.
def space_handler(a, b, c):
Let's add a global variable to space_handler.
in_user_input = True
def space_handler(a, b, c)
if not in_user_input:
return False
(....regular space handling)
Also, the places to change in_user_input are
in_user_input = False
for x in range(0,len(word),5):
# to press ctrl+shift+u
pyautogui.hotkey('ctrl', 'shift', 'u')
#Unicode letters for each word
pyautogui.typewrite(word[x+1:x+5])
pyautogui.press('space')
in_user_input = True
It seems like you'd just need the in_user_input around pyautogui.press('space'), but if someone puts in unicode 0020 (a space,) that is potentially a mean edge case. There may be other edge cases. But I think this should mostly work.
Hope this helps, or better, you figured things out already.
Upvotes: 1