ui.root.setKeyHandler

为具有非预定义键的根面板设置 keydown 事件处理脚本。当用户按下绑定的键命令时,处理程序仅触发一次。同一键将绑定到为其设置的最新处理程序。

用法返回
ui.root.setKeyHandler(keyCode, handler, description)
参数类型详细信息
keyCodeList[ui.Key]|ui.Key键码或键码数组。例如,ui.Key.A 或 [ui.Key.SHIFT, ui.Key.A]。
handler函数键命令的处理程序。
description字符串,可选用于说明相应键命令的简短说明。说明将显示在快捷方式菜单中。

示例

代码编辑器 (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');