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?
Related
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.
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.
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.
In raspberry pi using Python is there any way to make a program where according to the place you click on a designed screen actions can be taken and output given accordingly.
I am using the raspbian-jessie os and have a 7 inch touch screen, I want to create buttons on the screen. When I click on a button it should execute a program.
you probably put the wrong tag, there is nothing to do with functional-programming or graphics, and still little relations with whether you are in raspberry pi or something else
you mean you are in linux and got a touch screen, and want to add some indicator on your touch? what desktop are your using, or something to do with the hardware.?
please state your question clear! and don't let me guess your expection
Look into Kivy or Qt. These are the most popular GUI interfaces for Python.
I'm trying to write a program than will detect when my mouse pointer will change icon and automatically send out a mouse click. Is there a better way to do this than to take screenshots and parse the image for the mouse icon?
EDIT:
I'm running my program on windows 7.
I'm trying to learn some image processing and make a simple flash game i made automated.
Rules: when the curses changes shape, click to get a point.
Also what imaging modules for python will allow you to take a specific size screenshot not just the whole screen? This question has moved to a new thread: "Taking Screen shots of specific size"
The way to do this in Windows is to install either a global message hook with SetWindowsHookEx or SetWinEventHook. (Alternatively, you could build a DLL that embeds Python and hooks into the browser or its Flash wrapper app and do it less intrusively from within the app, but that's much more work.)
The message you want is WM_SETCURSOR. Note that this is the message sent by Windows to the app to ask whether it wants to change the cursor, not a message sent when the cursor changes. So, IIRC, you will want to put a WH_CALLWNDPROC and a WH_CALLWNDPROCRET and check GetCursorInfo before and after to see if the app has done so.
So, how do you do this from Python? Honestly, if you don't already know both win32api and friends from the pywin32 package, and how to write Windows message procs in some language, you probably don't want to. If you do want to, I'd start off with the (abandoned) pyHook project from UNC Assist. Even if you can't get it working, it's full of useful source code.
You should also search SO for [python] SetWinEventHook and [python] SetWindowsHookEx, and google around a bit; there are some examples out there (I even wrote one here somewhere…)
You can look at higher-level wrapper frameworks like pywinauto and winGuiAuto, but as far as I know, none of them has much help for capturing events.
I believe there are other tools, maybe AutoIt, that have all the functionality you need, but not in Python module. (AutoIt, for example, has its own VB-like scripting language instead.)