环境工具
系统archlinux
手柄 北通360, usb
安卓手机 小米11
游戏 流行群侠传
步骤
手机要打开开发者调试, 并通过usb连接测试
不属于此次重点, 可自行搜索, 通过命令行adb
devices测试输出,说明连接成功
安装scrcpy
此工具用来在电脑端远程操作手机, 并且该软件开源, 而且使用还算流畅
在手机电脑正常连接的情况下, 直接启动改程序即可
测试键盘控制
通过键盘测试游戏中一些主要操作的对应按键, 后续跟手柄进行绑定
键盘
前后左右(wsad)
轻击(h)
重击(j)
技能1(u)
技能2/3(i)
随从技能也会一起被放出去,真刺激
闪避(k)
怒气(p)
换武器(l)
编写手柄键盘按键映射程序
这里要注意, 按键使用的是keyboard组件, 测试使用pyautogui会出现连点的情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| """ 通过python + adb + scrcpy的方式,实现手柄玩安卓手机游戏
# Released by rdb under the Unlicense (unlicense.org) # Based on information from: # https://www.kernel.org/doc/Documentation/input/joystick-api.txt
# 参考 https://blog.csdn.net/Enderman_xiaohei/article/details/88050036?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-9-88050036-blog-109139735.pc_relevant_3mothn_strategy_recovery&spm=1001.2101.3001.4242.6&utm_relevant_index=12
https://blog.csdn.net/dhjabc_1/article/details/117444998
使用: linux下需要使用管理员身份运行(触发按键需要), 否则运行报错
sudo python xx.py
""" import pygame
import keyboard import pyautogui import time
class JoyToKey: """ 将手柄映射到键盘输入上 """
def exec(self, joystick, event): if event.type == pygame.KEYDOWN: pass elif event.type == pygame.KEYUP: pass
elif event.type == pygame.JOYBUTTONDOWN: if joystick.get_button(7) == 1: keyboard.press('enter') if joystick.get_button(0) == 1: keyboard.press('k') if joystick.get_button(2) == 1: keyboard.press('u') if joystick.get_button(1) == 1: keyboard.press('i') if joystick.get_button(3) == 1: keyboard.press('p') if joystick.get_button(4) == 1: keyboard.press('h') if joystick.get_button(5) == 1: keyboard.press('j') elif event.type == pygame.JOYBUTTONUP: if joystick.get_button(7) == 0: keyboard.release('enter') if joystick.get_button(0) == 0: keyboard.release('k') if joystick.get_button(2) == 0: keyboard.release('u') if joystick.get_button(1) == 0: keyboard.release('i') if joystick.get_button(3) == 0: keyboard.release('p') if joystick.get_button(4) == 0: keyboard.release('h') if joystick.get_button(5) == 0: keyboard.release('j')
elif event.type == pygame.JOYAXISMOTION: if joystick.get_axis(2) > 0: keyboard.press('h') if joystick.get_axis(5) > 0: keyboard.press('j') if round(joystick.get_axis(1)) < 0: keyboard.press('w') if round(joystick.get_axis(1)) > 0: keyboard.press('s') if round(joystick.get_axis(0)) < 0: keyboard.press('a') if round(joystick.get_axis(0)) > 0: keyboard.press('d')
if joystick.get_axis(2) < 0: keyboard.release('h') if joystick.get_axis(5) < 0: keyboard.release('j') if round(joystick.get_axis(0)) == 0: keyboard.release('a') keyboard.release('d') if round(joystick.get_axis(1)) == 0: keyboard.release('w') keyboard.release('s')
x, y = 482, 305 pyautogui.moveTo(x, y) if joystick.get_axis(3) != 0 or joystick.get_axis(4) != 0: print(y+int(round(joystick.get_axis(4))), x+int(round(joystick.get_axis(3))))
if __name__ == '__main__': pygame.init()
pygame.joystick.init()
joystick_count = pygame.joystick.get_count() print("Number of joysticks: {}".format(joystick_count))
joystick = pygame.joystick.Joystick(0) joyToKey = JoyToKey()
done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True else: joyToKey.exec(joystick, event)
pygame.quit()
|
程序运行
程序使用的是keyboard组件, 在linux下运行时需要sudo
1
| sudo python 北通360-流行群侠传手游.py
|
此时在scrcpy界面, 通过手柄即可控制手机进行游戏操作, 并且流畅性也还可以
说明
只是为了提高下游戏的趣味性, 其它操作可自行发掘
摇杆目前无法适应
摇杆转向跟鼠标适配未实现, 事件刷新太快, 暂时未想到好方法进行转换