Python Forum

Full Version: API or UDP to home automation system
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am a beginner in python programming. For a project I want to have a program that reads 2 keys pressed simultaneously, like preferably alt + or alt -.
I want to be able to read this via UDP or API in our home automation system.
Who can help me on my way?

I've been working on python for a while, but I can't get it to read two keys at the same time.
Also, I don't know where to start with API and UDP.
Quote:I've been working on python for a while, but I can't get it to read two keys at the same time.
show us what you have tried
Here i found the original code i have used:
https://github.com/moses-palmer/pynput/issues/20

but i changed it to this:

from pynput import keyboard

COMBINATION = {keyboard.Key.alt, keyboard.KeyCode.from_char('+')}

# The currently active modifiers
current = set()

def on_press(key):
    if key in COMBINATION:
        current.add(key)
        if all(k in current for k in COMBINATION):
            print('All modifiers active!')

def on_release(key):
    try:
        current.remove(key)
    except KeyError:
        pass

with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()
OK, so you're looking for a keyboard listener.
this one looks promising, though I haven't used it myself: https://pypi.org/project/keyboard-listener/
there are others that should be looked at, see: https://pypi.org/search/?q=listener
Thank you! I think it is very useful!