Simulate key press on Windows [duplicate] - python

I need to do some macros and I wanna know what is the most recommended way to do it.
So, I need to write somethings and click some places with it and I need to emulate the TAB key to.

I do automated testing stuff in Python. I tend to use the following:
http://www.tizmoi.net/watsup/intro.html
Edit: Link is dead, archived version: https://web.archive.org/web/20100224025508/http://www.tizmoi.net/watsup/intro.html
http://www.mayukhbose.com/python/IEC/index.php
I do not always (almost never) simulate key presses and mouse movement. I usually use COM to set values of windows objects and call their .click() methods.
You can send keypress signals with this:
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys("^a") # CTRL+A may "select all" depending on which window's focused
shell.SendKeys("{DELETE}") # Delete selected text? Depends on context. :P
shell.SendKeys("{TAB}") #Press tab... to change focus or whatever
This is all in Windows. If you're in another environment, I have no clue.

Maybe you are looking for Sendkeys?
SendKeys is a Python module for
Windows that can send one or more
keystrokes or keystroke combinations
to the active window.
it seems it is windows only
Also you have pywinauto (copied from my SO answer)
pywinauto is a set of open-source
(LGPL) modules for using Python as a
GUI automation 'driver' for Windows NT
based Operating Systems (NT/W2K/XP).
and example from the web page
> from pywinauto import application
> app = application.Application.start("notepad.exe")
> app.notepad.TypeKeys("%FX")
> app.Notepad.MenuSelect("File->SaveAs")
> app.SaveAs.ComboBox5.Select("UTF-8")
> app.SaveAs.edit1.SetText("Example-utf8.txt")
> app.SaveAs.Save.Click()

pyautogui is a great package to send keys and automate several keyboard / mouse related tasks. Check out Controlling the Keyboard and Mouse with GUI Automation and PyAutoGUI’s documentation.

You can use PyAutoGUI library for Python which works on Windows, macOS, and Linux.
Mouse
Here is a simple code to move the mouse to the middle of the screen:
import pyautogui
screenWidth, screenHeight = pyautogui.size()
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)
Docs page: Mouse Control Functions.
Related question: Controlling mouse with Python.
Keyboard
Example:
pyautogui.typewrite('Hello world!') # prints out "Hello world!" instantly
pyautogui.typewrite('Hello world!', interval=0.25) # prints out "Hello world!" with a quarter second delay after each character
Docs page: Keyboard Control Functions.
More reading: Controlling the Keyboard and Mouse with GUI Automation (Chapter 18 of e-book).
Related questions:
Python GUI automation library for simulating user interaction in apps.
Python simulate keydown.

