SavinaRoja / PyUserInput

A module for cross-platform control of the mouse and keyboard in python that is simple to install and use.
GNU General Public License v3.0
1.07k stars 244 forks source link

How to press 2(or more) keys then release them respectively ? #105

Open j7168908jx opened 6 years ago

j7168908jx commented 6 years ago

I was using PyUserInput on windows and was trying to simulate different kind of input. (py27, win64)

I tried something like this:

import ...    
k = pykeyboard.PyKeyboard()
k.press_key('a')
k.press_key('b')
time.sleep(2)
k.release_key('a') # line a
k.release_key('b') # line b

And I want it to help me play games like 'osu!' or something similar. In this game, it requires player to do the key pressing and releasing with the rythm of music, and one of the rythm pattern looks like above.

However, I found that this script cannot perform line a or b as I expected, which is something like pressing key 'a' and 'b' at the same time, and after exactly 2 seconds, I release both of them. As indicated from the game, I knew that one of the key 'a' and key 'b' was never released, until the next time I used k.press_key('a') .

How may I fix the problem ? I have tried something like this:

import ...
a = pykeyboard.PyKeyboard()
b = pykeyboard.PyKeyboard()
a.press_key('a')
b.press_key('b')
time.sleep(2)
a.release_key('a')
b.release_key('b')

and it's not working.

By the way, I encountered the long press problem, as PyKeyboard cannot simulate something like one was pressing the really keyboard in a text field, which would display like 'aaaaaaaaaaaaaaaaaa'. Though It seemingly had no infulence on my game, as the game thought I was pressing the key, I guess there may be something influencing both of my questions.

Thanks!

j7168908jx commented 6 years ago

I tried the same in C++, where I use

keybd_event(val,0,0,0);
keybd_event(val,0,KEYEVENTF_KEYUP,0);

to do the pressing and releasing, which worked well in my case.