Python Forum
API or UDP to home automation system
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
API or UDP to home automation system
#1
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.
Reply
#2
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
Reply
#3
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()
Larz60+ write Jul-07-2021, 11:06 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

fixed for you this time. Please use bbcode tags on future posts.
Reply
#4
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
Reply
#5
Thank you! I think it is very useful!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020