2

I am using scrcpy to mirror an Android phone to my computer in order to play a game. The issue is that scrcpy does not support keyboard mapping natively, so I'm writing a Python script to map the keyboard to execute the key presses I need to play the game (using WASD to move around, space to jump, etc.).

I'm fairly new to programing in general and to Python in particular, but so far it's been going pretty well using Pynput. Basically, I am mapping different keys on my keyboard to correspond to mouse clicks on different areas of the screen. My issue is that, as written, my script can only push one left mouse press event at a time.

For example, pressing "w" (to move forward) and space (jump) at the same time will move the cursor to different areas on the screen, and will therefore not result in the desired outcome. The game itself supports simultaneous touch input when played on an Android screen (I can press different areas on the screen at the same time to execute certain actions), so ideally I would need my script to be able to recreate this behavior.

I was wondering if there was a way to do this in Python?

from pynput.mouse import Button, Controller
from pynput.keyboard import Key, KeyCode, Listener

global MOUSE 
MOUSE = Controller()

global CENTER
global HEIGHT

CENTER = 315
HEIGHT = 800

global LISTEN


def cust_click(x,y):
    MOUSE.position = (x,y)
    MOUSE.press(Button.left)

def cust_mvmt_click(x, y):
    MOUSE.position = (CENTER, HEIGHT)
    MOUSE.press(Button.left)
    MOUSE.move(x, y)

#WASD movement

def w():
    cust_mvmt_click(0, -100)

def s():
    cust_mvmt_click(0, 100) 

def a():
    cust_mvmt_click(-100, 0)

def d():
    cust_mvmt_click(100, 0) 

#Miscellaneous
    
def space():
    cust_click(CENTER*5.75,HEIGHT*0.95)

def c():
    cust_click(CENTER*5.15, HEIGHT*1.2)

#Weapon controls

def r():
    cust_click(CENTER*4.75, HEIGHT*1.15)

def f():
    cust_click(CENTER*0.5, HEIGHT*0.7) 

def ctrl():
    cust_click(CENTER*5.15, HEIGHT) 

def q():
    cust_click(CENTER*5.3, HEIGHT*0.77)

def switch1():
    cust_click(CENTER*2.75, HEIGHT*1.15)

def switch2():
    cust_click(CENTER*3.3, HEIGHT*1.15) 

def switch3():
    cust_click(CENTER*3, HEIGHT*1.05)


def on_press(key):

    if key == KeyCode(char='w'):
        w()
    elif key == KeyCode(char='f'):
        f()
    elif key == Key.shift_l:
        ctrl()
    elif key == KeyCode(char='q'):
        q()
    elif key == KeyCode(char='s'):
        s()
    elif key == KeyCode(char='a'):
        a()
    elif key == KeyCode(char='d'):
        d()
    elif key == KeyCode(char='c'):
        c()    
    elif key == KeyCode(char='r'):
        r()
    elif key == Key.space:
        space()
    elif key == KeyCode(char='1'):
        switch1()
    elif key == KeyCode(char='2'):
        switch2()
    elif key == KeyCode(char='3'):
        switch3()
    elif key == Key.tab:
        LISTEN.stop()


def on_release(key):
    if key == Key.shift_l or key == KeyCode(char='1') or key == KeyCode(char='f') or key == KeyCode(char='2') or key == KeyCode(char='3') or key == KeyCode(char='r') or key == KeyCode(char='c') or key == KeyCode(char='s') or key == KeyCode(char='a') or key == KeyCode(char='d') or key == Key.space or key == KeyCode(char='q') or key == KeyCode(char='w'):
        MOUSE.release(Button.left)
        MOUSE.position = (CENTER*390/100,HEIGHT*70/100) #1235, 565


# Collect events until released
with Listener(
        on_press=on_press,
        on_release=on_release) as LISTEN:
    LISTEN.join()

1 Answers1

0

You can use pydirectinput or pydirectinput-rgx for clicking. Check

https://www.google.com/url?sa=t&source=web&rct=j&url=https://pypi.org/project/PyDirectInput/&ved=2ahUKEwi9sbb7w6b6AhXE23MBHV85ChgQFnoECBEQAQ&usg=AOvVaw2EChi0UGXZlMafbw1aHhod

for its documentation.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 26 '22 at 01:39
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32773815) – AnthonyW Sep 27 '22 at 14:13