I am trying to achieve the following behavior in a PYTHON 2.4 script, here are the steps, and after them, the question:
Python script starts
The script gives a 3 seconds delay to change to 'Z' program's window
The script does some clicks on the 'Z' program's window.
The script stops making clicks
/* ¿? */
Ask to continue with the program's excecution
/* ¿? */
Go to step 2
So, in steps 5 and 7 what I want to do is simulate the pressing of keys Alt+Tab in order to go back to the script window (in step 5), and go back again to the 'Z' program's window (in step 7).
And the problem is that I have no idea how to achieve this (the simulation to press keys alt+tab), and didn't find answers to my doubts.
I am using the python win32api modules to positionate mouse in a certain point and make the clicks, but I don't find the way to simulate the key pressing.
Try This :
1) Use : https://gist.github.com/chriskiehl/2906125
2)
import win32api
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.Run("app")
win32api.Sleep(100)
shell.AppActivate("myApp")
win32api.Sleep(100)
shell.SendKeys("name")
win32api.Sleep(500)
shell.SendKeys("{ENTER}")
win32api.Sleep(2500)
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
You need the WinApi function SendInput.
See the description in the MSDN:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
An Easier Way
Use this Library W32S
My Library.
And If u want , just copy the source instead
Related
I'm using Windows, Python, and PyAutoGUI to try to automate some activities in Minecraft as a fun project.
I have been successful with using PyAutoGUI to switch to Minecraft once I start the script in Visual Studio Code, click on the "Back to Game" button, and then move the avatar forward by holding the "w" key.
I am using a 3rd party program called "NeatMouse" to use my numpad keys in place of using a mouse. The numpad 8 button is equivalent to moving the mouse up, which in Minecraft causes your avatar to look up. When I press this button myself in Minecraft, it works as expected, so it must be the case that NeatMouse is not the problem.
When I try to have PyAutoGUI replicate this same key press, it seems like nothing is happening.
I have tried different combinations of
pag.press()
pag.hold()
pag.keyDown() & pag.keyUp()
These functions do work for the WASD keys, so I know that Minecraft is able to receive keyboard input from PyAutoGUI, so that generally must not be the problem.
Here is a sample code block of what I have tried.
import pyautogui as pag
def align_vertical_facing_axis(target):
facing = get_facing_axes()[1]
# Looking down
if facing > target:
print('torture')
pag.keyDown('num8')
sleep(1)
pag.keyUp('num8')
get_facing_axes() is a function I wrote to retrieve the axes the avatar is facing as a tuple from the Minecraft debug screen. A positive value in the [1] index means the character is looking at a downward angle. When I run this script, it does print "torture" to my console so I know for sure it is entering that "if" block.
That was the long version of the explanation, the short version is: PyAutoGUI won't press the numpad keys, what do??
After some research, pyautogui.platformModule contains the mappings for numpad.
Here are the windows key mappings: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
import pyautogui as pag
#This is where you update key mappings, num8 is now referring to the virtual key code VK_NUMPAD8 or 0x68
pag.platformModule.keyboardMapping.update({'num8':0x68})
#To call it, you use the mapping name you gave to the virtual key code
pag.press('num8')
Use the list I gave you for other key mapping values such as 0x68 for num8 and so on.
Is there a way to clear the "Run" console in PyCharm?
I want a code that delete/hide all the print() made previously.
Like the "clear_all" button, but without having to press it manually.
I have read that there is a way to do it in a terminal with os.system("cls"), but in PyCharm, it only adds a small square without clearing anything.
Also, I don't want to use print("\n" *100) since I don't want to be able to scroll back and see the previous prints.
In Pycharm:
CMD + , (or Pycharm preferences);
Search: "clear all";
Double click -> Add keyboard shortcut (set it to CTRL + L or anything)
Enjoy this new hot key in your Pycharm console!
Pycharm Community Edition 2020.1.3
You can right click anywhere above the current line on the console, and choose the "Clear All" option. It'll clear the console
How to
Download this package https://github.com/asweigart/pyautogui. It allows python to send key strokes.
You may have to install some other packages first
If you are installing PyAutoGUI from PyPI using pip:
Windows has no dependencies. The Win32 extensions do not need to be
installed.
OS X needs the pyobjc-core and pyobjc module installed (in that
order).
Linux needs the python3-xlib (or python-xlib for Python 2) module
installed.
Pillow needs to be installed, and on Linux you may need to install additional libraries to make sure Pillow's PNG/JPEG works correctly. See:
Set a keyboard shortcut for clearing the run window in pycharm as explained by Taylan Aydinli
CMD + , (or Pycharm preferences);
Search: "clear all"; Double click ->
Add keyboard shortcut (set it to CTRL + L or anything)
Enjoy this new hot key in your Pycharm console!
Then if you set the keyboard shortcut for 'clear all' to Command + L use this in your python script
import pyautogui
pyautogui.hotkey('command', 'l')
Example program
This will clear the screen after the user types an input.
If you aren't focused on the tool window then your clear hot-key won't work, you can see this for yourself if you try pressing your hot-key while focused on, say, the editor, you won't clear the embedded terminals contents.
PyAutoGUI has no way of focusing on windows directly, to solve this you can try to find the coordinate where the run terminal is located and then send a left click to focus, if you don't already know the coordinates where you can click your mouse you can find it out with the following code:
import pyautogui
from time import sleep
sleep(2)
print(pyautogui.position())
An example of output:
(2799, 575)
and now the actual code:
import pyautogui
while True:
input_1 = input("?")
print(input_1)
pyautogui.click(x=2799, y=575)
pyautogui.hotkey('command', 'l')
Easy Method:
Shortcut: Control K,
Right click on terminal and clear Buffer
There's also another way of doing it using the system class from os. All you need to do is have this code:
from os import system, name
# define our clear function
def clear():
# for windows the name is 'nt'
if name == 'nt':
_ = system('cls')
# and for mac and linux, the os.name is 'posix'
else:
_ = system('clear')
# Then, whenever you want to clear the screen, just use this clear function as:
clear()
However, in order for this functionality to work in pycharm, you need to enable "Emulate terminal in output console". You can find this under edit configuration of the file where you want to use the clear function, then it's under Execution option. Here's a screenshot: pycharm screensho
You could just do a ("\n" * 100000000), so it'll be impossible to scroll back.
In PyCharm terminal you can type 'cls' just like in linux terminal.
For Python Console (where you see the output) assign a shortkey for "clear all" in File -> Settings -> Keymap -> Other -> "Clear all"
You can also click somewhere on the PythonConsole -> Right button -> clear.
Hope it helps
I just relised that instead of going to the trouble of setting up a shortcut, you could just set up a command using PyAutoGUI to click on the trash bin on the side of the window e.g
note, to install pyautogui click on the end of the import pyautogui line, then press alt+enter and click install pyautogui.
import pyautogui
# to find the coordinates of the bin...
from time import sleep
sleep(2) # hover your mouse over bin in this time
mousepos = pyautogui.position() gets current pos of mouse
x,y = mousepos # storing mouse position
print(mousepos) # prints current pos of mouse
# then to clear it;
pyautogui.click(x, y) # and just put this line of code wherever you want to clear it
(this isn't perfect thanks to the time it takes to run the code and using the mouse, but it is reasonable solution depending on what you are using it for.)
I hope this answer is helpful even though this is an old question.
Just click the trash can icon to the left of the command window and it clears the command history!
In PyCharm 2019.3.3 you can right click and select "Clear All" button.This is deleting all written data inside of the console and unfortunately this is manual.
Sorry to say this, here the main question is how to do it programmatically means while my code is running I want my code to clear previous data and at some stage and then continue running the code. It should work like reset button.
After spending some time on research I solved my problem using Mahak Khurmi's solution https://stackoverflow.com/a/67543234/16878188.
If you edit the run configuration you can enable "emulate terminal in output console" and you can use the os.system("cls") line and it will work normally.
Iconman had the easiest answer.
But simply printing "\n" * 20 (or whatever your terminal height is) will clear the screen, and the only difference is that the cursor is at the bottom.
I came here because I wanted to visually see how long each step of a complex process was taking (I'm implementing a progress bar), and the terminal is already full of scrolling logging information.
I ended up printing ("A" * 40) * 20, and then "B" and "C" etc., and then filming it. Reviewing the video made it easy to see how many seconds each step took. Yes I know I could use time-stamps, but this was fun!
I am attempting to complete a simple process of opening a web/browser based document, selecting a field within said document, and then copying it so that it goes into my operating system's clipboard. Here's the specs :
Windows 7
Google Chrome ( latest stable )
Python 3.5
pyautogui for keyboard/mouse control
Here is the field I am trying to work with ( http://screencast.com/t/jt0kTagb ). When that little arrow is clicked it pops open to reveal a calendar to pick a date. If you click directly in the field instead it highlights the field's contents. When I manually press CTRL+C in this situation the field's contents go right into the clipboard as expected.
I've tried two methods of getting the field to go into my clipboard. The first was leveraging pyautogui's keyDown/up and press functions which essentially looked like :
imageCoord = noClick("img/date.png")
x, y = pyautogui.center(imageCoord)
pyautogui.click(x, y + 20)
pyautogui.keyDown('ctrl')
pyautogui.press('c')
pyautogui.keyUp('ctrl')
I then attempted to just use the app menu that appears if you right click on something which looked like this:
imageCoord = noClick("img/date.png")
x, y = pyautogui.center(imageCoord)
pyautogui.click(x, y + 20, button='right')
pyautogui.press("down", presses=2)
time.sleep(1)
pyautogui.press('enter')
Lastly I tried the pyautogui.hotkey() function which looked like this :
imageCoord = noClick("img/date.png")
x, y = pyautogui.center(imageCoord)
pyautogui.click(x, y + 20, button='right')
pyautogui.hotKey('ctrl', 'c')
In all three events the field is indeed selected and as best as I can tell the keypresses are going through as all other presses/functions that happen prior go off without a hitch.
The problem that I am facing is that when I do this manually in the same fashion as both of those scripts above I am able to get the contents. When I use the scripts, the clipboard is never updated/populated with the field's contents. Is there something I am overlooking or not considering when working with Python and Window's clipboard?
In the end all I am trying to do is put that value into an excel sheet. Any advice would be appreciated!
I have also discovered this issue on a different automation script, and have been working on troubleshooting it for several days. I'm also on Python 3.5 and Windows 7. I can rule out that it has anything to do with Google Chrome, as my particular script is actually working with SAP.
The documentation for pyautogui on Read the Docs (https://pyautogui.readthedocs.io/en/latest/cheatsheet.html#keyboard-functions) gives a direct example of using Ctrl + C to copy text to the clipboard, so I can verify you're not actually doing something wrong. I believe you're just looking at a bug here.
I have opened an issue on the project's GitHub page:
https://github.com/asweigart/pyautogui/issues/102
Use the PyAutoGui module.
pip install PyAutoGUI
We can easily use HotKey combinations.
See docs: https://pyautogui.readthedocs.io/en/latest/keyboard.html#the-hotkey-function
Pressing Ctrl+C
>>> import pyautogui
>>> pyautogui.hotkey('ctrl', 'c')
I found the solution!
pyautogui.keyDown('ctrl')
pyautogui.keyDown('c')
pyautogui.keyUp('c')
pyautogui.keyUp('ctrl')
In my script I had to use root.update() after.
I know this question is long but what I really want to know is in bold.
I would prefer to use python on Linux.
I'm trying to make a new keyboard layout kind of like devorak but the layout is set to either layout1 or layout2 depending on if you are holding a hot key or not (the hot key should probably be ctrl?)
e.g. press d -> "z" prints to the screen using key layout1
e.g. press ctrl d -> "x" prints to the screen using key layout2
My main problem (and question that needs answering) is the way characters need to print to the screen.
if someone presses the keys (in this order) "(a)(b)(c)(d)(ctrl+d)(shift+e=E)(f)(Enter)"
now lets say the output for these key presses should be "oijzxKb"
I don't want output to render with new lines:
o
i
j
z
x
K
b
I want the characters to appear instantly on the screen as each character is pressed (without waiting for them to press enter).
e.g.
press o
Screenshot1 o
press i
Screenshot2 oi
press j
Screenshot3 oij
.. etc
I assume I will need the following:
a way to read keypresses instantly
a way to print key presses instantly (to the terminal or a GUI or whatever is easiest initially, if it worked on any editor that would be cool!)
I could probably do this in PyGame (but then I probably wouldn't be able to cut and paste etc) and I'm guessing there should be an easier way.
I'm using a Logitech G110 keyboard, I may eventually want to use this as an alternative to my qwerty keyboard on all my applications across all my devices.
Thanks!
EDIT: SOLUTION:
Thanks to the first response,
using Getch from http://code.activestate.com/recipes/134892/
getch = _Getch()
word=""
while True:
c=getch.impl()
if c=="a":
word+="z"
elif ord(c)==127: #backspace
word=word[:-1]
else:
word+=c
print word
This will suffice for now thank you. Once I'm happy with refinement I'll look at doing something lower level, operating system specific without python.
One problem with getch however is that ctrl+a cant be distinguished between ctrl+A (e.g. if you hold ctrl and press keys, it can't tell the difference between upper and lower case)
If it's ok to depends on the X window system, you can use the python-xlib module or the xpyb module to access the X window system and use a XGrabKey call to grab the keyboard related events. Upon each KeyPress event you will be able to print the pressed key.
Now, if you really want to write a keymap, this is totally OS/window system dependent. If you use the X window system (Ubuntu does), you need to check the X documentation about how to write a new keymap. On Ubuntu, the current keymaps definition should be in /usr/share/X11/xkb. Take a look, and try to copy and edit one. You can use setxkbmap to change the current keymap then.
To modify the key mapping of your keyboard, you must use the tools provided by your OS. Most applications don't accept generated events for security reasons.
In your case, that would be xmodmap. Don't forget to create a backup of your current keymap using the -pke option because you will make a mistake - and then, your keyboard won't be working anymore.
If you also want your new keymap work on the console, have a look at the kbd package which changes the keyboard layout at the kernel level.
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.