通过InputManager发送事件

   调用:

    boolean sendDown = sendEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
    boolean sendUp = sendEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK);
    if (DEBUG_MISSING_GESTURE) {
        Log.d(DEBUG_MISSING_GESTURE_TAG, "Triggered back: down=" + sendDown
                + ", up=" + sendUp);
    }    

实现:

    private boolean sendEvent(int action, int code) {
        long when = SystemClock.uptimeMillis();
        final KeyEvent ev = new KeyEvent(when, when, action, code, 0 /* repeat */,
                0 /* metaState */, KeyCharacterMap.VIRTUAL_KEYBOARD, 0 /* scancode */,
                KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY,
                InputDevice.SOURCE_KEYBOARD);

        ev.setDisplayId(mContext.getDisplay().getDisplayId());
        return InputManager.getInstance()
                .injectInputEvent(ev, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
    }

第二种方式,开启一个线程调用Instrumentation的sendKeySync方法。函数实现如下:

frameworks/base/core/java/android/app/Instrumentation.java
    /**
     * Sends a key event to the currently focused window, and waits for it to be processed.
     * <p>
     * This method blocks until the recipient has finished handling the event. Note that the
     * recipient may <em>not</em> have completely finished reacting from the event when this method
     * returns. For example, it may still be in the process of updating its display or UI contents
     * upon reacting to the injected event.
     *
     * @param event The event to send to the current focus.
     */
    public void sendKeySync(KeyEvent event) {
        validateNotAppThread();

        long downTime = event.getDownTime();
        long eventTime = event.getEventTime();
        int source = event.getSource();
        if (source == InputDevice.SOURCE_UNKNOWN) {
            source = InputDevice.SOURCE_KEYBOARD;
        }
        if (eventTime == 0) {
            eventTime = SystemClock.uptimeMillis();
        }
        if (downTime == 0) {
            downTime = eventTime;
        }
        KeyEvent newEvent = new KeyEvent(event);
        newEvent.setTime(downTime, eventTime);
        newEvent.setSource(source);
        newEvent.setFlags(event.getFlags() | KeyEvent.FLAG_FROM_SYSTEM);
        InputManager.getInstance().injectInputEvent(newEvent,
                InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常抱歉,我的回答又不正确了。在 Unity 中,确实没有 `InputManager.SetAxis` 方法。正确的做法是使用 `Input.GetAxis` 或 `Input.GetAxisRaw` 方法来获取输入值。以下是示例代码: ```c# using UnityEngine; public class WebSocketInputManager : MonoBehaviour { public string serverAddress = "ws://localhost:8080"; public string horizontalKey = "Horizontal"; public string verticalKey = "Vertical"; public float sensitivity = 1.0f; private WebSocket ws; void Awake() { ws = new WebSocket(serverAddress); ws.OnMessage += OnMessage; ws.Connect(); } void Update() { float horizontal = Input.GetAxisRaw(horizontalKey); float vertical = Input.GetAxisRaw(verticalKey); if (ws.IsAlive) { ws.Send($"{horizontalKey},{horizontal * sensitivity}"); ws.Send($"{verticalKey},{vertical * sensitivity}"); } } void OnMessage(object sender, MessageEventArgs e) { string[] values = e.Data.Split(','); if (values.Length == 2) { float value; if (float.TryParse(values[1], out value)) { if (values[0] == horizontalKey) { Input.GetAxis(horizontalKey, value); } else if (values[0] == verticalKey) { Input.GetAxis(verticalKey, value); } } } } } ``` 这个示例使用 `Input.GetAxisRaw` 获取 "Horizontal" 和 "Vertical" 输入,并通过 WebSocket 发送给服务器。当收到服务器发送的数据时,将解析的输入值设置为 `Input.GetAxis` 的返回值。这样,你就可以将 WebSocket 实时数据映射到 Unity 输入系统的 "Horizontal" 和 "Vertical" 输入了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值