Search on blog:

Python: How to use pynput to get phrase of few chars

Some modules have function to catch single char and assign function but they don't have function to catch few chars which create phrase.

This is example uses pynput Listener to catch all pressed chars and keep them in string - and this helps to check if few last chars match some phrase. But it doesn't check if phrase is not part of longer phrase.

from pynput import keyboard

# keep all chars in global variable
all_keys = ""

def on_press(key):
    global all_keys

    try:
        all_keys += key.char

        if all_keys.endswith("open me"):
            print("I open Sesame")

    except AttributeError:
        #print('special key {0} pressed'.format(key))

        # space doesn't have `key.char`
        if key == keyboard.Key.space:
            all_keys += ' '

def on_release(key):
    #print('{0} released'.format(key))
    if key == keyboard.Key.esc:
        # Stop listener
        return False

# Collect events until released
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
    # ... other code ...
    listener.join()

On Linux there is program in Python AutoKey which catch text (phrase) and replace it with other text (it is so called "text expansion").

On Windows there is similar program but not in Python AutoHotKey and it is even more powerful then AutoKey because it can use combination of keys with mouse buttons.

Both can be useful to fast putting full code in place of short text (so called "snippets").


Notes:

pynput: Monitoring the keyboard

Wikipedia: AutoHotKey

GitHub: AutoHotKey

Python: Jak użyć pynput do sprawdzenie czy wciśnięto frazę z kilku znaków

Niektóre moduły mają funkcję do przechwycenia pojedyńczego znaku i wywołania funkcji ale nie mają one funkcji do sprawdzenia czy wciśnieto ciąg znaków tworzących pewną frazę.

Oto przykład użycia pynput Listener do przechwycenia wszytkich wciśniętych znaków i trzymania ich w stringu - co pozwala sprawdzić czy kilka ostatnich znaków nie tworzą szukaną …

Python: Jak w pynput uruchamiać funkcję tylko raz pomimo wielokrotnego klikania myszą gdy funcja już jest uruchomiona?

Jeśli będzie używał Listener do uruchamia w głównym wątku długo trwającego kodu wtedy będzie on blokował listener i nie będzie on mógł wykonywac innego kodu. Listener nie będzie mógł także odbierać od systemu zdarzeń i będą one czekały w kolejne i listener odbierze je później i zdarzenia te mogą uruchomić …

How to run pynput Listener simultaneously with other Python module

pynput documentation shows examples how to monitoring the keyboard and how to monitoring the mouse

For keyboard it looks similar for this

from pynput import keyboard

# --- functions ---

def on_press(key):
    # some code

def on_release(key):
    # some code

# --- main ---

with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()

For …

Tkinter: how to use pynput keyboard listener together with tkinter GUI

In documentation you can see example for pynput Monitoring The keyboard

with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()

which blocks code but you can put code between lines

with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:

    # ... your code ...

    listener.join()

or you can run it …

Tkinter: Jak użyć pynput keyboard listener razem z GUI tkinter

W dokumentacji można zobaczyć przykład dla Monitoring the keyboard

with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()

który blokuje kod więc nie może pracować razem z tkinter. Ale kod można wstawić między linie

with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:

    # ... your code ...

    listener.join()

lub …

« Page: 1 / 1 »