User avatar
NPDedyukhin
Posts: 29
Joined: Fri Sep 20, 2019 3:23 am
Location: RU

A script that reads the Enter key, and runs the following script

Tue Oct 08, 2019 3:16 am

Good afternoon! :)

There is a Raspbian Buster, and an empty script that automatically loads when you turn on the device. A USB keyboard is connected to the device (Pi).

It is necessary that after turning on the device, when the Enter key is pressed on the keyboard, the following script is launched.

How to do it? :roll:
Respectfully,
Nikita Dedyukhin

pcmanbob
Posts: 7942
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: A script that reads the Enter key, and runs the following script

Tue Oct 08, 2019 12:14 pm

Seeing as you have posted this in the python subforum , I will assume you are wanting to start a python program on the desktop.

So in your existing program you add

Code: Select all


junk = input ("press Enter to start")

your code will stop at this line and will only continue once enter has been pressed.


to call you python script you need to add it to the gui autostart file

to edit the file

Code: Select all


sudo nano /etc/xdg/lxsession/LXDE-pi/autostart

then at the end of the existing lines you need to add

Code: Select all


@python /home/pi/your_script.py
replacing the directory path and file name with the ones for your script, if you want to use python 3 then change python to python3

Edit.

if you need the terminal to run your python script then the line would look like this

Code: Select all

@lxterminal -e python3 /home/pi/your_script.py
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
NPDedyukhin
Posts: 29
Joined: Fri Sep 20, 2019 3:23 am
Location: RU

Re: A script that reads the Enter key, and runs the following script

Tue Oct 08, 2019 3:47 pm

pcmanbob wrote:
Tue Oct 08, 2019 12:14 pm
Seeing as you have posted this in the python subforum , I will assume you are wanting to start a python program on the desktop.

So in your existing program you add

Code: Select all


junk = input ("press Enter to start")

your code will stop at this line and will only continue once enter has been pressed.


to call you python script you need to add it to the gui autostart file

to edit the file

Code: Select all


sudo nano /etc/xdg/lxsession/LXDE-pi/autostart

then at the end of the existing lines you need to add

Code: Select all


@python /home/pi/your_script.py
replacing the directory path and file name with the ones for your script, if you want to use python 3 then change python to python3
The script was added to startup.

I tried to insert the line according to your advice:

junk = input ("press Enter to start")

The result is the following code:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import lcddriver
import time

lcd = lcddriver.lcd()

lcd.lcd_clear()
lcd.lcd_display_string("Press Enter", 1)
lcd.lcd_display_string("on keyboard", 2)

junk = input ("press Enter to start")

time.sleep(5)

lcd.lcd_backlight("off")


Before that, the code looked like this:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import lcddriver
import time

lcd = lcddriver.lcd()

lcd.lcd_clear()
lcd.lcd_display_string("Press Enter", 1)
lcd.lcd_display_string("on keyboard", 2)

time.sleep(5)

lcd.lcd_backlight("off")
Prior to this, 5 seconds after a message appeared on the display, the display backlight turned off (command: lcd.lcd_backlight ("off"))

Now, after adding a line, the display does not go out after 5 seconds (script execution really stops), but when Enter is pressed on the keyboard, nothing happens (script execution does not continue). I was also confused why there is no explicit indication of the Enter key in this line?
Respectfully,
Nikita Dedyukhin

scotty101
Posts: 3803
Joined: Fri Jun 08, 2012 6:03 pm

Re: A script that reads the Enter key, and runs the following script

Tue Oct 08, 2019 4:11 pm

Problem is that the python script running in the background won't have keyboard focus so pressing the enter key won't do anything.

Would you be open to using a physical button connected to the GPIO pins? That would be much easier.

Alternatively, have a look at this stack overflow question/answers which suggest using pyhook (may be windows specific however)
https://stackoverflow.com/questions/338 ... d/33864704
or for a linux compatible version
http://jeffhoogland.blogspot.com/2014/1 ... xhook.html
Last edited by scotty101 on Tue Oct 08, 2019 4:15 pm, edited 2 times in total.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

pcmanbob
Posts: 7942
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: A script that reads the Enter key, and runs the following script

Tue Oct 08, 2019 4:13 pm

Then I guess we need more information about your pi and its OS and how you are running this program.

So which pi are you using

what OS are you running inc full desktop or lite version

how are you auto starting the program , in detail if not using what I suggested.

what do you see on the monitor/tv screen when the program runs , do you even have a monitor/tv connected.

