I couldn't remember IME On / Off, so I decided to rely on the machine. Get the IME status in Python and tell Arduino via serial communication.
Python Use the additional modules below. Please install with pip etc.
python.py
from ctypes import *
from time import sleep
import win32gui
import win32con
import win32api
import serial
imm32 = WinDLL("imm32")
while True:
hWnd1 = win32gui.GetForegroundWindow()
hWnd2 = imm32.ImmGetDefaultIMEWnd(hWnd1)
#0x005 below is IMC_Indicates GET OPEN STATUS.
IMEStatus = win32api.SendMessage(hWnd2 , win32con.WM_IME_CONTROL, 0x005, 0)
#Change COM7 according to the serial port to connect to.
ser = serial.Serial('COM7',9600)
if IMEStatus == 0:
ser.write(b"b")
else:
ser.write(b"a")
ser.close()
sleep(0.05) #Seconds (=50ms)
Arduino It receives it serially and judges whether the LED is on or off.
arduino.ino
const int PinLED = 13;
int responseDelay = 10; //Milliseconds
char input_char;
pinMode(PinLED, OUTPUT);
Serial.begin(9600);
void loop() {
if (Serial.available() >0){
input_char = Serial.read();
if(input_char == 'a'){
digitalWrite(PinLED, HIGH);
}else{
digitalWrite(PinLED, LOW);
}
delay(responseDelay);
}
Recommended Posts