Tuesday, 15 March 2011

c# - Hook/detect windows language change even when app not focused -



c# - Hook/detect windows language change even when app not focused -

is there way observe if windows/os language changed when app not in focus? far able accomplish wanted if app focused using:

string language = ""; system.windows.input.inputlanguagemanager.current.inputlanguagechanged += new system.windows.input.inputlanguageeventhandler((sender, e) => { language = e.newlanguage.displayname; messagebox.show(language); });

but can understand, not want..

i thinking other solution such hooking keys alter language (for illustration alt+shift) wont able know language in utilize , user can alter default hotkey...

would appreciate help.

the problem facing related how wm_inputlangchange message works. message sent programs operating scheme in order inform them language changes. however, according documentation message sent "to topmost affected window". means can phone call native method getkeyboardlayout (it used inputlanguagemanager way) if application not active getkeyboardlayout homecoming lastly known, outdated, language.

taking business relationship might thought utilize solution pointed @vdohnal i.e. find current topmost window , read keyboard layout it. here quick proof of concept how within wpf application. used additional thread periodically finds topmost window , ready keyboard layout it. code far beingness perfect works , might help implement own solution.

public partial class mainwindow : window { [dllimport("user32.dll")] static extern intptr getkeyboardlayout(uint idthread); [dllimport("user32.dll")] private static extern intptr getforegroundwindow(); [dllimport("user32.dll")] static extern uint getwindowthreadprocessid(intptr hwnd, intptr processid); private cultureinfo _currentlanaguge; public mainwindow() { initializecomponent(); task.factory.startnew(() => { while (true) { handlecurrentlanguage(); thread.sleep(500); } }); } private static cultureinfo getcurrentculture() { var l = getkeyboardlayout(getwindowthreadprocessid(getforegroundwindow(), intptr.zero)); homecoming new cultureinfo((short)l.toint64()); } private void handlecurrentlanguage() { var currentculture = getcurrentculture(); if (_currentlanaguge == null || _currentlanaguge.lcid != currentculture.lcid) { _currentlanaguge = currentculture; messagebox.show(_currentlanaguge.name); } } }

c# wpf hook input-language

No comments:

Post a Comment