C#으로 모든 키보드 레이아웃 변경을 감지하고 싶습니다. win + space 또는 alt + shift 또는 마우스로 전환했는지 여부는 중요하지 않습니다. 데스크톱 응용 프로그램에 대해 상당히 잘 작동하는 코드를 작성했습니다 (아래 참조). 하지만 그것은 UWP 애플 리케이션을 위해 작동하지 않습니다. UWP 앱에서 레이아웃을 전환하면 감지되지 않습니다. 데스크톱 앱으로 전환하면 변경 사항이 즉시 감지됩니다 ... 레이아웃의 변경 사항을 감지하려면 어떻게해야합니까? 어떤 창이 활성화되어 있어도 어떤 순간에 어떤 레이아웃이 활성화되어 있는지 알아내는 다른 방법이 있습니까? 내 코드 :C#을 사용하는 범용 Windows 응용 프로그램에서 키보드 레이아웃 변경을 감지하지 못했습니다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Globalization;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string lastLang = "";
while (true)
{
string langSuffix = GetKeyboardLayoutIdAtTime();
if (!langSuffix.Equals(lastLang))
{
// do something
Console.WriteLine(getCurrentTimeStamp() + ": Changing '" + lastLang + "' to '" + langSuffix + "'.");
lastLang = langSuffix;
}
System.Threading.Thread.Sleep(1000);
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern int GetWindowThreadProcessId(IntPtr handleWindow, out int lpdwProcessID);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetKeyboardLayout(int WindowsThreadProcessID);
public static string GetKeyboardLayoutIdAtTime()
{
IntPtr hWnd = GetForegroundWindow();
int lpdwProcessId;
InputLanguageCollection installedInputLanguages = InputLanguage.InstalledInputLanguages;
CultureInfo currentInputLanguage = null;
int WinThreadProcId = GetWindowThreadProcessId(hWnd, out lpdwProcessId);
IntPtr KeybLayout = GetKeyboardLayout(WinThreadProcId);
// this remain unchanged when I switch layouts in UWP
Console.WriteLine("KL IntPtr: " + KeybLayout);
for (int i = 0; i < installedInputLanguages.Count; i++)
{
if (KeybLayout == installedInputLanguages[i].Handle) currentInputLanguage = installedInputLanguages[i].Culture;
}
if(currentInputLanguage == null)
{
Console.WriteLine(getCurrentTimeStamp() + "current input language is null...");
}
return currentInputLanguage.TwoLetterISOLanguageName;
}
private static string getCurrentTimeStamp()
{
return DateTime.Now.ToString("yyyyMMddHHmmssffff");
}
}
} 데스크톱에서
@stove 내 대답은 문제를 해결 했습니까? 그렇지 않다면 세부 사항을 말해주십시오. –