ui.root.setKeyHandler

Đặt trình xử lý sự kiện keydown cho bảng điều khiển gốc bằng một khoá không xác định trước. Trình xử lý chỉ được kích hoạt một lần khi người dùng nhấn lệnh khoá được liên kết. Cùng một khoá sẽ được liên kết với trình xử lý mới nhất được đặt cho khoá đó.

Cách sử dụngGiá trị hàng trả lại
ui.root.setKeyHandler(keyCode, handler, description)
Đối sốLoạiThông tin chi tiết
keyCodeList[ui.Key]|ui.KeyMã khoá hoặc một mảng mã khoá. Ví dụ: ui.Key.A hoặc [ui.Key.SHIFT, ui.Key.A].
handlerChức năngTrình xử lý cho lệnh khoá.
descriptionString, không bắt buộcĐoạn mô tả ngắn giải thích lệnh khoá này. Phần mô tả sẽ hiển thị trong Trình đơn lối tắt.

Ví dụ

Trình soạn thảo mã (JavaScript)

// Replace the default UI widgets with a few custom widgets.
// Print "Shift A" to the console when Shift+A is pressed.
ui.root.setKeyHandler(
  [ui.Key.SHIFT, ui.Key.A],
  function() {
    print('Shift A');
  },
  'A simple print'
);

// Create a solid black image.
var blackImage = ee.Image(1).visualize({palette: ['black']});

// Create a Layer object so we can easily manipulate its properties.
var blackLayer = ui.Map.Layer(blackImage, {}, 'Black Overlay', true);

// Add the layer to the Map.
Map.layers().add(blackLayer);

// Pressing the "b" key will toggle the layer on and off.
ui.root.setKeyHandler(ui.Key.B, function() {
  // Get the current visibility state.
  var isShown = blackLayer.getShown();

  // Set the visibility to the opposite of the current state.
  blackLayer.setShown(!isShown);

  // Print the status to the console.
  print('Black layer visible: ' + !isShown);
}, 'Toggle black layer');