pynput
pynput copied to clipboard
🔤 Is there any word listener like keyboard package?
How about:
WL = {
":smile:": "😄"
}
...
for key, value in WL.items(): if x == key: keyboard.type(value)
Can u tell more about x?
Should I listen every key stroke until get x and do that comparison?
Sorry, x is just variable. You can name it Smiley or whatever, just remember to define it. In WL = {...} you can list all your word replacements, then let pynput go through them and print the output.
I'm not sure If you or me understand well. I want to create global hook (that works all windows) and when I type :smile: somewhere,
- Delete
:smile:(maybe sending backspace as much as length of string) - Type
😊
But, I'm not sure how can I catch :smile: word (chars[0:6]).
Do you want to make the replacement in real time or do want to scan a text for words? In the first instance, the method I described should be working. Backspacing is done automatically.
Alternatively, try Autohotkey (works in Windows only).
I want to make text replacement in real time like AutoHotkey HotStrings
- Yes, I know AutoHotkey and I'm trying to find python alternative
- Also I know
ahkpackage (that I contributed too)
Thank you so much for your attention
Same here :)
I'm working on sample (with pynput) for it, when I finished, I will share my code here
PS: WL (word list) is an example for a "dictionary". I found it the most convenient way to emulate AutoHotkey's HotStrings.
🎉 I wrote small script that works like HotString (may has some bug)
from pynput.keyboard import Controller, Key, Listener
_text = ""
WORD_MAPPING = {
"bad": "not fine",
"sad": "will be Happy"
}
controller = Controller()
def check_word_mapping():
global _text
for word, value in WORD_MAPPING.items():
if _text == word:
controller.type("\b" * len(_text) + value)
_text = ""
return
def on_press(key):
global _text
if key == Key.backspace:
_text = _text[:-1]
elif key in [Key.enter, Key.space]:
_text = ""
else:
if hasattr(key, "char"):
_text += key.char
check_word_mapping()
else:
_text = key.value.char if key.value.char else ""
with Listener(on_press=on_press) as listener:
listener.join()
That looks nice. Is it what you wanted? I notice that "xcvbad" will not work but only "xcv bad". Is that on purpose?
PS: Maybe add this before "with Listener...": except AttributeError: print('on_release special key {0} released'.format( key)) if key == Key.esc: # Stops listener on PC return False elif key == Key.cmd_r: # Stops listener on MacBook return False
That looks nice. Is it what you wanted? I notice that "xcvbad" will not work but only "xcv bad". Is that on purpose?
No, but I like it. "well presented bug is feature" 😁
Now I only need to find a way to suppress control keys. Apparently it is very difficult, not like AutoHotkey... :(