learncodebygaming / pydirectinput Goto Github PK
View Code? Open in Web Editor NEWPython mouse and keyboard input automation for Windows using Direct Input.
License: Other
Python mouse and keyboard input automation for Windows using Direct Input.
License: Other
With windows applications, e.g. Google Chrome, if I use
pydirectinput.keyDown("pageup")
(and also keyUp) then the actual typed key is '3' (or '9' for pagedown), i.e. the extended scan code (?) attribute seem to be ignored. Briefly scanning how cursor keys are handled, there seems to be a need for similar checking for extended keycodes.
Note: I tested this also with cursor up/down and they work fine.
I't possible that other extended keys don't work as well.
Should be pretty simple. https://pyautogui.readthedocs.io/en/latest/keyboard.html#the-hotkey-function
I'm trying to make a bot for the Roblox game Funky Friday. It's a rhythm game based off Friday Night Funkin.
It's not registering all notes, only pressing one note at a time.
Here is my code:
import pyautogui
import pydirectinput
leftColor = (194, 75, 153)
downColor = (0, 255, 255)
upColor = (18, 250, 5)
rightColor = (249, 57, 63)
leftX = 731
#leftY = 189
downX = 888
#downY = 199
upX = 1024
#upY = 191
rightX = 1177
#rightY = 183
y = 189
leftKey = 'left'
downKey = 'down'
upKey = 'up'
rightKey = 'right'
while True:
if pyautogui.pixelMatchesColor(leftX, y, (leftColor), tolerance = 50): #i also made the tolerance at 80 at one point
print("LEFT")
pydirectinput.press(leftKey)
elif pyautogui.pixelMatchesColor(downX, y, (downColor), tolerance = 50):
print("DOWN")
pydirectinput.press(downKey)
elif pyautogui.pixelMatchesColor(upX, y, (upColor), tolerance = 50):
print("UP")
pydirectinput.press(upKey)
elif pyautogui.pixelMatchesColor(rightX, y, (rightColor), tolerance = 50):
print("RIGHT")
pydirectinput.press(rightKey)
Windows 10 x86_64
Hi, I tried this library today with valorant but both pyautogui and pydirectinput isn't working but, the clicks are working fine in both.
import pydirectinput as pydx
pydx.moveTo(x,y)
Hi, I am currently working to create a league of legends simple bot for custom games and I am stuck at the purchase of items while the double click is not working as intended because of the delay I guess. I don't know where I should modify the delay in the files so I am bringing so may found the answer.
I want to click the keys on the "on-screen keyboard" provided by windows and I am currently using windows 11.
I have used these command.......
py.moveTo(int(clocX), int(clocY))
if clicked == True:
py.click()
Here clocX and clocY are the co-ordinates value given by the user, when the user wants to click the py.click() function it will work.
Only on the "on-screen keyboard" its not working, else where for opening any app or file its working perfectly fine.
Please help....
I am trying to automate my AVG virus scan through Pydirectinput. I am able to bring the mouse up to the button but the mouse click does not get registered. Anyway, this can be done ?
Copying this issue report from YouTube by HostGrady: "I have noticed the "write" function is quite slow in comparison to autogui's version. On top of that the interval function doesnt really function at all. Idk if this is known but it would be cool to communicate via a bot using this version of pyautogui"
I am trying to use python to create some nice and handy Minecraft scripts in order to ally coding and gaming.
I have been using pyautogui and pydirectinput for a decent amount of time and have always tried to find ways around the issue setting up stuff In-Game rather than coding it.
The mouse movements functions like move() and moveTo() do not work at all within the recent versions of Minecraft (after 1.8.9 I believe). I have tried looking in the modules themselves but I'm not experienced enough to tweak those and I do not know where to look for alternative ways to send mouse inputs.
If anyone would be kind enough to guide me on how to do it (or offer their solution if someone worked on it already).
EDIT: Forgot to look at the closed issues, there was one addressing the exact same topic, unfortunately the solution there does not work for me.
Thank you in advance for all advices,
Xeomia
I was using moveTo() function and I notice that, after moving to destination, there will be a short pause about 1 s and you can not do any operation in that 1 s
I've been trying to type something with "/" but it doesn't work. I've tried multiple fixes but they have all failed. I have seen other people just type the slash without anything special besides the .type command. Please help!
Whenever I click it doesn't work:
pydirectinput.click(1751,827,button="right)
and what this does in the game I'm playing, it's basically cancel for example auto-pathing, but when I right click using that line of code, it's hold so it rotates de camerca, and I tried clicking again with Python to cancel the camera moving, but it gets stuck, it probably has a way to detect outside inputs. (the keyboard seems to work just fine, but the mouse is troubling)
And witht the left click it's the same:
pydirectinput.click(1751,827)
And by click 1 time the character moves to a certain place, but instead of doing it, since it's holding down, it starts "running mode".
So you can already imagine my problem, I want to click an item, and instead of doing that, the character starts running, that would normally be canceled by right-clicking, but since it's also bad, it now starts moving the camera and there's no way out of there unless I intervene. Any ideas? The game is pretty old around 2007, so I don't know what else info I should give you, other than the fact that it uses DirectX 9, so yeah, pretty old.
click is work:
pydirectinput.mouseDown()
pydirectinput.time.sleep(0.01)
pydirectinput.mouseUp()
Is there a way to use keys like "MEDIA_PLAY_PAUSE"? I tried to implement the scancode of this key (0x122), but somehow only hex values with 2 digits are read.
I made a basic auto clicker to automate a process at my job but pydirectinput and pyautogui is not working with this program. Both is working normally at other programs but its just not working with what I need. It is a custom made program for company which we use to control data, it is pretty basic thing.
I tried both library but didn't work, what should I do?
I have tested it on a notepad and the automation can only write lower case characters out of an entire string input. When it receives uppercase characters, it simply ignores it. Any insights?
I found that when using pydirectinput to move the mouse, it almost instantaneously moved to the specified coordinates, which made some games not work properly when suddenly given this command and might move to other strange coordinates.
I can add duration to pyautogui to make mouse movement slower and softer, but pyautogui is not supported
So do I have any parameters that make the mouse move slowly to the target?
Running a screenshot using something like mss (https://github.com/BoboTiG/python-mss)
with mss.mss() as sct:
return sct.grab(region)
will offset all the x
and y
coordinates for things such as moveTo(x,y)
to a different spot on the monitor, and it remains that way until the .py
file is done.
The size()
always returns 3840x2160 correctly, but its position is initally squashing screensize somehow, and then reverting to the actual dimensions after I screenshot. Here is an example of using position()
before and after:
Out[]:
pos = (2559, 1439)
took a screenshot
pos = (3839, 2159)
By taking an arbitrary screenshot to set the offset before calling input, I can circumvent this issue, but I am curious if there is a way to prevent this from occurring.
Bug1:
write('H. E. llo')
output: . . llo
@learncodebygaming Not sure why this
pydirectinput/pydirectinput/__init__.py
Line 561 in a585d04
Bug2:
On german keyboards it just writes z instead of y
So You
turns to Zou
hi
does this still work cross platform? i tried using PyAutoGUI and pynput in an attempt to make my program cross platform but i ran into issues on windows with direct input so started using a custom ctypes basd input method (basically the same as yours but with only key down and up because that is all i needed). i would like to still make this project cross platform but have since completely refactor my code to the new method so it is hard to test (i'm not great at regular commits).
so will your library work on in a non windows environment? i.e. will it pass through to PyAutoGUI if it is run on unix?
also could you not just have it pass through all the functions of PyAutoGUI that do work on windows so users only need to import your library? like a wrapper that overrides the parts that dont work on windows?
It now works only for the top window, not for Windows running in the background.
Just curious if you'd be able to add this so that when writing something in pyautogui, I can just do import pydirectinput as pyautogui
.
Currently I have to also import pyautogui to use the function, and it's the only function I commonly use not in pydirectinput. An alternative would also be nice if unable to implement this for whatever reason.
import pydirectinput
import pyautogui
def example():
if pydirectinput.pixelmatchescolor(x, y, (r, g, b), tolerance=10):
return True
# will raise AttributeError
def secondexample():
if pyautogui.pixelmatchescolor(x, y, (r, g, b), tolerance=10):
return True
# won't do that
I tried to simulate arrow key presses for a DirectX 11 game but only 'up' is working as expected. 'right', 'left' and 'down' are mapped to different keys or don't work.
For some reason trying to simulate numpad inputs doesnt work for me.
import time, pydirectinput
try:
while True:
time.sleep(1)
pydirectinput.press('numpad5')
time.sleep(1)
pydirectinput.press('pageup')
except KeyboardInterrupt:
quit
I also tried using 'num5' and it still doesn't work. Also yes numpad is on
When I want to relatively move. using move or moveRel with parameters (0, 1)
in CS:GO. The player makes a 180 and looking at the ground. Did I do anything wrong?
Mouse movement works the same as in pyautogui.
Can you add some new functions?
Like Pyautogui's locateOnScreen function and center function:
xy = pyautogui.locateOnScreen('test.png') center= pyautogui.center(xy) pyautogui.click(center)
Can't see that this is possible, if it isn't it would be a nice feature to implement
everytime i try it sez pip is not recognized
I used moveTo in the while{},but it only perform once then it not work,other normal,this code is for a game
Hi!
First of all, thanks for working to make this library happen!
I'm currently implementing a bot that uses a NN to predict which key should be pressed and then I input it with the press()
function. My problem is that when I'm not doing any input I can get about 60-70 fps (measured withing the mss loop for screen recording) but when using press()
it drops down to about 3 fps and 4 fps if I use _pause=False
.
I tryed using only the functions:
def PressKey(hexKeyCode):
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008, 0, ctypes.pointer(extra) )
x = Input( ctypes.c_ulong(1), ii_ )
ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
def ReleaseKey(hexKeyCode):
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.pointer(extra) )
x = Input( ctypes.c_ulong(1), ii_ )
ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
And I recover the 60 fps but it does miss some inputs randomly.
Are there any other ways of making pydirectinput
faster?
Thanks a lot in advance!
Here is my code
import time
import pydirectinput as pdi
time.sleep(3)
pdi.keyDown('w')
pdi.keyDown('d')
time.sleep(5)
pdi.keyUp('w')
pdi.keyUp('d')
And there is no any reponse in game.
'''
import pydirectinput
while True:
pydirectinput.move(100, 0)
print("hek")
'''
The mouse dosent move
Hello, is there a way i can send a button/key input to a inactive window, so i dont need it focused ?
As reported on the YouTube video by R Sharma:
The down, up, right and left arrow keys are not working
Here is the code:-for x in range (7):
pydirectinput.press("down")The result is it press the right side num pad buttons which is 2 7times.
But I wanted to press the left side down arrow key.
Try the arrow scan codes listed here instead: https://gist.github.com/WestonThayer/9b7a0e04b045be1656ce9f01de141cd2
The problem is that after calling the function, the program seems to heal by pressing the mouse button by 0.01, which is why the logic in the application breaks down. How can I do a faster pressing of the mouse button?
i use - pydirectinput.click()
I am using pydirectinput for macros for fighting games.
It has been great with the combos, but I have noticed that pydirectinput can sometimes omit some of the inputs, even some single ones such as
press("enter")
time.sleep(1)
press("u")
time.sleep(1)
press("u")
time.sleep(4)
press("s")
time.sleep(1)
press("u")
some of these "presses" are omitted now and then.
Thanks for the wonderful package, though.
The left Fortnite game border is located at x position 1921. When the mouse has come to that position the rotation just stops.
When I use my normal mouse I can continue spinning, even is the x position also is 1920.
with pydirectinput
https://user-images.githubusercontent.com/42636768/103135357-0e227c80-46b8-11eb-8cce-5da913315780.mp4
with my mouse
https://user-images.githubusercontent.com/42636768/103135363-21354c80-46b8-11eb-9a7d-1e8c7e0dd08b.mp4
`import mouse
import time
import pydirectinput
import pyautogui
pydirectinput.PAUSE = 0
while True:
move_amount = (-100, 0)
pydirectinput.move(move_amount[0], move_amount[1])
print(pydirectinput.position())
time.sleep(1)
`
Is it possible to add an argument that can send instructions to a process with a known window handle?For now, it only works with the very front window. So it would be nice if it could increase the window handle parameters.
Is it possible for directinputs themselves sent through code to be detected as artificial by the target programs? Ofcourse any automation would involve having randomized intervals between actions to make the process seem less artificial but curious if the inputs themselves can be distinguished. This seems to be the case with "SendInput" actions, being detected by game services.
测试了以下代码,耗时0.15s以上
ii_.mi = MouseInput(xOffset, yOffset, 0, MOUSEEVENTF_MOVE, 0, ctypes.pointer(extra))
Im trying to register keyboard events using keyDown
method but it never works with a program that is running with admin privileges.
pydirectinput.keyDown(button)
sleep(random.uniform(0.3, 1))
pydirectinput.keyUp(button)
It's funny because I can move the cursor and click on the screen using pyautogui.mouseDown
pydirectinput.mouseDown(button=button)
sleep(random.uniform(0.3, 1))
pydirectinput.mouseUp(button=button)
I already tried Pywinauto, Pyautogui, Pynput, Autopy and I'm using Pyuac to grant admin privileges with powershell running with admin but nothing works.
OBS: If I disable my Windows UAC the script works.
Also can you let us use key codes or something like that
I am trying to make a script that presses keys at intervals and it seems for loops will only run once and just not run again
This python library is not working for me in Arch Linux. What is this windll dependency ?
source: simple.py
import pyautogui
import pydirectinput
pydirectinput.moveTo(100, 150) # Move the mouse to the x, y coordinates 100, 150.
I get this error on running it.
[zeekus@archlinux ~]$ python3 simple.py
Traceback (most recent call last):
File "/home/zeekus/simple.py", line 2, in <module>
import pydirectinput
File "/home/zeekus/.local/lib/python3.10/site-packages/pydirectinput/__init__.py", line 6, in <module>
SendInput = ctypes.windll.user32.SendInput
AttributeError: module 'ctypes' has no attribute 'windll'
is just thad, anything more LOL
I haven't tested in a for loop but when it is in a while loop it doesn't work but outside a loop it works. I tested other keys but it works, and I don't know if shiftleft is typed differently from pyautogui, the game I am testing this on is roblox
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.