did you see the edit I posted about adding the extra information to call the terminal if its required to run your python script.

Does it work if you call it from the command line or in a terminal window.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
NPDedyukhin
Posts: 29
Joined: Fri Sep 20, 2019 3:23 am
Location: RU

Re: A script that reads the Enter key, and runs the following script

Tue Oct 08, 2019 4:31 pm

pcmanbob wrote:
Tue Oct 08, 2019 4:13 pm
Then I guess we need more information about your pi and its OS and how you are running this program.

So which pi are you using

what OS are you running inc full desktop or lite version

how are you auto starting the program , in detail if not using what I suggested.

what do you see on the monitor/tv screen when the program runs , do you even have a monitor/tv connected.

did you see the edit I posted about adding the extra information to call the terminal if its required to run your python script.

Does it work if you call it from the command line or in a terminal window.
*operating system: "2019-07-10-raspbian-buster-full"
*type of loading: Boot to CLI
*how to run the script automatically:
sudo nano /etc/rc.local
before the line "exit 0" add the line:
#!/usr/bin/env python
python /home/pi/Desktop/pi_cam/start_pi_cam.py
*I don’t have a monitor / TV, I have an LCD 1602. The message "Press Enter to keyboard" appears on it

I tried using code using "pygame" - it turned out successfully:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import lcddriver
import time

lcd = lcddriver.lcd()

lcd.lcd_clear()
lcd.lcd_display_string("Press Enter", 1)
lcd.lcd_display_string("on keyboard", 2)

import pygame
pygame.init()
screen = pygame.display.set_mode((400,400))

done = False
while not done:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                time.sleep(5)

                lcd.lcd_backlight("off")
Respectfully,
Nikita Dedyukhin

User avatar
NPDedyukhin
Posts: 29
Joined: Fri Sep 20, 2019 3:23 am
Location: RU

Re: A script that reads the Enter key, and runs the following script

Wed Oct 09, 2019 7:20 am

scotty101 wrote:
Tue Oct 08, 2019 4:11 pm
Problem is that the python script running in the background won't have keyboard focus so pressing the enter key won't do anything.

Would you be open to using a physical button connected to the GPIO pins? That would be much easier.

Alternatively, have a look at this stack overflow question/answers which suggest using pyhook (may be windows specific however)
https://stackoverflow.com/questions/338 ... d/33864704
or for a linux compatible version
http://jeffhoogland.blogspot.com/2014/1 ... xhook.html
At first everything worked out (the code above using pygame). But now, the keyboard does not respond to pressing Enter. : |

I need to use a keyboard, not a button. I will need to enter data later.

I'll figure it out. I will use your link (pyhook)
Respectfully,
Nikita Dedyukhin

User avatar
NPDedyukhin
Posts: 29
Joined: Fri Sep 20, 2019 3:23 am
Location: RU

Re: A script that reads the Enter key, and runs the following script

Wed Oct 09, 2019 12:39 pm

NPDedyukhin wrote:
Wed Oct 09, 2019 7:20 am
scotty101 wrote:
Tue Oct 08, 2019 4:11 pm
Problem is that the python script running in the background won't have keyboard focus so pressing the enter key won't do anything.

Would you be open to using a physical button connected to the GPIO pins? That would be much easier.

Alternatively, have a look at this stack overflow question/answers which suggest using pyhook (may be windows specific however)
https://stackoverflow.com/questions/338 ... d/33864704
or for a linux compatible version
http://jeffhoogland.blogspot.com/2014/1 ... xhook.html
At first everything worked out (the code above using pygame). But now, the keyboard does not respond to pressing Enter. : |

I need to use a keyboard, not a button. I will need to enter data later.

I'll figure it out. I will use your link (pyhook)
The problem was that the following script also used pygame. and therefore an error occurred in the following script that I did not know about.
therefore, in the following script, it was necessary to add a line:
pygame.quit ()
before initializing pygame again

Now everything works successfully, and keyboard keystrokes are processed.

Script Code No. 1:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pygame
pygame.init()
screen = pygame.display.set_mode((400,400))

done = False
while not done:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
            	import select_connection #to run the script number 2
Script Code No. 2 (select_connection.py):

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygame
pygame.quit() #the line that was missing
pygame.init()
screen = pygame.display.set_mode((400,400))

done = False
while not done:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                import k_left #run the following script if the left arrow button is pressed
            if event.key == pygame.K_RIGHT:
                import k_right #run the following script if the right arrow button is pressed
Respectfully,
Nikita Dedyukhin

Return to “Python”