pynput icon indicating copy to clipboard operation
pynput copied to clipboard

🔤 Is there any word listener like keyboard package?

Open yemreak opened this issue 5 years ago • 14 comments

  • How can I make word extender with pynput? ( :smile: -> 😄 )

For more detail check that repo

yemreak avatar Jul 18 '20 17:07 yemreak

How about:

WL = { ":smile:": "😄" }

...

for key, value in WL.items(): if x == key: keyboard.type(value)

SpecialCharacter avatar Jul 22 '20 18:07 SpecialCharacter

Can u tell more about x?

Should I listen every key stroke until get x and do that comparison?

yemreak avatar Jul 22 '20 19:07 yemreak

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.

SpecialCharacter avatar Jul 22 '20 19:07 SpecialCharacter

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]).

yemreak avatar Jul 22 '20 20:07 yemreak

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).

SpecialCharacter avatar Jul 22 '20 21:07 SpecialCharacter

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 ahk package (that I contributed too)

Thank you so much for your attention

yemreak avatar Jul 22 '20 21:07 yemreak

Same here :)

SpecialCharacter avatar Jul 22 '20 21:07 SpecialCharacter

I'm working on sample (with pynput) for it, when I finished, I will share my code here

yemreak avatar Jul 22 '20 21:07 yemreak

PS: WL (word list) is an example for a "dictionary". I found it the most convenient way to emulate AutoHotkey's HotStrings.

SpecialCharacter avatar Jul 23 '20 00:07 SpecialCharacter

🎉 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()

yemreak avatar Jul 23 '20 09:07 yemreak

That looks nice. Is it what you wanted? I notice that "xcvbad" will not work but only "xcv bad". Is that on purpose?

SpecialCharacter avatar Jul 23 '20 16:07 SpecialCharacter

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

SpecialCharacter avatar Jul 23 '20 16:07 SpecialCharacter

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" 😁

yemreak avatar Jul 23 '20 21:07 yemreak

Now I only need to find a way to suppress control keys. Apparently it is very difficult, not like AutoHotkey... :(

SpecialCharacter avatar Jul 28 '20 17:07 SpecialCharacter