Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
KeyChannelResponder.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package io.flutter.embedding.android;
6
7import android.view.KeyEvent;
8import androidx.annotation.NonNull;
9import io.flutter.embedding.engine.systemchannels.KeyEventChannel;
10
11/**
12 * A {@link KeyboardManager.Responder} of {@link KeyboardManager} that handles events by sending the
13 * raw information through the method channel.
14 *
15 * <p>This class corresponds to the RawKeyboard API in the framework.
16 */
17public class KeyChannelResponder implements KeyboardManager.Responder {
18 private static final String TAG = "KeyChannelResponder";
19
20 @NonNull private final KeyEventChannel keyEventChannel;
21
22 @NonNull
23 private final KeyboardManager.CharacterCombiner characterCombiner =
25
26 public KeyChannelResponder(@NonNull KeyEventChannel keyEventChannel) {
27 this.keyEventChannel = keyEventChannel;
28 }
29
30 @Override
31 public void handleEvent(
32 @NonNull KeyEvent keyEvent, @NonNull OnKeyEventHandledCallback onKeyEventHandledCallback) {
33 final int action = keyEvent.getAction();
34 if (action != KeyEvent.ACTION_DOWN && action != KeyEvent.ACTION_UP) {
35 // There is theoretically a KeyEvent.ACTION_MULTIPLE, but theoretically
36 // that isn't sent by Android anymore, so this is just for protection in
37 // case the theory is wrong.
38 onKeyEventHandledCallback.onKeyEventHandled(false);
39 return;
40 }
41
42 final Character complexCharacter =
43 characterCombiner.applyCombiningCharacterToBaseCharacter(keyEvent.getUnicodeChar());
44 KeyEventChannel.FlutterKeyEvent flutterEvent =
45 new KeyEventChannel.FlutterKeyEvent(keyEvent, complexCharacter);
46
47 final boolean isKeyUp = action != KeyEvent.ACTION_DOWN;
48 keyEventChannel.sendFlutterKeyEvent(
49 flutterEvent,
50 isKeyUp,
51 (isEventHandled) -> onKeyEventHandledCallback.onKeyEventHandled(isEventHandled));
52 }
53}
KeyChannelResponder(@NonNull KeyEventChannel keyEventChannel)
void handleEvent( @NonNull KeyEvent keyEvent, @NonNull OnKeyEventHandledCallback onKeyEventHandledCallback)