Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Python SendKeys Win32API


ghost's Avatar
0 0

I'am trying to send keystrokes to a program with Python, that part went well, but I'am having problem with holding/pressing the key down for a period of time. A friend tiped me of thet this code works:

win32api.Sleep(5000) 
shell.SendKeys("<KeyRelease-w>")```

The problem is that it also sends the <KeyPress-w> to the app too.
Anyone know a solution for this?


ynori7's Avatar
Future Emperor of Earth
0 0

What exactly are you trying to do? Why do you need to send keystrokes?


reaper4334's Avatar
Member
0 0

What exactly are you trying to do?

[Edit] Well, that's awkward having said the exact same thing as ynori. His post wasn't there when I posted. [/Edit]


ynori7's Avatar
Future Emperor of Earth
0 0

reaper4334 wrote: His post wasn't there when I posted.

That's hard to believe since I posted 15 minutes before you. You must type reeeeaaally slow.


ghost's Avatar
0 0

I'am trying to tell a application that I'am holøding down a certain key, like moving around inside a game.


reaper4334's Avatar
Member
0 0

ynori7 wrote: [quote]reaper4334 wrote: His post wasn't there when I posted.

That's hard to believe since I posted 15 minutes before you. You must type reeeeaaally slow.[/quote] haha I probably just opened the thread, got distracted, then came back and didn't refresh. Sorry lol

ctrl_ wrote: I'am trying to tell a application that I'am holøding down a certain key, like moving around inside a game. Errrr… Any more specifics?

[Edit] You could try checking what the active window is and then using that.

while someloop:
    if window="Internet Explorer":
        # send the key
    else:
        # don't
    if window != prev_window:
        prev_window = window
        break

Just a rough idea


ghost's Avatar
0 0

What I'm trying to is to move a character of some sort inside a game by holding down the varius movement keys like W, A, S and D. The problem so far is that I can't find a way to press/hold down the key for a period of time. I have tryed the following code and it works, but the problem is that it sends everything inside the "" and not W, but it does work for some reason.

import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
shell.AppActivate("Garry's Mod")
win32api.Sleep(100)
shell.SendKeys("<KeyPress-w>")
win32api.Sleep(5000)
shell.SendKeys("<KeyRelease-w>")

What I'm trying to find is a way to to that without sending all the other keys to the app to.


reaper4334's Avatar
Member
0 0

ctrl_ wrote: What I'm trying to is to move a character of some sort inside a game by holding down the varius movement keys like W, A, S and D. The problem so far is that I can't find a way to press/hold down the key for a period of time. I have tryed the following code and it works, but the problem is that it sends everything inside the "" and not W, but it does work for some reason.

import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
shell.AppActivate("Garry's Mod")
win32api.Sleep(100)
shell.SendKeys("<KeyPress-w>")
win32api.Sleep(5000)
shell.SendKeys("<KeyRelease-w>")

What I'm trying to find is a way to to that without sending all the other keys to the app to.

You mean it sends "<KeyPress-w>"?

If so, have you tried:

import win32com.client

shell = win32com.client.Dispatch(&quot;WScript.Shell&quot;)
shell.AppActivate(&quot;Garry&#39;s Mod&quot;)
win32api.Sleep(100)
shell.SendKeys(&quot;w&quot;)
win32api.Sleep(5000)
shell.SendKeys(&quot;w&quot;)

??


ghost's Avatar
0 0

Then it just sends the "w" once and waits 5 sec before it sends another. With the <KeyPress-w> and <KeyRelease-w> it does hold down the key but it also sends the K-e-y-P-r-e-s-s.


reaper4334's Avatar
Member
0 0

ctrl_ wrote: Then it just sends the "w" once and waits 5 sec before it sends another. With the <KeyPress-w> and <KeyRelease-w> it does hold down the key but it also sends the K-e-y-P-r-e-s-s.

Hmm.. in that case, have you checked to see if "KeyPress" and "KeyRelease" have their own functions rather than being used in "SendKeys"? That's all I can really think of.

Have you tried doing a loop?

import win32com.client

shell = win32com.client.Dispatch(&quot;Wscblockedript.Shell&quot;)
shell.AppActivate(&quot;Garry&#39;s Mod&quot;)
i = 0
while i &lt;= 5000:
    shell.SendKeys(&quot;w&quot;)
    win32api.Sleep(10)
    i += 10

I doubt that'l work, though, if it's being used in a game.. it'l probably end up walking, stopping, walking, stopping.. etc.. but it's worth a try.


ghost's Avatar
0 0

I have checked if they got owne functions but I was only able to find some for linux and not windows.

reaper4334 wrote:

import win32com.client

shell = win32com.client.Dispatch(&quot;Wscblockedript.Shell&quot;)
shell.AppActivate(&quot;Garry&#39;s Mod&quot;)
i = 0
while i &lt;= 5000:
    shell.SendKeys(&quot;w&quot;)
    win32api.Sleep(10)
    i += 10

I have also tryed this with poor results.