Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
KeyboardChannel.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.engine.systemchannels;
6
7import androidx.annotation.NonNull;
8import androidx.annotation.Nullable;
9import io.flutter.plugin.common.BinaryMessenger;
10import io.flutter.plugin.common.MethodCall;
11import io.flutter.plugin.common.MethodChannel;
12import io.flutter.plugin.common.StandardMethodCodec;
13import java.util.HashMap;
14import java.util.Map;
15
16/**
17 * Event message channel for keyboard events to/from the Flutter framework.
18 *
19 * <p>Receives asynchronous messages from the framework to query the engine known pressed state.
20 */
21public class KeyboardChannel {
22 public final MethodChannel channel;
23 private KeyboardMethodHandler keyboardMethodHandler;
24
25 @NonNull
26 public final MethodChannel.MethodCallHandler parsingMethodHandler =
27 new MethodChannel.MethodCallHandler() {
28 Map<Long, Long> pressedState = new HashMap<>();
29
30 @Override
31 public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
32 if (keyboardMethodHandler == null) {
33 // Returns an empty pressed state when the engine did not get a chance to register
34 // a method handler for this channel.
35 result.success(pressedState);
36 } else {
37 switch (call.method) {
38 case "getKeyboardState":
39 try {
40 pressedState = keyboardMethodHandler.getKeyboardState();
41 } catch (IllegalStateException exception) {
42 result.error("error", exception.getMessage(), null);
43 }
44 result.success(pressedState);
45 break;
46 default:
47 result.notImplemented();
48 break;
49 }
50 }
51 }
52 };
53
54 public KeyboardChannel(@NonNull BinaryMessenger messenger) {
55 channel = new MethodChannel(messenger, "flutter/keyboard", StandardMethodCodec.INSTANCE);
56 channel.setMethodCallHandler(parsingMethodHandler);
57 }
58
59 /**
60 * Sets the {@link KeyboardMethodHandler} which receives all requests to query the keyboard state.
61 */
62 public void setKeyboardMethodHandler(@Nullable KeyboardMethodHandler keyboardMethodHandler) {
63 this.keyboardMethodHandler = keyboardMethodHandler;
64 }
65
66 public interface KeyboardMethodHandler {
67 /**
68 * Returns the keyboard pressed states.
69 *
70 * @return A map whose keys are physical keyboard key IDs and values are the corresponding
71 * logical keyboard key IDs.
72 */
73 Map<Long, Long> getKeyboardState();
74 }
75}
final MethodChannel.MethodCallHandler parsingMethodHandler
void setKeyboardMethodHandler(@Nullable KeyboardMethodHandler keyboardMethodHandler)
static ::testing::Matcher< GBytes * > MethodCall(const std::string &name, ::testing::Matcher< FlValue * > args)
GAsyncResult * result