Python Forum
API or UDP to home automation system - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: API or UDP to home automation system (/thread-34196.html)



API or UDP to home automation system - MandMtje - Jul-06-2021

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.


RE: API or UDP to home automation system - Larz60+ - Jul-06-2021

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


RE: API or UDP to home automation system - MandMtje - Jul-07-2021

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



RE: API or UDP to home automation system - Larz60+ - Jul-07-2021

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


RE: API or UDP to home automation system - MandMtje - Jul-08-2021

Thank you! I think it is very useful!