Seite 1 von 1

Aktion bei Tastenanschlag

Verfasst: Mittwoch 10. Juni 2020, 10:57
von Dawa82
Hallo,

ich bin Anfänger in Python und probiere gerade folgende Funktion auf meinem RaspberryPi zu realisieren:

Bei Tastaturanschlag + : Den Monitor anschalten und den Browser zu öffnen
Bei Tastaturanschlag - : Den Monitor ausschalten und den Browser zu schließen

Wenn ich mich über ssh verbinde und das Script aufrufe funktioniert das auch super.

Wenn ich allerdings start.sh(siehe unten) auf der Oberfläche vom Pi starte funktioniert es gar nicht. Wenn ich über das Terminal vom Pi meine Funktion display.py starte funktioniert nur der erste Tastenanschlag auf "+". Sobald der Focus das Terminal verlässt, funktioniert kein Tastenanschlag mehr. Wie muss ich so etwas starten, damit es im Hintergrund läuft?

Danke schon mal vorab. :)

#start.sh

Code: Alles auswählen

export DISPLAY=:0.0
python /home/pi/script/display.py
#display.py

Code: Alles auswählen

#!/usr/bin/python3

# adapted from https://github.com/recantha/EduKit3-RC-Keyboard/blob/master/rc_keyboard.py

import sys, termios, tty, os, time
import subprocess
import time
from selenium import webdriver

def getch():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)

    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch

button_delay = 0.2

while True:
    char = getch()

    if (char == "q"):
        print("Stop!")
        exit(0)

    if (char == "+"):
        subprocess.call('XAUTHORITY=~pi/.Xauthority DISPLAY=:0 xset dpms force on', shell=True)
        time.sleep (1)
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_experimental_option("useAutomationExtension", False)
        chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
        chrome_options.add_argument("--start-fullscreen")
        chrome_options.add_argument("disable-infobars")
        chrome_options.add_argument("--disable-notifications")
        chrome_options.add_argument("--profile-directory=Default")
        chrome_options.add_argument('--user-data-dir=/home/pi/.config/chromium')
        chrome = webdriver.Chrome(chrome_options=chrome_options)

        chrome.get("http://192.168.8.150")

        time.sleep(button_delay)

    elif (char == "-"):
        chrome.quit()
        time.sleep(1)
        subprocess.call('XAUTHORITY=~pi/.Xauthority DISPLAY=:0 xset dpms force off', shell=True)
        time.sleep(button_delay)

Re: Aktion bei Tastenanschlag

Verfasst: Samstag 13. Juni 2020, 10:59
von xXSkyWalkerXx1
Sobald der Focus das Terminal verlässt, funktioniert kein Tastenanschlag mehr. Wie muss ich so etwas starten, damit es im Hintergrund läuft?
Ich benutze dafür das 'keyboard' Modul, welches bei mir auch im Hintergrund funktioniert.

Code: Alles auswählen

keyboard.add_hotkey( <keyboard_btn> , <function> ) # Buttons in Strings