I tried to use this script to prevent windows screen lock. The script works for moving the mouse, but it doesn't prevent windows 10 from locking.
import pyautogui
import time
import win32gui, win32con
import os
Minimize = win32gui.GetForegroundWindow()
win32gui.ShowWindow(Minimize, win32con.SW_MINIMIZE)
x = 1
while x == 1:
pyautogui.moveRel(1)
pyautogui.moveRel(-1)
time.sleep (300)
Yes it can. But sadly not by moving mouse which I don't know why and would like to know. So, my suggestion is to use pyautogui KEYBOARD EVENTS if possible. I have solved my problems by using VOLUME-UP & VOLUME-DOWN keys. Example code is provided below:
import pyautogui
import time
while True:
pyautogui.press('volumedown')
time.sleep(1)
pyautogui.press('volumeup')
time.sleep(5)
You can use any other keys if you want.
import ctypes
# prevent
ctypes.windll.kernel32.SetThreadExecutionState(0x80000002)
# set back to normal
ctypes.windll.kernel32.SetThreadExecutionState(0x80000000)
https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate
Tested on python 3.9.1, win 10 64bit
Related
I have the following script that is intended to take a screenshot of a video every 5sec by taking a screenshot, then hitting shift-right to fast-foward to the next 5sec interval, repeat. It looks like the shiftdown is not working, as whenever i do it manually, it works but whenever I run the script, the right button works but no shift.
time.sleep(2)
while t < 20:
time.sleep(0.5)
pyautogui.keyDown('shiftleft')
time.sleep(0.5)
pyautogui.press('right')
pyautogui.keyUp('shiftleft')
time.sleep(2)
screenshot = pyautogui.screenshot()
screenshot.save(loc + str(t) + '.png')
t = t + 1
time.sleep(0.5)
I think you should use the pyautogui.hotkey() function instead.
Like this:
import pyautogui as pe
pe.hotkey('shift', 'left)
It basically does the same thing, it presses shift and left and does the thing.
Here's another answer that doesn't require installing pydirectinput. This answer applies to Windows only. From the comments in this question, having numlock or caps lock activated will break <shift> + <arrow> in pyautogui. To get around this, you'll have to detect if either numlock or capslock is active. To do that, you'll have to do Windows API calls. You can do the calls with built-in ctypes or pywin32
Here's an example for ctypes (ctypes code adapted from this answer):
import pyautogui
import time
mywin = pyautogui.getWindowsWithTitle("Notepad")[0]
mywin.activate()
time.sleep(2)
import ctypes
VK_CAPITAL = 0x14
VK_NUMLOCK = 0x90
user32 = ctypes.WinDLL('user32.dll')
if user32.GetKeyState(VK_CAPITAL):
# numlock is active, need to deactivate it before using <shift>+<arrow>
capslock = True
pyautogui.press('capslock')
else:
capslock = False
if user32.GetKeyState(VK_NUMLOCK):
# capslock is active, need to deactivate it before using <shift>+<arrow>
numlock = True
pyautogui.press('numlock')
else:
numlock = False
pyautogui.keyDown('shift')
pyautogui.press('right')
pyautogui.keyUp('shift')
if capslock:
pyautogui.press('capslock')
if numlock:
pyautogui.press('numlock')
If you are using Windows AND if you are willing to try a different module, pydirectinput seems to work better than pyautogui for <shift>+<arrow> key operations. I got the idea from this answer
The example below uses an open copy of Notepad. In Notepad, <shift> + <arrow> will select text. For this example to work, you'll want a some spaces in your file and the cursor would have to be to the left of at least one space.
import pyautogui
import pydirectinput
import time
mywin = pyautogui.getWindowsWithTitle("Notepad")[0]
mywin.activate()
time.sleep(2)
pydirectinput.keyDown('shift')
pydirectinput.press('right')
pydirectinput.keyUp('shift')
I am trying to make a shortcut to open google when I do the function normal it works but when I try to do it in a shortcut it doesn't work.
import pyautogui as pg
import keyboard
hotkey1 = "ctrl+alt+w"
def google():
pg.hotkey("win")
pg.typewrite("google\n", 0.05)
while True:
if keyboard.is_pressed(hotkey1):
google()
You only need a pause until pg can take over. It so happens that when you press the hotkey combo, you might accidentally hit some other keys and therefore it doesn't work as intended. This should give it enough time for the key to be depressed before the automatic process starts.
import time
def google():
time.sleep(0.25)
pg.hotkey("win")
pg.typewrite("google\n", 0.05)
I am trying to achieve a functionality in which I can minimize other apps leaving only my current active window on my app using python. How can this be done?
A similar function is done using Windows + Home key, but how can I do this in my app?
Platform : WxPython
O.s : Windows
First, you need to pynput by using
pip install pynput
Now use this code to press Windows + Home key
from pynput.keyboard import Key, Controller
keyboard = Controller()
keyboard.press(Key.cmd)
keyboard.press(Key.home)
keyboard.release(Key.home)
keyboard.release(Key.cmd)
Then add delay to switch the app you want. Otherwise, it will keep only the python shell. I am adding 5 seconds delay you can change it as you want.
from pynput.keyboard import Key, Controller
from time import sleep
keyboard = Controller()
sleep(5) # this is 5 seconds change as you want
keyboard.press(Key.cmd)
keyboard.press(Key.home)
keyboard.release(Key.home)
keyboard.release(Key.cmd)
I created a short script using pyautogui module with the purpose of avoid a window 10 session to lock due to user but inactivity. But it's not working.
The script is moving the cursor a little each 120 sec.
Any idea why it's not working? Sometime ago I found on web a small program that was doing exactly the same thing and worked.
Let me just clarify that the script works (moving the mouse) but the windows 10 os locks anyway.
import pyautogui
import time
import win32gui, win32con
import os
Minimize = win32gui.GetForegroundWindow() #minimize current window
win32gui.ShowWindow(Minimize, win32con.SW_MINIMIZE) #minimize current window
x = 1
print("Application running...")
while x == 1:
pyautogui.moveRel(1) # move mouse 1 pixels down
time.sleep (120)
#end of script
I have a program which makes the mouse go crazy and point randomly on different places on screen.
it works just fine, but when i go to task manager or simply click ctrl + alt + delete, it stops functioning, even when im exiting task manager.
any help?
(the program still seems to run in the proccess list, but simply doesn't do anything)
the program:
from win32api import SetCursorPos,GetSystemMetrics
from time import sleep
from random import uniform
from win32console import GetConsoleWindow
from win32gui import ShowWindow
win = GetConsoleWindow()
ShowWindow(win,0)
def click(x,y):
SetCursorPos((x,y))
while True:
click((int)(uniform(GetSystemMetrics(0),10)),(int)(uniform(GetSystemMetrics(1),10)))
sleep(0.02)