How i can generate a keyboard event(PRESS F5) using python? - python

I tried this way but it is not working as expected,
import win32api
import win32con
import time
time.sleep(3)
win32api.keybd_event(win32con.VK_F5,0x74,0,0)

Related

I cannot use pyautogui or pydirectinput for game scripting

I'm trying to make a script for stardew valley, the mouse moves correctly and all the other stuff too, but when i use the click function it doesn't work
My code:
from PIL import ImageGrab
from PIL import ImageOps
import pydirectinput
import pyautogui
import keyboard
import time
from numpy import *
test1 = (1400, 720)
test2 = (890, 420)
store = (1150, 420)
time.sleep(1.5)
def main():
# pyautogui.write("t")
# pyautogui.write("/emote angry")
# pyautogui.press("enter")
pyautogui.moveTo(store)
time.sleep(1)
pyautogui.rightClick()
pydirectinput.rightClick()
main()
i've tried with both modules but none of them worked for me, and i have tried with other games and the click works

Why doesn't my Python script work in game?

I've been trying to make a simple script, it presses the key when the image of the key is visible in the left bottom corner of the screen.
The problem is when I'm in the game (Multi Theft Auto) the script stops working only in the game window. I tried running script/game as administrator, changing resolution, making the game windowed/fullscreen but nothing changes. Here's my code:
from numpy import random
import pyautogui
import pydirectinput
import time
import keyboard
import random
import win32api, win32con
sleeptime1 = random.uniform(1.05, 2.03)
sleeptime2 = random.uniform(1.04, 2.01)
sleeptime3 = random.uniform(1.02, 2.05)
while True:
if pyautogui.locateCenterOnScreen('key_a.png', region=(0,580,500,500), grayscale=True, confidence=0.7) != None:
pydirectinput.press('e')
time.sleep(sleeptime1)
if pyautogui.locateCenterOnScreen('key_b.png', region=(0,580,500,500), grayscale=True, confidence=0.7) != None:
pydirectinput.press('q')
time.sleep(sleeptime2)
else:
pydirectinput.click()
time.sleep(sleeptime3)
just run your game as administrator like your python, that should help.

auto update port list in python

I want to create a user interface with tkinter and it includes reading serial ports.
I use code like this
from tkinter.ttk import Combobox
import serial
import sys
import serial.tools.list_ports
from tkinter.messagebox import showinfo
from tkinter import *
window=Tk()
window.title("test cell")
ports=list(serial.tools.list_ports.comports())
selected=StringVar(window)
m_select=Combobox(window,textvariable=selected)
m_select['values']=ports
m_select['state']='readonly'
m_select.place(x=0,y=0)
window.mainloop()
in this case I can read all connected devices but if I connect or disconnect a new device I can not see it and I have to close the file and run it again.
is there any way that I could refresh it automatically and do not need to close it??
See the below example to update the list inside the combobox when the down arrow is clicked.
I've tested this with a dummy function that returns a random number of com ports but this should work the same with the serial.tools.comports function.
(You'll need to comment back in lines that you require, I don't have serial on this PC)
from tkinter.ttk import Combobox
#import serial
import sys
#import serial.tools.list_ports
from tkinter.messagebox import showinfo
from tkinter import *
def comports():
"""Dummy function, remove me for the real thing"""
import random
ports = [f"COM{i}" for i in range(random.randint(1,10))]
return ports
def updateComPortList():
#ports=list(serial.tools.list_ports.comports())
ports = comports()
m_select['values']=ports
window=Tk()
window.title("test cell")
selected=StringVar(window)
m_select=Combobox(window,textvariable=selected,postcommand = updateComPortList)
m_select['state']='readonly'
m_select.place(x=0,y=0)
window.mainloop()

"Import Error: widgets" In Qtile ArchLinux

When I use this code
widget.Battery(
format = 'Battery: {percent:2.0%}',
foreground = colors[2],
background = colors[5],
padding = 5
),
it Work completly Fine but when I use this code
widget.CPU(
foreground = colors[2],
background = colors[4],
threshold = 90,
padding = 5
),
at bar it shows Import Error: CPU and it happnes with memory net and all those widgets
these thing i have imported
import os
import re
import socket
import subprocess
from libqtile import *
from libqtile.config import Click, Drag, Group, KeyChord, Key, Match, Screen
from libqtile.command import lazy
from libqtile.lazy import lazy
from libqtile.utils import guess_terminal
from typing import List
from libqtile.widget import *
You need to install all the requirements of each widget.
For example, for the CPU widget you may need the python library called psutil
You have all the information of each widget in http://docs.qtile.org/en/latest/manual/ref/widgets.html#
You need to change this line
from libqtile.widget import *
to
from libqtile import widget

Simulate real click python

As I can simulate a click as if it were made from the mouse, a game has detection of click events or simulated keys, I would like to know if it is possible to simulate a real click.
Translated.
My code:
import time
import keyboard
import pyautogui
from playsound import playsound
pyautogui.PAUSE = 0.2
sW, sH = pyautogui.size()
down = False
while True:
if keyboard.is_pressed('f4'):
if not down:
down = True
playsound('on.mp3')
pyautogui.moveTo(sW*0.67, sH*0.5)
pyautogui.click()
pyautogui.moveTo(sW*0.67, sH*0.6)
pyautogui.click()
pyautogui.moveTo(sW*0.67, sH*0.64)
else:
down = False
This code simulates a real click of the mouse.
import win32api, win32con
from time import sleep
def click(x,y, duration=30, delay=0.3):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
sleep(duration / 1000)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
sleep(delay)
Why?
Because the lib win32api is directly connected with the windows commands.
When u click with the mouse, you actualy triggers a function inside the windows.
win32api does de same.
Also...win32api is the fastest comunication with the windows for this stuff, like image rendering

Categories