how to get keyboard input without python shell - python-3.x

the problem = I dont know how to get a program to detect a input from the keyboard without using IDLE
A solution to my problem would look like; some code so that when a key is pressed it sets/makes a variable.
(I am doing this on a raspberry pi)
eg. code code code 'user presses space bar' pi camera takes picture and ends the preview. I dont want it so that the input is in the python shell, just when the space bar is pressed. just to note I am a beginner coder who knows next to nothing so if you can help or point me in the right direction I would be grateful.
edit:
here is the code I have so far
from picamera import PiCamera
from time import sleep
import webbrowser
#importing all the needed things
webbrowser.open('/home/pi/RyansScienceFair/test.html')
#opens a webbrowser that I coded
camera = PiCamera
camera.start_preview(alpha = 220)
sleep(10)
camera.stop_preview()
#runs the camera for 10 seconds with some transparency
what I want is when the user is looking at the web page, which will be displayed under the camera display, they can press spacebar and then the program takes a photo and then ends the camera preview. I can do the camera capture and work out the end time for the camera but its the trigger that I cant figure out.

Related

pynput not working when my cursor is hovering the terminal

I've been working on a 2D game playable in the terminal. I began working on the project in Ubuntu 20.04. I use pynput to tap f11 to get the terminal full-screen and then I use pynput.mouse to get the mouse position so my asset can follow it. Everything worked fine, I had a little controllable ASCII spaceship flying on the screen.
Here is my problem : since I upgraded to Ubuntu 22.04, pynput seems to work only if my mouse is not hovering the terminal, if it is, it does nothing.
This code :
from pynput.mouse import Controller
mouse = Controller()
while True:
print(mouse.position)
works fine while the mouse is outside my terminal window but when I hover the terminal, mouse.position returns always the same value, the last value when my cursor wasn't hovering the window. Same problem with keyboard.tap, the key get pressed only when I'm in another window, like Atom for example.
I use python 3.10.4
EDIT :
I noticed that the problem occurs with the entire gnome interface but works with other apps like Firefox or Atom.
Moreover, I've just tried pyautogui and it behaves the same way as pynput, it doesn't work.

Inconsistent serial fails

I am trying to develop a python tkinter based app that would read the data from my arduino sensor platform.
I am getting inconsistent errors that i can't understand why this happens.
First of all, this is the code that is to blame:
def readSerial():
print("1")
global after_id
print("2")
ser_bytes = ser.readline()
print("3")
ser_bytes = ser_bytes.decode("utf-8")
print("4")
text.insert("end", ser_bytes)
print("5")
after_id = root.after(50, readSerial)
Sometimes this works. The data appear on my tkinter frame, as the characters 1,2,3,4,5 keep scrolling down in the terminal. This is the rarest case.
Other times the terminal printing keeps on going down, but nothing appears on my tkinter text frame. When i press the read button again, the numbers stop printing in the terminal, but the text from the serial appear on my tkinter frame. How is this possible i cannot understand.
Some times the terminal printing keeps on going down, but nothing appears on my tkinter text frame. If i press the 'start reading' button again, the program will get stuck, the button will remain gray, the print in the terminal will stop.
Other times i get freeze for some reason. Make the program will freeze, or the stop button will make it freeze.
Not only i cannot understand why the behaviors except (1) - the normal case occur, i don't understand why there is no consistency in what behavior will be triggered. When behavior 2, or 3 or 4 will happen and why? What triggers each specific behavior?
Can someone understand what is going on?
I would ask myself about your setup (which I don't know what the circuit is):
have you tested your code with test data that works independently of a link to your Arduino.
have you checked the validity of the data coming from the Arduino.
what measures are you taking to avoid noise being introduce with your data input.

Trying to get Python Graphics with text to voice application working

I'm using python3 with Tkinter but I can't get over that i need a mainloop() statemment to get the screen to pop up and draw, then only by closing the graphics screen will the program then go on to the text to speak section. How can I have my graphics displayed without the program halting?

pyautogui.pixel picking up color from upper left of actual position

while True:
x=pyautogui.position()
print(x+pyautogui.pixel(x[0],x[1]))
and program is picking up color that is actually upper left of my cursor...
I am using python 3.6, windows 10, on 125% magnification. is this one of the reason?
Usually picking color with the mouse gives NOT the color of the mouse pointer on the screen but the color of the screen 'below' the cursor.
On my box (Linux Mint) the code:
import pyautogui
while True:
posXY = pyautogui.position()
print(posXY, pyautogui.pixel(posXY[0], posXY[1]) )
if posXY[0] == 0:
break
delivers what it should - the color 'below' the mouse pointer.
Notice the 'break' in the endless loop. This allows you to stop the loop by moving the mouse to the left edge of the screen :)
I have the same setup as yours (but Python 3.7) and faced the very same issue.
Why it occurred? Every now and then I connect and disconnect the external monitor from my laptop. This causes certain scaling issues every time. So, I tried restarting my computer with the external monitor connected and did not disconnect it until I ran this script. It worked like a charm. BTW, I also tried just restarting the Windows Explorer, but this did not resolve the issue.
tldr; Restart your PC and do not modify the display configuration since the restart

Python low level mouse clicks

Using Windows 10 64bit
I'm trying to learn how to code in python and I decided to make my first program a simple bot for a game.
The game is a downloadable game.
The goal is for my mouse to click a button at coordinates (200, 200)
I have tried many different ways to get this to work.
I've tried...
PyAutoGui
pywin32
ctypes
PyAutoGui implementation
pyautogui.click(200, 200)
pywin32 implementation
import win32api, win32con
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
click(10,10)
Any ideas? From what I understand I need to use a low level driver?
I just don't understand how to emulate it as if a real mouse was clicking
try pyautogui.mouseDown() or pyautogui.mouseUp() instead...
info: https://automatetheboringstuff.com/chapter18/
Make sure, that you set the raw input checkbox in game settings for the mouse. I had the same issue with CSGO
Not sure if this will work with your game but you can use PyUserInput.

Resources