Can't simulate holding a key in python - python

i'm trying to make a python script that simulates holding down keys using pyautogui or keyboard, but i just can't manage to make it really hold down the key. Instead of typing zzzzzzzzzzzzzzzzzzzzzzzzz it types z. If anyone could help me that would be great if possible please use keyboard or pyautogui library but if not i'm willing to learn another library. Thank you all in advance.
with pyautogui.hold("z"):
#anything
result: z
expected: zzzzzzzzzzzzzzzzzzzzzzz

Related

How to make button presser on Python

I have a question, is it possible to use python to make it so that when pressing the right mouse button, some key on the keyboard is pressed (for example, the G key)
Not sure if this is the best place to ask such question but I'll answer and whatever happens to the thread happens.
From my experience, I think the easiest and most accessible module for keyboard control would be 'keyboard' by BoppreH. I've used it and had no issues. Good cross-platform choice for Windows and Linux, if that's a consideration.
Here are a couple of resources. Should be easy enough to integrate.
A friendly suggestion for next time, friend. Please try looking online first. Happy Coding :)
https://pypi.org/project/keyboard/
https://github.com/boppreh/keyboard

Python SendInput() keypress doesn't get recognised

I tried to simulate keypresses in python, but they dont get recognised the programm. All work in simple editors, some even in an open admin cmd window, but nothing here.
What I tried so far:
-librarys: pyautogui, keyboard, pydirectinput, pynput, pyKey
Solutions with ctypes and SendInput:
Python simulate keydown
Use Python to send keystrokes to games in Windows?
Simulating a keypress in python
All other solutions i found were all using win32api or SendInput and didn't work either.
The usecase is the programm Parsec, if that helps. Also mouse movement and clicks work fine with all of the solutions above.
Could anyone help me with that, or point me in a direction? Because i don't know how to continue.
Some programs, especially video games, are specifically made to be resistant to what you are trying to do. This is to prevent bots and cheating within the game or program. You could find a way to make a driver that simulates key presses, but considering that the program has taken specific measures to defend against this, it is likely bannable or violates the Terms and Service agreement.

Make python write letters

I was making a python project that could play online games and i had a problem. I wanted to make the computer create a user but to do so you had to write in a name. I tried using ctypes but that didnt work out because i couldnt find any way to write letters, only click and move the cursor. Does anyone know a way to make python take control over keyboard? I am thinking something like this:
import ctypes
import webbrowser
webbrowser.open("www.url.com")
ctypes.key.a()
ctypes.key.c()
ctypes.key.c()
ctypes.key.enter()
I would recommend pyautogui. It is as simple as pyautogui.press('b').

Character Control using Python

I was wondering if anyone knows how to import a sprite and let it move when pressing the arrow keys in python, without using pygame, or some other library. This is purely out of curiosity, because I was just thinking about some sort of personal challenge, and that's what came to mind: a python game, without the help of pygame or libtcod. Is this possible, or do you need a library to do this for you? I'd appreciate anyone's input on this one.
Thanks.
What comes to my mind is using Tkinter's Canvas class. It is quite possible to do a simple program like that described using bindings on this class.
A good example of this may be found in Mark Lutz's Programming Python (http://shop.oreilly.com/product/9780596158118.do), the Moving Pics example.
If you don't own this, you can still look at the code for it as a reference by downloading the code using the link on the right side of the page. The path to the appropriat folder is /PP4E-Examples-1.3.1/Examples/PP4E/Gui/MovingPics/.

convert ahk to python

I am learning both autohotkey and python. I wrote this script in ahk (and it works!) that automatically joins tables (using tableninja) in the pokerstars client--
^q::
Loop
{
Send q
Sleep 500
Send {PgUp}
Sleep 500
Send w
Sleep 60000
}
return
I'd like to convert this into python--could you give me an idea as to which modules I can use to accomplish this?
What the python script needs to do is to (while looping) type in a letter (on a notepad that's already open), go down two lines, type in another letter, then wait one minute before starting over.
I am thinking--
import module to auto-type letters
import module that works as timer
def function
type letter q
enter
enter
def function
type letter w
def function
sleep
while True
function
function
function
I am teaching myself how to code. I haven't reached that part about python modules just yet. Thanks!
Assuming you work on windows(don't think AHK runs on anything else), you should check out sendkeys. It will make sending keystrokes a piece of cake. If you want somthing a little more robust, take a look at pywinauto
For the shortcut part, take a look at pyhook
I suggest these modules:
SendKeysCtypes for any sending of keystrokes and sending shortcuts to a window.
SendKeysCtypes is a new and more stable version of SendKeys. I have had issues with SendKeys in the past.
PYHK to deal with global hotkeys - receive hotkeys and trigger functions.
PYHK is based on pyHook and makes hotkey registration very simple. I wrote it because I had the exact same idea as you - I wanted to to do AHK functionality in python.
win32gui for window handling such as moving resizing.
I personally prefer win32gui for short, simple tasks. I use pywinauto for more complex tasks. An example would be if I had to access a menu within a program (like File-New).
mouse.py to control the mouse. This is the most robust way I have found so far. The version I use is an extension of a module I found here at stackoverflow - ctypes mouse_events.
I have personally done several programs for poker with python. I have released source code of my smaller programs. You can find them with source on my website schurpf.com/poker-software.
there's also AutoPy, a cross-platform library for this purpose.

Categories