windows/runner/keyboard_mode.cpp
#include <windows.h> #include <iostream> extern "C" __declspec(dllexport) void SetKeyboardToEnglish() { // Load the English (United States) keyboard layout HKL hkl = LoadKeyboardLayout(TEXT("00000409"), KLF_ACTIVATE); // English (United States) layout if (hkl == NULL) { std::cerr << "Failed to load keyboard layout" << std::endl; return; } // Activate the layout if (!ActivateKeyboardLayout(hkl, KLF_SETFORPROCESS)) { std::cerr << "Failed to activate keyboard layout" << std::endl; } } extern "C" __declspec(dllexport) void SetKeyboardToHalfWidthAlphanumeric() { // Load the Japanese keyboard layout with half-width alphanumeric mode HKL hkl = LoadKeyboardLayout(TEXT("00000411"), KLF_ACTIVATE); // Japanese IME if (hkl == NULL) { std::cerr << "Failed to load keyboard layout" << std::endl; return; } // Activate the layout if (!ActivateKeyboardLayout(hkl, KLF_SETFORPROCESS)) { std::cerr << "Failed to activate keyboard layout" << std::endl; return; } // Simulate the Alt + 半角/全角 key press to switch to half-width alphanumeric INPUT inputs[4] = {}; // Press Alt inputs[0].type = INPUT_KEYBOARD; inputs[0].ki.wVk = VK_MENU; // Press 半角/全角 (VK_KANA) inputs[1].type = INPUT_KEYBOARD; inputs[1].ki.wVk = VK_KANA; // Release 半角/全角 (VK_KANA) inputs[2].type = INPUT_KEYBOARD; inputs[2].ki.wVk = VK_KANA; inputs[2].ki.dwFlags = KEYEVENTF_KEYUP; // Release Alt inputs[3].type = INPUT_KEYBOARD; inputs[3].ki.wVk = VK_MENU; inputs[3].ki.dwFlags = KEYEVENTF_KEYUP; // Send the key events if (SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT)) == 0) { std::cerr << "Failed to send input" << std::endl; } }
KeyboardMode
クラス
import 'dart:ffi'; import 'dart:io'; typedef SetKeyboardToEnglishFunc = Void Function(); typedef SetKeyboardToHalfWidthAlphanumericFunc = Void Function(); class KeyboardMode { final DynamicLibrary _nativeLib; KeyboardMode() : _nativeLib = Platform.isWindows ? DynamicLibrary.open('keyboard_mode.dll') : throw UnsupportedError('This platform is not supported') { _setKeyboardToEnglish = _nativeLib .lookup<NativeFunction<SetKeyboardToEnglishFunc>>('SetKeyboardToEnglish') .asFunction(); _setKeyboardToHalfWidthAlphanumeric = _nativeLib .lookup<NativeFunction<SetKeyboardToHalfWidthAlphanumericFunc>>('SetKeyboardToHalfWidthAlphanumeric') .asFunction(); } late final void Function() _setKeyboardToEnglish; late final void Function() _setKeyboardToHalfWidthAlphanumeric; void setKeyboardToEnglish() => _setKeyboardToEnglish(); void setKeyboardToHalfWidthAlphanumeric() => _setKeyboardToHalfWidthAlphanumeric(); }
windows/runner/CMakeLists.txt
"win32_window.cpp" "${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc" "Runner.rc" "runner.exe.manifest" ) # ここを追加 # Define the DLL set(PLUGIN_NAME "keyboard_mode") add_library(${PLUGIN_NAME} SHARED "keyboard_mode.cpp") # Define the libraries to link to. #target_link_libraries(runner PRIVATE flutter keyboard_hook) apply_standard_settings(${PLUGIN_NAME}) # Apply the standard set of build settings. This can be removed for applications # that need different build settings. apply_standard_settings(${BINARY_NAME}) # ここまで