Python SendInput() keypress doesn't get recognised - python

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.

Related

Why does no Python lib move the mouse in-games (CS:GO, GTA, etc.)

I tried out many Python libaries including but not limited to: PyDirectInput, PyAutoGui, Clicknium, Autoit, Mouse, PyGame and many more, but only win32api seems to work with win32api.mouse_event(win32con.MOUSEEVENTF_MOVE). The problem with that WinAPI is that it doesn't actually reliably work on all machines, e.g the mouse driver may interfere and make the movement sloppy and make it go all over the place, just like with a high sensitivity. With a high sensitivity it actually doesn't move where you want it to move it actually just moves somewhere random and flicks all over the place. I did an intense amount of research and found no real libary which could achieve actual mouse movement in a game like CS:GO other than the buggy win32api. Do you know any lib that might actually work? Thanks in advance!
Have you tried any of the python bindings for AutoHotkey? I've never really bothered doing anything serious with the python bindings, but AutoHotkey always worked reliably for me when I was doing similar stuff with games. It might be worth a shot. A quick search turned up two potential candidates:
ahk: The more well-known of the two. Skimming through the code, it looks like it's translating the Python calls into an AHK script and running it with the ahk executable.
AutoHotkey.py: Seems to embed python into AHK and executes your Python script after applying some extensions.

Trying to Automate a Keyboard Shortcut, Have Tried both Pyautogui and Keyboard

I'm trying to automated a keyboard shortcut (ctrl+ e) I have tried both pyautogui and the keyboard function, however whenever I run my code instead of executing the shortcut in the application it executes in the command line. It literally just types "e" in the command line and I don't know how to fix this, it's driving me insane.
Literally no one else seems to be having this problem so I'm hoping that I can get an answer here. I have tried:
pyautogui.hotkey('ctrl','e')
keyboard.press('ctrl')
keyboard.press('e')
keyboard.release('ctrl')
keyboard.release('e')
neither have been successful. My system is Darwin 18.6.0 if that helps.
You are almost there. keyboard can do the thing but you are using it the wrong way. You can create combinations using keyboard. Here is how to do it with keyboard:
keyboard.press("ctrl+e")
keyboard.release("ctrl+e")
Make sure to add delay before so that you can switch to your window

Key repeat in browser from python code

Note : I am pretty surprised I did not see that same question on this forum, if I missed it please link in comment :)
I am writing a bot in python that would eventually be able to play a browser-based video game (HTML5 canvas game, not Flash). In order to do that I need to simulate the keyboard events from the python code.
I thought pyautogui would do the work, but unfortunately it seems the only way to repeat a key is to do pyautogui.press(key) in a loop, and it is not good at all for playing games as the move is not smooth at all.
I found some solutions for Windows, but I need a solution for macOSX.

How to run Pygame in Mac background

Im wondering how to provide keyboard input to a running python/pygame program that is in my Mac's background.
Have moved this question to:
How to test if global key is pressed on Mac using PyObjC and Python.
And no longer using Pygame.
I think I interpret this question as: "I need to automate keyboard input". If so, dogtail
is a good idea. However, since your program is going to be in the background, this may complicate matters, do you have the source to the game you're trying to pass input to?

How to get in python the key pressed without press enter?

I saw here a solution, but i don't want wait until the key is pressed. I want to get the last key pressed.
The related question may help you, as #S.Lott mentioned: Detect in python which keys are pressed
I am writting in, though to give yu advice: don't worry about that.
What kind of program are you trying to produce?
Programas running on a terminal usually don't have an interface in which getting "live" keystrokes is interesting. Not nowadays. For programs running in the terminal, you should worry about a usefull command line User Interfase, using the optparse or other modules.
For interative programs, you should use a GUI library and create a decent UI for your users, instead of reinventing the wheel.Which wouldb eb etter for what you ar trying to do? Theuser click on an icon,a window opens on the screen, witha couple of buttons on it, and half a dozen or so menu options packed under a "File" menu as all the otehr windws on the screen - or - a black terminal opens up, with an 80's looking text interface with some blue-highlighted menu options and so on?. You can use Tkinter for simple windowed applications, as it comes pre-installed with Python + Windows, so that yoru users don't have to worry about installign aditional libraries.
Rephrasing it just to be clear: Any program that requires a user interface should either se a GUI library, or have a WEB interface. It is a waste of your time, and that of your users, to try and create a UI operating over the terminal - we are not in 1989 any more.
If you absolutely need a text interface, you should look at the ncurses library then. Better than trying to reinvent the wheel.
http://code.activestate.com/recipes/134892/
i think it's what you need
ps ooops, i didn't see it's the same solution you rejected...why, btw?
edit:
do you know:
from msvcrt import getch
it works only in windows, however...
(and it is generalised in the above link)
from here: http://www.daniweb.com/forums/thread115282.html

Categories