Two other options are:
pynput - https://pypi.org/project/pynput/ - which is for Windows (tested), Linux and MacOS- docs are at https://pynput.readthedocs.io/en/latest/
PyDirectInput - https://pypi.org/project/PyDirectInput/ - which is for Windows only and can be used with (or without) PyAutoGUI
Warning - if you are wanting to use keyboard control for games, then pynput doesn't always work - e.g. it works for Valheim, but not for the Witcher 3 - which is where PyDirectInput will work instead. I also tested PyDirectInput and it works for Half life 2 (as a test of an older game).
Tip - You will likely need to reduce (don't remove for games) the delay between character typing - use pydirectinput.PAUSE = 0.05
As an example, here is a function that allows virtual keyboard typing - currently only tested on Windows:
from pynput import keyboard
try:
import pydirectinput
pydirectinput.PAUSE = 0.05
except ImportError as err:
pydirectinput = False
print("pydirectinput not found:")
def write_char(ch):
upper = ch.isupper()
if pydirectinput and pydirectinput.KEYBOARD_MAPPING.get(ch.lower(), False):
if upper:
pydirectinput.keyDown('shift')
print('^')
pydirectinput.write(ch.lower(), interval=0.0)
print(ch)
if upper:
pydirectinput.keyUp('shift')
else:
keyboard.Controller().type(ch)
This allows a string to be sent in, with upper case alphabetic characters handled through pydirectinput. When characters don't simply map, the function falls back to using pynput. Note that PyAutoGUI also can't handled some shifted characters - such as the £ symbol, etc.

Related

Using python to emulate keypresses in an application

I am trying to write a python script that will type out lines from a text document as if they were coming from a keyboard.
I already have a code snippet working in some apps (see below), and this types every line from the file I open correctly, I tested the output into notepad++ for example and it types it all out.
import keyboard
import time
time.sleep(3) """ this gives me enough time to alt-tab into the game (Witcher 3)
that I am trying to have the keypresses inserted into, I also tried some code with
win32gui that brough the Witcher 3 app to the front, but this is simpler. """
with open('w3recipes.txt', 'r', encoding='utf-8') as recipes:
for line in recipes:
keyboard.write(line)
time.sleep(0.05)
The issue is that these keystrokes are not registered by Witcher 3, the game I am trying to write all these keystrokes to. I tried changing the game from fullscreen to windowed with no luck, and I tried compiling the script to a .exe and running it as an admin as well, no dice. I also tried the pynput library as opposed to the keyboard library used here and that yielded the same result.
Any help would be appreciated, I am trying to write a few hundred console commands to this game and there is no newline character in the game's console; it only supports 1 command at a time before hitting enter. My only other option is sitting here copy-pasting all the lines in which would be tiresome.
Thanks in advance.
Use another library, pydirectinput. It's an updated version of pyautogui, and I've found it works with most (if not all) games.
Quoting from docs:
This library aims to replicate the functionality of the PyAutoGUI mouse and keyboard inputs, but by utilizing DirectInput scan codes and the more modern SendInput() win32 function. PyAutoGUI uses Virtual Key Codes (VKs) and the deprecated mouse_event() and keybd_event() win32 functions. You may find that PyAutoGUI does not work in some applications, particularly in video games and other software that rely on DirectX. If you find yourself in that situation, give this library a try!
Write functions:
>>> import pyautogui
>>> import pydirectinput
>>> pydirectinput.moveTo(100, 150) # Move the mouse to the x, y coordinates 100, 150.
>>> pydirectinput.click() # Click the mouse at its current location.
>>> pydirectinput.click(200, 220) # Click the mouse at the x, y coordinates 200, 220.
>>> pydirectinput.move(None, 10) # Move mouse 10 pixels down, that is, move the mouse relative to its current position.
>>> pydirectinput.doubleClick() # Double click the mouse at the
>>> pydirectinput.press('esc') # Simulate pressing the Escape key.
>>> pydirectinput.keyDown('shift')
>>> pydirectinput.keyUp('shift')
# And this is the one you want,
>>> pydirectinput.write('string') # Write string
>>> pydirectinput.typewrite("string")

how to press 'Windows Key' + R pywinauto send_keys function

I've tried to open the "Run" application to execute Windows commands. But I use an ugly function for it. I want to decrease my code size.
I currently use
import pywinauto
pywinauto.Application().start("explorer.exe Shell:::{2559a1f3-21d7-11d4-bdaf-00c04f60b9f0}")
I want to use
import pywinauto
pywinauto.keyboard.send_keys('{RWIN} R')
but it doesn't work.
Execution Movie
I want pywinauto to press the Windows key at the same time as the R key.
You are right, in order to make it work you have to keep the win key pressed down while pressing r key. To achieve this you should use modifiers and actions:
import pywinauto
pywinauto.keyboard.send_keys("{VK_LWIN down}r{VK_LWIN up}")
See the related Pywinauto docs: https://pywinauto.readthedocs.io/en/latest/code/pywinauto.keyboard.html?highlight=send_keys#pywinauto-keyboard

Why pyautogui cannot find items in OSX Dock?

I started to learn pyautogui for my personal project and almost instantly ran into the problems when trying to open OSX dock icons.
I want to open local Spotify which is under Mac Launchpad.
My code to do this.
import pyautogui
launchpad = pyautogui.locateOnScreen('img/Launchpad.png')
This return None so the image was not found.
image example attached
However, if I open Mac OSX Notes window and paste the same image into it and ran the program again the image is found every time. Similarly, if I just leave image open in my Editor.
Is dock actually part of the OSX screen pyautogui can search from? If not how to interact with it?
Figured that using application hotkeys vs find on the screen is a much less brittle approach. Below how I finally build Spotify bot.
import time
import pyautogui
# use pyauutogui key shortcut to open OSX spotlight search
pyautogui.hotkey('command', 'space')
# type spotify and press enter to open application
pyautogui.typewrite('Spotify')
pyautogui.hotkey('enter')
# use Spotify keyboard shortcuts to select search.
# key docs here: https://support.spotify.com/ie/article/Keyboard-shortcuts/
time.sleep(5)
pyautogui.hotkey('command', 'l')
# typewrite allows passing string arguments using keyboard
pyautogui.typewrite('concentration music')
# move to select the song with tab and press enter to play
pyautogui.hotkey('tab', 'tab', 'tab', 'tab')
time.sleep(2)
pyautogui.hotkey('enter')
pyautogui.hotkey('space')
# sleeps 30 seconds while music is playing
time.sleep(30)
pyautogui.hotkey('command', 'q')
I made a program to pertaining your problem. It is a spotify bot aswell.
import pyautogui as p
from time import sleep as t
p.keyDown("command")
p.press("space")
t(2)
p.keyUp("command")
p.typewrite("Spotify")
p.press("enter")
t(3)
p.moveTo(153,132)
p.click()
t(1)
p.typewrite("Never Gonna Give You Up")
p.press("enter")
p.moveTo(707,324)
t(4)
p.click()
t(10)
p.keyDown("command")
p.press("q")
t(1)
p.keyUp("command")
The coordinates will be different pertaining to the position of your spotify window, so you can do p.mouseInfo/pyautogui.mouseInfo to find the exact coordinates and substitute them in the code I gave above.

how to print text in selected text box on windows [duplicate]

I need to do some macros and I wanna know what is the most recommended way to do it.
So, I need to write somethings and click some places with it and I need to emulate the TAB key to.
I do automated testing stuff in Python. I tend to use the following:
http://www.tizmoi.net/watsup/intro.html
Edit: Link is dead, archived version: https://web.archive.org/web/20100224025508/http://www.tizmoi.net/watsup/intro.html
http://www.mayukhbose.com/python/IEC/index.php
I do not always (almost never) simulate key presses and mouse movement. I usually use COM to set values of windows objects and call their .click() methods.
You can send keypress signals with this:
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys("^a") # CTRL+A may "select all" depending on which window's focused
shell.SendKeys("{DELETE}") # Delete selected text? Depends on context. :P
shell.SendKeys("{TAB}") #Press tab... to change focus or whatever
This is all in Windows. If you're in another environment, I have no clue.
Maybe you are looking for Sendkeys?
SendKeys is a Python module for
Windows that can send one or more
keystrokes or keystroke combinations
to the active window.
it seems it is windows only
Also you have pywinauto (copied from my SO answer)
pywinauto is a set of open-source
(LGPL) modules for using Python as a
GUI automation 'driver' for Windows NT
based Operating Systems (NT/W2K/XP).
and example from the web page
> from pywinauto import application
> app = application.Application.start("notepad.exe")
> app.notepad.TypeKeys("%FX")
> app.Notepad.MenuSelect("File->SaveAs")
> app.SaveAs.ComboBox5.Select("UTF-8")
> app.SaveAs.edit1.SetText("Example-utf8.txt")
> app.SaveAs.Save.Click()
pyautogui is a great package to send keys and automate several keyboard / mouse related tasks. Check out Controlling the Keyboard and Mouse with GUI Automation and PyAutoGUI’s documentation.
You can use PyAutoGUI library for Python which works on Windows, macOS, and Linux.
Mouse
Here is a simple code to move the mouse to the middle of the screen:
import pyautogui
screenWidth, screenHeight = pyautogui.size()
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)
Docs page: Mouse Control Functions.
Related question: Controlling mouse with Python.
Keyboard
Example:
pyautogui.typewrite('Hello world!') # prints out "Hello world!" instantly
pyautogui.typewrite('Hello world!', interval=0.25) # prints out "Hello world!" with a quarter second delay after each character
Docs page: Keyboard Control Functions.
More reading: Controlling the Keyboard and Mouse with GUI Automation (Chapter 18 of e-book).
Related questions:
Python GUI automation library for simulating user interaction in apps.
Python simulate keydown.
Two other options are:
pynput - https://pypi.org/project/pynput/ - which is for Windows (tested), Linux and MacOS- docs are at https://pynput.readthedocs.io/en/latest/
PyDirectInput - https://pypi.org/project/PyDirectInput/ - which is for Windows only and can be used with (or without) PyAutoGUI
Warning - if you are wanting to use keyboard control for games, then pynput doesn't always work - e.g. it works for Valheim, but not for the Witcher 3 - which is where PyDirectInput will work instead. I also tested PyDirectInput and it works for Half life 2 (as a test of an older game).
Tip - You will likely need to reduce (don't remove for games) the delay between character typing - use pydirectinput.PAUSE = 0.05
As an example, here is a function that allows virtual keyboard typing - currently only tested on Windows:
from pynput import keyboard
try:
import pydirectinput
pydirectinput.PAUSE = 0.05
except ImportError as err:
pydirectinput = False
print("pydirectinput not found:")
def write_char(ch):
upper = ch.isupper()
if pydirectinput and pydirectinput.KEYBOARD_MAPPING.get(ch.lower(), False):
if upper:
pydirectinput.keyDown('shift')
print('^')
pydirectinput.write(ch.lower(), interval=0.0)
print(ch)
if upper:
pydirectinput.keyUp('shift')
else:
keyboard.Controller().type(ch)
This allows a string to be sent in, with upper case alphabetic characters handled through pydirectinput. When characters don't simply map, the function falls back to using pynput. Note that PyAutoGUI also can't handled some shifted characters - such as the £ symbol, etc.

Alert boxes in Python?

Is it possible to produce an alert similar to JavaScript's alert("message") in python, with an application running as a daemon.
This will be run in Windows, Most likely XP but 2000 and Vista are also very real possibilities.
Update:
This is intended to run in the background and alert the user when certain conditions are met, I figure that the easiest way to alert the user would be to produce a pop-up, as it needs to be handled immediately, and other options such as just logging, or sending an email are not efficient enough.
what about this:
import win32api
win32api.MessageBox(0, 'hello', 'title')
Additionally:
win32api.MessageBox(0, 'hello', 'title', 0x00001000)
will make the box appear on top of other windows, for urgent messages. See MessageBox function for other options.
For those of us looking for a purely Python option that doesn't interface with Windows and is platform independent, I went for the option listed on the following website:
https://pythonspot.com/tk-message-box/ (archived link: https://archive.ph/JNuvx)
# Python 3.x code
# Imports
import tkinter
from tkinter import messagebox
# This code is to hide the main tkinter window
root = tkinter.Tk()
root.withdraw()
# Message Box
messagebox.showinfo("Title", "Message")
You can choose to show various types of messagebox options for different scenarios:
showinfo()
showwarning()
showerror ()
askquestion()
askokcancel()
askyesno ()
askretrycancel ()
edited code per my comment below
You can use PyAutoGui to make alert boxes
First install pyautogui with pip:
pip install pyautogui
Then type this in python:
import pyautogui as pag
pag.alert(text="Hello World", title="The Hello World Box")
Here are more message boxes, stolen from Javascript:
confirm()
With Ok and Cancel Button
prompt()
With Text Input
password()
With Text Input, but typed characters will be appeared as *
GTK may be a better option, as it is cross-platform. It'll work great on Ubuntu, and should work just fine on Windows when GTK and Python bindings are installed.
from gi.repository import Gtk
dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.INFO,
Gtk.ButtonsType.OK, "This is an INFO MessageDialog")
dialog.format_secondary_text(
"And this is the secondary text that explains things.")
dialog.run()
print "INFO dialog closed"
You can see other examples here. (pdf)
The arguments passed should be the gtk.window parent (or None), DestroyWithParent, Message type, Message-buttons, title.
You can use win32 library in Python, this is classical example of OK or Cancel.
import win32api
import win32com.client
import pythoncom
result = win32api.MessageBox(None,"Do you want to open a file?", "title",1)
if result == 1:
print 'Ok'
elif result == 2:
print 'cancel'
The collection:
win32api.MessageBox(0,"msgbox", "title")
win32api.MessageBox(0,"ok cancel?", "title",1)
win32api.MessageBox(0,"abort retry ignore?", "title",2)
win32api.MessageBox(0,"yes no cancel?", "title",3)
Start an app as a background process that either has a TCP port bound to localhost, or communicates through a file -- your daemon has the file open, and then you echo "foo" > c:\your\file. After, say, 1 second of no activity, you display the message and truncate the file.

Categories