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.
Related
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
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.
I wrote a music player in Python using Pygame and Pydub(I used Pygame for actually playing the music, while Pydub is probably unrelated to the issue).
The music works fine even when windows are switched unless I switch to another Pygame window. I thought this effect would go away if I compiled it(cx_freeze), but that didn't work.
So I was wondering if there is any way to let the music keep playing when the window is switched to another Pygame window.
I used pygame.mixer.music instead of Sound objects if that might somehow be related.
Thanks in advance!
Not exactly a fix, but it seems that if I disable the mixer(pygame.mixer.quit()) it doesn't cancel out the audio. Unfortunately, I haven't found a way to use audio in two pygame windows at once.
Feel free to post another answer if anyone figures out a full fix.
While working on a simple little timer I was creating to simultaneously help me learn more about Python and to help me keep on schedule with my schoolwork; I decided to add a little tune to let me know when your time was up. However, after using the following snippet of code to program each individual note:
winsound.Beep(Freq, Dur)
I realized that whenever I actually played the little song, an odd clicking sound happened between each note. Does anyone know what's going on? Should I use a different expression? Am I just losing my mind?
Edit: Since posting this, I realized what the issue is, the program is waiting about a sixteenth of second in between playing each note. So I'd like to ask another question (any answers relating to the others would still be appreciated), namely, how can I make it not wait in between the notes.
import winsound
winsound.Beep(262,500)
winsound.Beep(277,500)
winsound.Beep(294,500)
winsound.Beep(311,500)
winsound.Beep(330,500)
winsound.Beep(349,500)
winsound.Beep(370,500)
winsound.Beep(392,500)
winsound.Beep(415,500)
winsound.Beep(440,500)
winsound.Beep(466,500)
winsound.Beep(493,500)
winsound.Beep(523,500)
All the above code does is play a simple chromatic scale from middle c to c2, but once one runs it, one would immediately see what Im talking about
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/.