Python code for raspberry pi not "alt+tabbing" to new window with GPIO button

27 Views Asked by At

I'm working on building a 'thing' (idk what to call it) that has a UI for monitoring certain environmental values and triggers events based on the senors' readings. The readings are also send to a Database for a Grafana dashboard. I once tried to embed the Grafana Dashboard into the UI I created (as I've embedded webpages before into other projects). However, my UI absolutely did not like the Grafana webpages at all...

So, I have my UI and the Grafana dashboard open in kiosk mode on boot. I'm able to switch between the programs' UI and the Web-browser using a keyboard and alt-tabbing. I'm trying to make a 'one-button' approach using a single gpio button press to simulate the alt-tab action.

Note for ex_2: using $ sudo showkey -a I was able to find 9 0011 0x09 to pass the number 9 to the 'keyboard' library as scan code for the tab key.

I use sudo to run the py scripts because some libraries weren't able to do anything with the tab key without being root.

Apologies if formatting is garbage. First time asker; long time lurker...

What I have tried to no avail:

ex_1: gives no error. works fine for all other keyboard needs. doesn't work. ran from terminal as $ sudo nohup python myscript.py &.

import pyautogui as pg
import time
from gpiozero import Button

tib = Button(16)

def macro():
    pg.hotkey('alt','tab', interval=0.2)

def butpress():
    while True:
        tib.when_pressed = macro
        time.sleep(.1)

butpress()

ex_2: gives no error. also works fine for all other keyboard needs. doesn't work. ran from terminal as '$ sudo nohup python myscript.py &'

import keyboard
import time
from gpiozero import Button

tib = Button(16)

def macro():
    keyboard.send('alt, 9')

def butpress():
    while True:
        tib.when_pressed = macro
        time.sleep(.1)

butpress()

ex_3: gives no error. also works fine for all other keyboard needs. doesn't work. ran from terminal as '$ sudo nohup python myscript.py &'

from pynput.keyboard import Key, Controller
import time
from gpiozero import Button

keyboard = Controller()
tib = Button(16)

def macro():

    keyboard.press(Key.alt)
    time.sleep(.1)
    keyboard.press(Key.tab)
    time.sleep(.1)
    keyboard.release(Key.tab)
    time.sleep(.1)
    keyboard.release(Key.alt)

def butpress():
    while True:
        tib.when_pressed = macro
        time.sleep(.1)

butpress()

I've also tried other combinations of commands from the different libraries and still no success. I found someone that got what I am trying to do working from a long time ago. However, even copying his script line for line doesn't work for me at all. https://www.youtube.com/watch?v=0Mn9woIGXm4

Also the reason there are so many sleeps in some of the attempts, some may already know, but I found other stack overflow threads talk about something similar from a long time ago and they suggested sleeps between key events for certain libraries, and that worked for that threads' poster (hasn't worked for me).

The reason for the sleep in the butpress(): is because I cannot find deeper documents on the gpiozero library for how when_pressed is working (should probably dive into the library later), and I don't need the while True loop banging through at clock speed.

Any and all advise will be tried because I am out of ideas and pretty beaten down by this project... Thank you in advance.

0

There are 0 best solutions below