cazz
Posts: 198
Joined: Wed Nov 14, 2012 9:01 pm
Location: Sweden

Control a kiosk mode screen with bash and python

Thu Oct 04, 2018 9:39 pm

Hi
I have a raspberry running on a screen and Chromium in kiosk mode.
It work greate and now I have even add 3 other tab of webpage that I like to Control with a tiny keyboard that have 6 button on.
When I press "1" it show a in the terminal, "2" show b ……..

I have think about use javascript and use DIV to change page but it feel this is more faster and it works :)

I also guess I can use only python because right now I use bash to read the input

(menu.sh)

Code: Select all

  #!/bin/bash

while true; do
read -rsn1 input
if [ "$input" = "a" ]; then
    python /home/pi/control/tab1.py
    echo "Tab 1"
fi
if [ "$input" = "b" ]; then
    python /home/pi/control/tab2.py
fi
if [ "$input" = "c" ]; then
    python /home/pi/control/tab3.py
fi
if [ "$input" = "d" ]; then
    python /home/pi/control/tab4.py
fi
if [ "$input" = "e" ]; then
    python /home/pi/control/tab5.py
fi
done

and I use python to simulate the tab change
(tab1.py)

Code: Select all

from pynput.keyboard import Key, Controller
keyboard = Controller()
keyboard.press(Key.ctrl)
keyboard.press('1')
keyboard.release('1')
keyboard.release(Key.ctrl)

I know the bash code works because "Tab 1" show when I press button 1
I also know the python works when I run from a remote computer with python the script

But when I put the bash script so it autoload when the raspberry startup nothing happend when I press the button.


Have no idea why it does not work.
/Cazz

User avatar
Paeryn
Posts: 2143
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Control a kiosk mode screen with bash and python

Thu Oct 04, 2018 11:27 pm

When starting the bash script at startup (pretty much whichever way you did it) the program won't have any input stream from which to read any keypresses (programs are run in the background so there is nowhere for them to get any input from). When X11 is running then keypresses generally only get sent to the window that has focus, which I assume will be your browser.

At a guess the Python code works when you remotely log in because pynput looks to generate key and mouse events as X11 events and if your browser on the RPi has focus then it receives the events. You'll have a problem with doing this locally the way you are as (I'm assuming your 6-key device appears as a keyboard from what you've said) for your program to get keypresses from the device it needs to be running in a terminal emulator whose window has input focus, which means that any generated key events your Python code emits will also go to your bash script via the terminal's window and not to your browser.

To work you would need your 6-key device to not be used by X11 as a keyboard and for your bash script to read directly from the device. Or have a terminal open that has input focus and use another method to inject key events directly into the browser's window rather than just generating X11 events, though a quick Google search suggests that chromium could possibly reject synthetic key events injected directly into its window.
She who travels light — forgot something.

cazz
Posts: 198
Joined: Wed Nov 14, 2012 9:01 pm
Location: Sweden

Re: Control a kiosk mode screen with bash and python

Sat Oct 06, 2018 10:57 am

Thanks for the replay
I Think I try first to use one HTML page with Javascript that I can use to change page in a DIV to start with.
/Cazz

Return to “Other programming languages”

Who is online

Users browsing this forum: No registered users and 1 guest