How can you simulate double tapping space in python? - python

I've been trying to code a Minecraft bot for a server that gives fly if you double tap space. However, I've been unable to find a way to click the space bar twice without leaving the initial click still active or too slow to activate the fly effect in the maximum 1.3 second time period. I've tried to find the string value for spacebar in other modules such as win32com and pyautogui but I've been unable to find them by myself or through the internet. I've also used keyboard.release('space') to simulating lifting your finger off from the spacebar, but this is too slow for the fly permission to be activated.
Code:
from pyautogui import *
import pyautogui
import time
import keyboard
import random
import win32api, win32con
import win32com.client as comclt
wsh = comclt.Dispatch("WScript.Shell")
wsh.AppActivate("Crystal Client (v1.8.9-d9d3740/main)")
wsh.SendKeys("{Esc}")
keyboard.press('space')
keyboard.press('space')
keyboard.press('space')

import pyautogui
for i in range(1):
pyautogui.press('space')
or more simply, since PyAutoGUI lets you specify the number of presses you want to perform:
import pyautogui
pyautogui.press('space', presses=2)

The docs for the keyboard module suggest send as used for "single key (e.g. 'space') or multi-key, multi-step hotkey" and I would put this in the multi-step hotkey realm so replacing
keyboard.press()
with
keyboard.send()
allows you to take advantage of the built in press-and-release api
keyboard.send('space', do_press=True, do_release=True)

Related

Maneuvering in window with python

