napi_value _GetKeyMap()

in src/keyboard_win.cc [284:325]


napi_value _GetKeyMap(napi_env env, napi_callback_info info) {
  UseForegroundKeyboardLayoutScope use_foreground_keyboard_layout;

  napi_value result;
  NAPI_CALL(env, napi_create_object(env, &result));

  UINT clear_key_code = VK_DECIMAL;
  UINT clear_scan_code = ::MapVirtualKeyW(clear_key_code, MAPVK_VK_TO_VSC);
  BYTE keyboard_state[256];

  size_t cnt = sizeof(usb_keycode_map) / sizeof(usb_keycode_map[0]);
  for (size_t i = 0; i < cnt; ++i) {
    const char *code = usb_keycode_map[i].code;
    int native_scancode = usb_keycode_map[i].native_keycode;

    if (!code || native_scancode <= 0) {
      continue;
    }

    int native_keycode = ::MapVirtualKeyW(native_scancode, MAPVK_VSC_TO_VK);

    napi_value entry;
    NAPI_CALL(env, napi_create_object(env, &entry));

    NAPI_CALL(env, napi_set_named_property_string_utf8(env, entry, "vkey", _VKeyToStr(native_keycode)));

    std::string value = GetStrFromKeyPress(native_keycode, 0, keyboard_state, clear_key_code, clear_scan_code);
    NAPI_CALL(env, napi_set_named_property_string_utf8(env, entry, "value", value.c_str()));

    std::string withShift = GetStrFromKeyPress(native_keycode, kShiftKeyModifierMask, keyboard_state, clear_key_code, clear_scan_code);
    NAPI_CALL(env, napi_set_named_property_string_utf8(env, entry, "withShift", withShift.c_str()));

    std::string withAltGr = GetStrFromKeyPress(native_keycode, kControlKeyModifierMask | kAltKeyModifierMask, keyboard_state, clear_key_code, clear_scan_code);
    NAPI_CALL(env, napi_set_named_property_string_utf8(env, entry, "withAltGr", withAltGr.c_str()));

    std::string withShiftAltGr = GetStrFromKeyPress(native_keycode, kShiftKeyModifierMask | kControlKeyModifierMask | kAltKeyModifierMask, keyboard_state, clear_key_code, clear_scan_code);
    NAPI_CALL(env, napi_set_named_property_string_utf8(env, entry, "withShiftAltGr", withShiftAltGr.c_str()));

    NAPI_CALL(env, napi_set_named_property(env, result, code, entry));
  }
  return result;
}