我想用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難道我的回答解決了您的問題?如果沒有,請告訴我細節。 –