I have been trying to figure out how to automate disabling a particular device as a speaker while keeping it as a microphone. PowerShell would not disable it as a PnP device unless I wanted to disable the entire object. So I have resorted to python and using keys. I have been able to open the sound panel. I've also been able to maneuver between windows. But when I send the down key to enter into the selection menu (as you would typing) it doesn't do anything. I've tried pyautogui and pynput. The general format so far during testing is:
import pyautogui
from pynput.keyboard import Key, Controller
import os, win32gui, time, win32api
os.system('cmd /k "control mmsys.cpl sounds"')
#hwnd = win32gui.FindWindow("none", "Sounds")
#win32gui.SetForegroundWindow(hwnd)
time.sleep(3)
pyautogui.press(
'pagedown')```

How can I send keystrokes and mouse movement to a specific PID?

How can I send keystrokes and mouse movements to a specific running program through its PID. I've used both pywinauto and pynput, and they work great, but I want to send keys to a program that is not in focus. I found this question: How to I send keystroke to Linux process in Python by PID? but it never explains what filePath is a path to.
If you could help solve for this example, that would be great! I want to send the "d" key to an open Minecraft tab for 10 seconds, and then send the "a" key for the next 10 seconds and stop. I would need this to be able to run in the background, so it could not send the keys to the computer as a whole, but only to the Minecraft tab. I am on Windows 10 by the way.
Any help would be appreciated!
Pretty sure you won't be able to, at least not easily let me explain a little bit how all of this works.
Lets start with the hardware and os, the OS has certain functions to read the input you give the computer. This input goes into a "pipe", the OS is reading input, and putting into the pipe, on the other side of the pipe there may be an application running, or it may not. The OS typically manages this (which app to put on the pipe listening) by defining which app/window is active. Apps access this pipe with the API given by the OS, they read the input and decide on it.
The libraries you cited above, change the values of the keyboard and mouse, in other words, they make the OS read other values, not the real ones, then the OS puts them in the "pipe", and are read by the app that is listening on the pipe (the one active). Some apps have their own API's for this, but I would guess Minecraft doesn't. If they don't have an API, what can you do? well, as I said, nothing easy, first of all "hacking" the app, in other words change it to listen to some other input/output rather than the one given by the OS, (this would be you making your own API). The other one would be you changing the OS, which would also be extremely hard, but maybe a tiny bitty easier. It also depends on your OS, I think Microsoft does offer input injection api's
So, simple options, first, run a VM with a GUI and use pywinauto, pyautogui, etc. The other option would be if you can run it in the browser, do so, and use something like Selenium to automate the input.
Quick note, why does selenium works and the browser can read input in the background? Easy, it's not, it just executes the code it would execute if it would have read the input! javascript, cool isn't
With ahk you can do this with Python+AutoHotkey
pip install ahk
pip install "ahk[binary]"
from ahk import AHK
from ahk.window import Window
ahk = AHK()
win = Window.from_pid(ahk, pid='20366')
win.send('abc') # send keys directly to the window
Note that some programs may simply ignore inputs when they are not in focus. However, you can test this works in general even when not in focus by testing with a program like notepad
Full disclosure: I author the ahk library.

how to use python to check whether the system setting for mouse is left-handed?

Motivation
I'm going to use python to click the left mouse button to do some automation test.
My development & test environment
My local development machine(Win10) is left-handed for mouse. It means that I click the left button of my mouse to show the Mouse-Right-Click-Menu.
The test server is right-handed for mouse. I usually use TeamViewer to access the test server, and I don't need additional settings for mouse on the test server for my access. I mean, with TeamViewer, if I click left button of my mouse in the TeamViewer viewport, the Mouse-Right-Click-Menu is popped up on the test server.
Problem
I use pynput(https://pypi.org/project/pynput/) to click the mouse button. Here is a test code:
def simulate_left_click():
from pynput.mouse import Button, Controller
m = Controller()
m.press(Button.right)
On my local machine which is left-handed, this code does NOT pop Mouse-Right-Click-Menu, which is what I want, while on test server which is right-handed, this code pops Mouse-Right-Click-Menu. This inconsistent behavior of this code is annoying.
What I want to do
I want to write a function 'is_system_left_handed()', like this:
def simulate_left_click():
from pynput.mouse import Button, Controller
m = Controller()
if is_system_left_handed():
m.press(Button.right)
else:
m.press(Button.left)
My questions
How to implement the function 'is_system_left_handed()'? is it possible?
If it is not possible, is there any other python package available for this problem?
Thanks
Use GetSystemMetrics(SM_SWAPBUTTON) to detect if the Mouse-Buttons are swapped...
SM_SWAPBUTTON = 23
from win32api import GetSystemMetrics
print(GetSystemMetrics(SM_SWAPBUTTON))
PS:
Requires pywin32: python -m pip install pywin32

Capturing windows keyboard strokes while script isn't current used process

I am trying to make an application that redoes my exact movements on keyboard on a certain process, so I am attempting to register my keyboard strokes time-stamped, I tried to do it using msvcrt or pygame, however they both require keys to be entered to the command prompt itself, else it wont detect it.
Is it possible to create a time-stamped "keylogger" using PYTHON
P.S: I just want to learn more about python scripting.
this is the code i tried:
import msvcrt
import time
import datetime
while True:
char = msvcrt.getch()
print char
print datetime.datetime.now().time()
What you've written is a program that captures inputs to itself. What you need, at least for Windows, are global input hooks. This will allow your application to capture all input to the machine.
Take a look at the following resources:
Windows Hooks Overview
Python for Windows Extensions
PyHook, a Python wrapper for global input hooks

Python : get master volume windows 7

I am trying to built an App in which user has to just scroll his/her mouse over the windows sound icon to change the sound level. Linux users are already familiar with this.
I have divided my problem in these steps:
1.) Get current audio device list using a python api.
2.) Control the master voulme using the api.
3.) Attach a mouse event listener to it.(Sorry i am from Java background).
4.) Get mouse event listener method to do my work .
Plz suggest a proper python API to achieve my task.
And is this the correct approach towards my problem statement or there is a better way to approach this.
For this purpose you could use PyWin32 http://sourceforge.net/projects/pywin32/ or ctypes.
And your approach is pretty fine.
Here's a simple example for mouse with pywin32:
import win32api
import win32con
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
click(10,10)
and here's a similar one with ctypes:
import ctypes
ctypes.windll.user32.SetCursorPos(10, 10)
ctypes.windll.user32.mouse_event(2, 0, 0, 0,0)
ctypes.windll.user32.mouse_event(4, 0, 0, 0,0)
Ctypes is somewhat harder sometimes to figure out and debug (requieres ALOT of time on MSDN), but it's awesomely fast.

Categories