Simulating key presses in a game using Python - python

Thank you for reading this.
So e.g I start csgo, minecraft, or whatever game. When I'm in game I want to open the chat by pressing the key "y", nevertheless, I've tried using pyautogui, keyboard, win32api and win32con, they only simulate virtual key presses and I guess that's the problem since the games don't accept it as real key presses.
Is there any way I could simulate it as actual key presses?
I appreciate any answer!
Have a good day :)

I don't think Python is the right tool for this, try looking for key mappers (like KeyMapper), they simulate keypresses better afaik and you can easily bound 'y' to press whatever button opens chat in the game you want.

Related

how to press the key automatically in the game?

sorry in advance for my bad english.
I'm writing a game automation program , specifically the game requires a key press to start doing (something) , but when i use KeyBoard.press() function from KeyBoard module i have problem where game doesn't recognize my key i tried in game chat box and it still works , it means the key is still pressed down but the game doesn't pick up , i tried using some other functions like " KeyBoard.press_and_release() , KeyBoard.send() " and some other modules but the result is the same,
here is the example code
While True:
if (...):
keyboard.press('e')
keyboard.release('e')
but what confuses me is when i add time.sleep() between the press and release commands it works but what confuses me is when i add time.sleep() between the press and release commands it works but there is a big delay
Anyone have any solution for me?
While True:
if (...):
keyboard.press('e')
time.sleep(0.05)
keyboard.release('e')
If I understand properly (and, I might not, sorry) it could be an issue with the game rather than python. Some games, especially online games, implement features specifically to stop automation.
Have you tried your code in a different context - another game or, seeing as it simulates key presses, a text editor? I think you are saying it works in the chat box but not for game control inputs. If it works as intended outside of the game but not inside that would suggest the problem is something to do with how the game handles key presses.
In that while loop, you might be simulating 20 key presses a second, while a more usual average would be between about 5 and 7 with the world record reported at 16. Perhaps the game detects if the rate is too high and discounts those key presses. Also, a physical keyboard will be subject to repeat rate and repeat delay, which the game may be testing to specifically try and eliminate this type of virtualized automation. Or, something else entirely - without knowing which game you are trying to automate I am left with guess work.
That said, with your reference to an in-game chat box, I strongly suspect you are trying to automate an online multi-player game. Even if you are just doing it for your own personal interest (and not to get an advantage over other players), that kind of thing is frowned upon and can potentially get your game account banned. I would normally suggest you share which game you are trying to automate with the community to get more specific information, but I doubt you will be happy to do that.

How can I get keys pressed by the keyboard, even while the window isn't focused?

I'm making a key press overlay for rhythm games. How would I go about obtaining keyboard inputs outside of the Python window's focus?
I would assume you would use the keyboard module, but I can't find any documentation on it other than how to have it record and simulate key presses. Upon importing it and looking at possible functions, I see that there are some that appear to have the functionality of getting keys down/up. I have no idea how to use them though. I'm not sure if I'm even on the right track with the keyboard module but maybe one of you guys know.

How can I press the fn+f keys with Pynput?

I need to be able to press the Function key along with one of the f-keys with pynput, but it doesn't seem to recognize the function key as a keypress, nor does it recognize anything as a keypress when I hit both the function and one of the f-keys.
I'm trying to make a program that will change my keyboard backlight, and to do that I need to be able to have the program press fn+f3 and fn+f4, but I can't seem to make that work. Can anyone tell me how I can do this?

How do I send key presses and mouse movements to an application running in the background? (In Python)

Ok, so what I want to do is allow my program to send mouse movements and keypresses (both mouse and keyboard) to a particular task without having to be tabbed into the task so I can do other things on my computer with the mouse and keyboard while the task runs. Is it possible to do this relatively simply with existing python modules?
example of what I want to do:
task='application name'
task.leftclick
task.moveX(int)
task.moveY(int)
task.keypress(r)
All while being able to use my mouse and keyboard normally on a different application.
Thanks for the help in advance!
(Python3.6)
For anyone who happens upon this post, I found https://pypi.org/project/ahk/ I haven't looked into it fully but either you can use this or create an auto hotkey script and just call that with you python code to achieve what I wanted to do in the question.

Get key pressed event

I'm developing translation software for Linux using Python. I'm looking for a way to get the key pressed event. When an english letter key is pressed, I wan the pressed key to be assigned to a variable.
I've tried googling and reading various articles, but haven't had any luck so far.
Using Pygame, you care about the Pygame.KEYDOWN keyboard event. You can see how to use it in the pygame example aliens.py.

Categories

Resources