我正在尝试编写脚本来模拟游戏手柄,以进行测试。
I've used the inputs library to capture the signals being sent by the gamepad but I'm at a loss when it comes to sending those signals to the computer to interpret (i.e. make the computer press A for me).
Code:
while 1:
events = get_gamepad()
for event in events:
print(event.ev_type, event.code, event.state)
Run Code Online (Sandbox Code Playgroud)
Result:
while 1:
events = get_gamepad()
for event in events:
print(event.ev_type, event.code, event.state)
Run Code Online (Sandbox Code Playgroud)
This resulted from me pressing left on the d-pad and the A button.
I was able to make a script using pynput which lets me type as follows:
from pynput.keyboard import Key, Controller
import time
kb = Controller()
#time before typing
time.sleep(4)
kb.press('h')
kb.release('h')
kb.press('e')
kb.release('e')
kb.press('l')
kb.release('l')
kb.press('l')
kb.release('l')
kb.press('o')
kb.release('o')
Run Code Online (Sandbox Code Playgroud)
So my question is:
How do I send (for example) a press of the A button using a similar method?
# Your code modified
def method_a():
# do something here, maybe react on
# an additional parameter for press/release
pass
def method_b():
pass
while 1:
events = get_gamepad()
for event in events:
print(event.ev_type, event.code, event.state)
if event.code == "A":
method_a()
if event.code == "B":
method_b()
# testing: run indirectly the methods of your
# business logic to mock the hardware
# unittests or similar here
def test_method_aba():
method_a()
method_b()
method_a()
# check state, do assertions
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
244 次 |
| 最近记录: |