Windows GUI automation – Python Plays Tetris p. 2
This is the second chapter of the series Python Plays Tetris, a series where our goal is to build a script which is able to understand and play the game of tetris.
In this tutorial I will explain windows GUI automation, and more specifically how to simulate the use of the keyboard (keyboard input) on a specific window and how to move the cursor of the mouse with python.
To do this operation we need to have the pywin32 library installed.
If you don’t have it, you can watch the first few minutes of my previous tutorial https://www.youtube.com/watch?v=pOz4xo2mGrI, where I show you how you can install pywin32 easily.
2.1 Simulate Mouse cursor movement
Taking control of the mouse with python is a really simple operation, and it can be done with a single line of code.
After we import the win32api library we simply call the function to set the position of the cursor and then we set the coordinates x and y where we would like to place the cursor.
In the example below I’m moving the cursor to the position 10, 10 of the screen.
import win32api win32api.SetCursorPos((10, 10))
2.2 Simulate keyboard input
To simulate the keyboard input we are using win32api as we did to simulate the mouse input but this time we will do it a bit more advanced.
We are going to simulate pressing a key, not in general, but for a specific window so that even when the code is running we can still use our keyboard regardless of the simulation.
To do this we need find the “hwnd” (windows handle) associated to the window where we want to send the input.
Check the first tutorial (chapter 1.2) to see how to get the hwnd of a specific window.
Once we have the hwnd we can send the command using SendMessage, and inside we pass thress parameters: the hwnd, the action we want to do so in this case we say we want to press the key down, and at last what key we want to press and we defined “Arrow up”.
The second line is the code to say that we are releasing the key. So first we press it and then we release it.
win32api.SendMessage(game_hwnd, win32con.WM_KEYDOWN, win32con.VK_UP) win32api.SendMessage(game_hwnd, win32con.WM_KEYUP, win32con.VK_UP)