Running scripts only when a specific language is active

Get help with using AutoHotkey and its commands and hotkeys
EarlMorton
Posts: 4
Joined: 18 Sep 2020, 17:47

Running scripts only when a specific language is active

18 Sep 2020, 18:02

I use two languages on my computer (US English and Polytonic Greek). I have some Autohotkey scripts that I want to run only when Greek is active. Is there a way that a script can detect the current Windows language and either continue to run or exit immediately, depending on which language is active? I tried using the A_Language built-in variable, but it always returns English ("0409"). As far as I can tell, Windows does not indicate the current language in an environment variable, but I could be wrong about that. It's been a few years since I've tinkered with environment variables, and I'm not sure I'm finding all of them. I am using Autohotkey version 1.1.32.00, and Windows 10.

Thanks!
User avatar
Jim Dunn
Posts: 224
Joined: 01 Sep 2020, 20:21
Location: NSW

Re: Running scripts only when a specific language is active

18 Sep 2020, 19:33

Perhaps A_Language just returns the default for the machine.

On my system

Code: Select all

RegRead, CurLocaleName, % "HKEY_CURRENT_USER\Control Panel\International", % "LocaleName"
MsgBox % CurLocaleName
returns "en-AU" for Australian English, which is correct in my case.

Since it's at "HKEY_CURRENT_USER" one would hope it is current user specific, but I guess you'd need to try it out on the machine(s) in question to find out for sure.
User avatar
mikeyww
Posts: 1113
Joined: 09 Sep 2014, 18:38

Re: Running scripts only when a specific language is active

18 Sep 2020, 19:45

Code: Select all

MsgBox, 64, Language, % getLang()
ExitApp

getLang() {
 lang := {0436: "af;Afrikaans"
  , 041C: "sq;Albanian"
  , 0001: "ar;Arabic"
  , 0401: "ar-sa;Arabic (Saudi Arabia)"
  , 0801: "ar-iq;Arabic (Iraq)"
  , 0C01: "ar-eg;Arabic (Egypt)"
  , 1001: "ar-ly;Arabic (Libya)"
  , 1401: "ar-dz;Arabic (Algeria)"
  , 1801: "ar-ma;Arabic (Morocco)"
  , 1C01: "ar-tn;Arabic (Tunisia)"
  , 2001: "ar-om;Arabic (Oman)"
  , 2401: "ar-ye;Arabic (Yemen)"
  , 2801: "ar-sy;Arabic (Syria)"
  , 2C01: "ar-jo;Arabic (Jordan)"
  , 3001: "ar-lb;Arabic (Lebanon)"
  , 3401: "ar-kw;Arabic (Kuwait)"
  , 3801: "ar-ae;Arabic (you.A.E.)"
  , 3C01: "ar-bh;Arabic (Bahrain)"
  , 4001: "ar-qa;Arabic (Qatar)"
  , 042D: "eu;Basque"
  , 0402: "bg;Bulgarian"
  , 0423: "be;Belarusian"
  , 0403: "ca;Catalan"
  , 0004: "zh;Chinese"
  , 0404: "zh-tw;Chinese (Taiwan)"
  , 0804: "zh-cn;Chinese (China)"
  , 0C04: "zh-hk;Chinese (Hong Kong SAR)"
  , 1004: "zh-sg;Chinese (Singapore)"
  , 041A: "hr;Croatian"
  , 0405: "cs;Czech"
  , 0406: "the;Danish"
  , 0413: "nl;Dutch (Netherlands)"
  , 0813: "nl-be;Dutch (Belgium)"
  , 0009: "en;English"
  , 0409: "en-us;English (United States)"
  , 0809: "en-gb;English (United Kingdom)"
  , 0C09: "en-au;English (Australia)"
  , 1009: "en-ca;English (Canada)"
  , 1409: "en-nz;English (New Zealand)"
  , 1809: "en-ie;English (Ireland)"
  , 1C09: "en-za;English (South Africa)"
  , 2009: "en-jm;English (Jamaica)"
  , 2809: "en-bz;English (Belize)"
  , 2C09: "en-tt;English (Trinidad)"
  , 0425: "et;Estonian"
  , 0438: "fo;Faeroese"
  , 0429: "fa;Farsi"
  , 040B: "fi;Finnish"
  , 040C: "fr;French (France)"
  , 080C: "fr-be;French (Belgium)"
  , 0C0C: "fr-ca;French (Canada)"
  , 100C: "fr-ch;French (Switzerland)"
  , 140C: "fr-lu;French (Luxembourg)"
  , 043C: "gd;Gaelic"
  , 0407: "de;German (Germany)"
  , 0807: "de-ch;German (Switzerland)"
  , 0C07: "de-at;German (Austria)"
  , 1007: "de-lu;German (Luxembourg)"
  , 1407: "de-li;German (Liechtenstein)"
  , 0408: "el;Greek"
  , 040D: "he;Hebrew"
  , 0439: "hi;Hindi"
  , 040E: "hu;Hungarian"
  , 040F: "is;Icelandic"
  , 0421: "in;Indonesian"
  , 0410: "it;Italian (Italy)"
  , 0810: "it-ch;Italian (Switzerland)"
  , 0411: "ja;Japanese"
  , 0412: "ko;Korean"
  , 0426: "lv;Latvian"
  , 0427: "lt;Lithuanian"
  , 042F: "mk;FYRO Macedonian"
  , 043E: "ms;Malay (Malaysia)"
  , 043A: "mt;Maltese"
  , 0414: "no;Norwegian (Bokmal)"
  , 0814: "no;Norwegian (Nynorsk)"
  , 0415: "pl;Polish"
  , 0416: "pt-br;Portuguese (Brazil)"
  , 0816: "pt;Portuguese (Portugal)"
  , 0417: "rm;Rhaeto-Romanic"
  , 0418: "ro;Romanian"
  , 0818: "ro-mo;Romanian (Moldova)"
  , 0419: "ru;Russian"
  , 0819: "ru-mo;Russian (Moldova)"
  , 0C1A: "sr;Serbian (Cyrillic)"
  , 081A: "sr;Serbian (Latin)"
  , 041B: "sk;Slovak"
  , 0424: "sl;Slovenian"
  , 042E: "sb;Sorbian"
  , 040A: "es;Spanish (Traditional Sort)"
  , 080A: "es-mx;Spanish (Mexico)"
  , 0C0A: "es;Spanish (International Sort)"
  , 100A: "es-gt;Spanish (Guatemala)"
  , 140A: "es-cr;Spanish (Costa Rica)"
  , 180A: "es-pa;Spanish (Panama)"
  , 1C0A: "es-do;Spanish (Dominican Republic)"
  , 200A: "es-ve;Spanish (Venezuela)"
  , 240A: "es-co;Spanish (Colombia)"
  , 280A: "es-pe;Spanish (Peru)"
  , 2C0A: "es-ar;Spanish (Argentina)"
  , 300A: "es-ec;Spanish (Ecuador)"
  , 340A: "es-cl;Spanish (Chile)"
  , 380A: "es-uy;Spanish (Uruguay)"
  , 3C0A: "es-py;Spanish (Paraguay)"
  , 400A: "es-bo;Spanish (Bolivia)"
  , 440A: "es-sv;Spanish (El Salvador)"
  , 480A: "es-hn;Spanish (Honduras)"
  , 4C0A: "es-ni;Spanish (Nicaragua)"
  , 500A: "es-pr;Spanish (Puerto Rico)"
  , 0430: "sx;Sutu"
  , 041D: "sv;Swedish"
  , 081D: "sv-fi;Swedish (Finland)"
  , 041E: "th;Thai"
  , 0431: "ts;Tsonga"
  , 0432: "tn;Tswana"
  , 041F: "tr;Turkish"
  , 0422: "uk;Ukrainian"
  , 0420: "your;Urdu"
  , 042A: "vi;Vietnamese"
  , 0434: "xh;Xhosa"
  , 043D: "ji;Yiddish"
  , 0435: "zu;Zulu"}
 For objItem in ComObjGet("winmgmts:").ExecQuery("SELECT * FROM Win32_OperatingSystem")
  Return lang[objItem.Locale]
}
User avatar
Jim Dunn
Posts: 224
Joined: 01 Sep 2020, 20:21
Location: NSW

Re: Running scripts only when a specific language is active

18 Sep 2020, 19:55

:thumbup: Very good. That should always work, I'd hope, even if the user has changed during a session from what's saved in the registry.
EarlMorton
Posts: 4
Joined: 18 Sep 2020, 17:47

Re: Running scripts only when a specific language is active

19 Sep 2020, 01:12

Thank you, but on my machine this returns English even when I am set to use Greek. This seems to be picking up the default language (just like A_Language), rather than the current language.

Perhaps I asked the question incorrectly. To be more specific, I have Windows configured to enable typing in two languages. This is configured in Settings > Time and Language > Language > Preferred languages. I have the two languages listed there, and "English (United States)" is set as the "Default app language; Default input language" and "Windows display language." Under "Ελληνικά" (Greek), it says only "Language pack installed." Both languages also have icons that indicate options that are configured for each: "Display language installed" and "Typing installed." (English also has three additional option icons, but they are not relevant here.)

On this settings page, when I click on "Ελληνικα," an options button is shown, and one of the options is to "Add a keyboard." I have added the "Greek Polytonic" keyboard. So while I am typing in any app, I can press Windows+Spacebar to toggle between the two language keyboards. That is how I typed the words in Greek in this post. So maybe I am not toggling the "language," but rather I'm toggling the "keyboard."

I don't find anything in the AutoHotKey Help about detecting the keyboard in use. Windows knows which is active at any time, obviously, so it seems like an application such as AutoHotKey should be able to query Windows to find that out. I don't know where Windows keeps that bit of information; I thought it might be in an environment variable, but I guess not. The Registry seems likely, but I don't know much about it, and certainly not enough to find out where it might be in there, although I do see that AutoHotKey has functions to read values from the Registry.
User avatar
mikeyww
Posts: 1113
Joined: 09 Sep 2014, 18:38

Re: Running scripts only when a specific language is active

19 Sep 2020, 07:31

That looks quite tricky. I believe that you want the active input locale identifier. The trick is that it is specific to your context such as, seemingly, the specific process that you are using. Thus, although the following script can identify the "culture", it appears that when this is run, you will still not capture the input locale for the window of your interest.

Code: Select all

MsgBox, 64, Input locale, % getInputLocale()
ExitApp

getInputLocale() {
 out = %TEMP%\inputLocale.txt
 FileRecycle, %out%
 RunWait, %ComSpec% /c "powershell get-culture >%out%",, Hide
 FileReadLine, text, %out%, 4
 FileRecycle, %out%
 Return RegExReplace(text, ".+?\s+.+?\s+(.+)", "$1")
}
The following Windows batch script suffers from the same problem.

Code: Select all

@echo off
SET out=%TEMP%\inputLocale.txt
SETLOCAL EnableDelayedExpansion
FOR /F "tokens=1* delims=:" %%G IN ('%WINDIR%\System32\systeminfo.exe') DO (
 IF "%%G"=="Input Locale" FOR /F "tokens=*" %%A IN ("%%H") DO echo %%A>%out%
)
START "View" %out%
EXIT
BoBo
Posts: 3764
Joined: 13 May 2014, 17:15

Re: Running scripts only when a specific language is active

19 Sep 2020, 08:00

https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getkeyboardlayout HTH

@mikeyww - Your above lang := {0436: "af;Afrikaans" ...}-array, isn't that extractable from the system, so hasn't to be hardcoded (guessing)? :think:
User avatar
mikeyww
Posts: 1113
Joined: 09 Sep 2014, 18:38

Re: Running scripts only when a specific language is active

19 Sep 2020, 08:02

I'm not sure that solves the issue at hand, unless you have a working AHK script based on this.
EarlMorton
Posts: 4
Joined: 18 Sep 2020, 17:47

Re: Running scripts only when a specific language is active

19 Sep 2020, 11:03

I think I'm getting in over my head. Although I've dabbled in simple programming for many years, I'm not a developer nor an IT professional. (I'm a retired technical writer.) Mikeyww, I tested both of your latest code examples, and you are correct: they both return English regardless of which keyboard is active. I don't quite understand that. When the Greek keyboard is active, I can switch to any application in any window, and continue to type in Greek. (I do that often unintentionally!) So the active keyboard seems to be a system-wide setting, rather than a local one.

I did run across this document (https://docs.microsoft.com/en-us/uwp/api/windows.globalization.language.currentinputmethodlanguagetag?view=winrt-19041), and it seems like this function might do what I want, but I don't know how to implement it in an AHK script. It also says that it applies to "WinRT," and I'm not sure if that is relevant to Windows 10, which I am using.

My AHK script assigns some macros to the grave/tilde key to make it easier to type accents and other diacritical marks used in polytonic Greek. The keystrokes for typing such characters in the standard Windows Polytonic Greek keyboard layout are quite cumbersome by comparison. This much works fine for me. The problem is that when I want to actually type a grave accent or a tilde in an English context (which I've been doing quite a bit over the past week or so), I have to go the Character Map application to get those characters. So I was hoping that my AHK script could easily determine if I'm typing in English or Greek and either run the macros or return the default characters for that key accordingly. That seems to be much more complicated than I thought it would be. I'm not sure that continuing along these lines is worth the effort.

Thank you very much for your time and suggestions!
User avatar
mikeyww
Posts: 1113
Joined: 09 Sep 2014, 18:38

Re: Running scripts only when a specific language is active

19 Sep 2020, 12:19

Interesting. My KB changes by window or program (I'm not sure which).

Here's more info, but I'm not sure how to translate it into AHK.
User avatar
mikeyww
Posts: 1113
Joined: 09 Sep 2014, 18:38

Re: Running scripts only when a specific language is active

19 Sep 2020, 13:06

This worked.

Code: Select all

; Get active keyboard layout
; @mikeyww on 19 September 2020
; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=81204
; https://www.autohotkey.com/boards/viewtopic.php?style=1&t=64519

#If langNameThis()="English_United_States"
F4::MsgBox, 64, English, I'm English!
#If

#If langNameThis()="Greek"
F4::MsgBox, 64, Greek, I'm Greek!
#If

langNameThis() {
 WinGet, thisWindow,, A
 Return langName(thisWindow)
}

langName(window) {
 lang := {0436: "Afrikaans"
  , 041c: "Albanian"
  , 0401: "Arabic_Saudi_Arabia"
  , 0801: "Arabic_Iraq"
  , 0c01: "Arabic_Egypt"
  , 1001: "Arabic_Libya"
  , 1401: "Arabic_Algeria"
  , 1801: "Arabic_Morocco"
  , 1c01: "Arabic_Tunisia"
  , 2001: "Arabic_Oman"
  , 2401: "Arabic_Yemen"
  , 2801: "Arabic_Syria"
  , 2c01: "Arabic_Jordan"
  , 3001: "Arabic_Lebanon"
  , 3401: "Arabic_Kuwait"
  , 3801: "Arabic_UAE"
  , 3c01: "Arabic_Bahrain"
  , 4001: "Arabic_Qatar"
  , 042b: "Armenian"
  , 042c: "Azeri_Latin"
  , 082c: "Azeri_Cyrillic"
  , 042d: "Basque"
  , 0423: "Belarusian"
  , 0402: "Bulgarian"
  , 0403: "Catalan"
  , 0404: "Chinese_Taiwan"
  , 0804: "Chinese_PRC"
  , 0c04: "Chinese_Hong_Kong"
  , 1004: "Chinese_Singapore"
  , 1404: "Chinese_Macau"
  , 041a: "Croatian"
  , 0405: "Czech"
  , 0406: "Danish"
  , 0413: "Dutch_Standard"
  , 0813: "Dutch_Belgian"
  , 0409: "English_United_States"
  , 0809: "English_United_Kingdom"
  , 0c09: "English_Australian"
  , 1009: "English_Canadian"
  , 1409: "English_New_Zealand"
  , 1809: "English_Irish"
  , 1c09: "English_South_Africa"
  , 2009: "English_Jamaica"
  , 2409: "English_Caribbean"
  , 2809: "English_Belize"
  , 2c09: "English_Trinidad"
  , 3009: "English_Zimbabwe"
  , 3409: "English_Philippines"
  , 0425: "Estonian"
  , 0438: "Faeroese"
  , 0429: "Farsi"
  , 040b: "Finnish"
  , 040c: "French_Standard"
  , 080c: "French_Belgian"
  , 0c0c: "French_Canadian"
  , 100c: "French_Swiss"
  , 140c: "French_Luxembourg"
  , 180c: "French_Monaco"
  , 0437: "Georgian"
  , 0407: "German_Standard"
  , 0807: "German_Swiss"
  , 0c07: "German_Austrian"
  , 1007: "German_Luxembourg"
  , 1407: "German_Liechtenstein"
  , 0408: "Greek"
  , 040d: "Hebrew"
  , 0439: "Hindi"
  , 040e: "Hungarian"
  , 040f: "Icelandic"
  , 0421: "Indonesian"
  , 0410: "Italian_Standard"
  , 0810: "Italian_Swiss"
  , 0411: "Japanese"
  , 043f: "Kazakh"
  , 0457: "Konkani"
  , 0412: "Korean"
  , 0426: "Latvian"
  , 0427: "Lithuanian"
  , 042f: "Macedonian"
  , 043e: "Malay_Malaysia"
  , 083e: "Malay_Brunei_Darussalam"
  , 044e: "Marathi"
  , 0414: "Norwegian_Bokmal"
  , 0814: "Norwegian_Nynorsk"
  , 0415: "Polish"
  , 0416: "Portuguese_Brazilian"
  , 0816: "Portuguese_Standard"
  , 0418: "Romanian"
  , 0419: "Russian"
  , 044f: "Sanskrit"
  , 081a: "Serbian_Latin"
  , 0c1a: "Serbian_Cyrillic"
  , 041b: "Slovak"
  , 0424: "Slovenian"
  , 040a: "Spanish_Traditional_Sort"
  , 080a: "Spanish_Mexican"
  , 0c0a: "Spanish_Modern_Sort"
  , 100a: "Spanish_Guatemala"
  , 140a: "Spanish_Costa_Rica"
  , 180a: "Spanish_Panama"
  , 1c0a: "Spanish_Dominican_Republic"
  , 200a: "Spanish_Venezuela"
  , 240a: "Spanish_Colombia"
  , 280a: "Spanish_Peru"
  , 2c0a: "Spanish_Argentina"
  , 300a: "Spanish_Ecuador"
  , 340a: "Spanish_Chile"
  , 380a: "Spanish_Uruguay"
  , 3c0a: "Spanish_Paraguay"
  , 400a: "Spanish_Bolivia"
  , 440a: "Spanish_El_Salvador"
  , 480a: "Spanish_Honduras"
  , 4c0a: "Spanish_Nicaragua"
  , 500a: "Spanish_Puerto_Rico"
  , 0441: "Swahili"
  , 041d: "Swedish"
  , 081d: "Swedish_Finland"
  , 0449: "Tamil"
  , 0444: "Tatar"
  , 041e: "Thai"
  , 041f: "Turkish"
  , 0422: "Ukrainian"
  , 0420: "Urdu"
  , 0443: "Uzbek_Latin"
  , 0843: "Uzbek_Cyrillic"
  , 042a: "Vietnamese"}
 Return lang[SubStr(Format("{:x}"
  , DllCall("GetKeyboardLayout", "UInt", DllCall("GetWindowThreadProcessId", "UInt", window, "UInt", 0), "UInt")), -3)]
}
garry
Posts: 2433
Joined: 22 Dec 2013, 12:50

Re: Running scripts only when a specific language is active

19 Sep 2020, 14:08

@mikeyww works fine
I used this ( in your earlier script )

Code: Select all

WinGet, thisWindow,, A
MsgBox, 64, Keyboard layout, % langName(thisWindow)
ExitApp
langName(window) {
 lang := {0436: "Afrikaans"
  , 041c: "Albanian"
  , 0401: "Arabic_Saudi_Arabia"
;.......

in WINDOWS-10 : use key WIN+SPACE to select keyboard language

Code: Select all

;-  in WINDOWS-10 : use key WIN+SPACE to select keyboard language ( WIN and then select with SPACE )
;-  ----------------------------------------------------------------------
;-  if desired language not exist can be set with "SetDefaultKeyboard"  and then select with WIN+SPACE
;=========================================================================
SetDefaultKeyboard(0x0408) ; greek
;- 0807 ch-de
;- 0419 ru 
;- 0409 en-us

SetDefaultKeyboard(LocaleID){
	Global
	SPI_SETDEFAULTINPUTLANG := 0x005A
	SPIF_SENDWININICHANGE := 2
	Lan := DllCall("LoadKeyboardLayout", "Str", Format("{:08x}", LocaleID), "Int", 0)
	VarSetCapacity(Lan%LocaleID%, 4, 0)
	NumPut(LocaleID, Lan%LocaleID%)
	DllCall("SystemParametersInfo", "UInt", SPI_SETDEFAULTINPUTLANG, "UInt", 0, "UPtr", &Lan%LocaleID%, "UInt", SPIF_SENDWININICHANGE)
	WinGet, windows, List
	Loop %windows% {
		PostMessage 0x50, 0, %Lan%, , % "ahk_id " windows%A_Index%
	}
}
;========================================================================
Last edited by garry on 19 Sep 2020, 14:16, edited 1 time in total.
User avatar
mikeyww
Posts: 1113
Joined: 09 Sep 2014, 18:38

Re: Running scripts only when a specific language is active

19 Sep 2020, 14:11

I don't think the problem was setting the keyboard; it was getting it.
garry
Posts: 2433
Joined: 22 Dec 2013, 12:50

Re: Running scripts only when a specific language is active

19 Sep 2020, 14:20

yes, sorry, EarlMorton wrote already :
I can press Windows+Spacebar to toggle between the two language keyboards.
EDIT : with script above can add other keyboardlanguage and then select with WIN+SPACE
Last edited by garry on 20 Sep 2020, 02:04, edited 1 time in total.
EarlMorton
Posts: 4
Joined: 18 Sep 2020, 17:47

Re: Running scripts only when a specific language is active

19 Sep 2020, 16:07

Yes, mikeyww, this does work! I'm not sure how, but it does, and I'm happy. I modified it to fit in my existing macros. Thank you so much for your time and effort!
teadrinker
Posts: 1865
Joined: 29 Mar 2015, 09:41
Contact:

Re: Running scripts only when a specific language is active

20 Sep 2020, 11:24

If you don't need to get the language of the console window (cmd.exe), you can do it like this:

Code: Select all

F11:: MsgBox % GetInputLangName( GetInputLangID(WinExist("A")) )
 
GetInputLangID(hWnd) {
   threadId := DllCall("GetWindowThreadProcessId", "Ptr", hWnd, "UInt", 0)
   lyt := DllCall("GetKeyboardLayout", "Ptr", threadId, "UInt")
   return langID := Format("{:#x}", lyt & 0x3FFF)
}
 
GetInputLangName(langId)  {
   static LOCALE_SENGLANGUAGE := 0x1001
   charCount := DllCall("GetLocaleInfo", "UInt", langId, "UInt", LOCALE_SENGLANGUAGE, "UInt", 0, "UInt", 0)
   VarSetCapacity(localeSig, size := charCount << !!A_IsUnicode, 0)
   DllCall("GetLocaleInfo", "UInt", langId, "UInt", LOCALE_SENGLANGUAGE, "Str", localeSig, "UInt", size)
   return localeSig
}
malcev
Posts: 718
Joined: 12 Aug 2014, 12:37

Re: Running scripts only when a specific language is active

20 Sep 2020, 11:44

Your code is not right - (focused controls + controls running from ApplicationFrameHost.exe).
You can do it like this:

Code: Select all

F11::MsgBox, % GetInputLangName(GetLayout())

GetLayout()
{
   static uia
   if !uia
      uia := UIA_Interface()
   DetectHiddenWindows, on
   GetFocusedElement := uia.GetFocusedElement()
   pid := GetFocusedElement.CurrentProcessId
   ControlGetFocus, FocusedControl, ahk_pid %pid%
   ControlGet, Hwnd, Hwnd,, %FocusedControl%, ahk_pid %pid%
   ThreadID := DllCall("GetWindowThreadProcessId", "Ptr", hWnd, "UInt", 0)
   InputLocaleID := DllCall("GetKeyboardLayout", "Ptr", ThreadID, "UInt")
   return langID := Format("{:#x}", InputLocaleID & 0x3FFF)
}


GetInputLangName(langId)  {
   static LOCALE_SENGLANGUAGE := 0x1001
   charCount := DllCall("GetLocaleInfo", "UInt", langId, "UInt", LOCALE_SENGLANGUAGE, "UInt", 0, "UInt", 0)
   VarSetCapacity(localeSig, size := charCount << !!A_IsUnicode, 0)
   DllCall("GetLocaleInfo", "UInt", langId, "UInt", LOCALE_SENGLANGUAGE, "Str", localeSig, "UInt", size)
   return localeSig
}


;~ UI Automation Constants: http://msdn.microsoft.com/en-us/library/windows/desktop/ee671207(v=vs.85).aspx
;~ UI Automation Enumerations: http://msdn.microsoft.com/en-us/library/windows/desktop/ee671210(v=vs.85).aspx
;~ http://www.autohotkey.com/board/topic/94619-ahk-l-screen-reader-a-tool-to-get-text-anywhere/

/* Questions:
	- better way to do __properties?
	- support for Constants?
	- if method returns a SafeArray, should we return a Wrapped SafeArray, Raw SafeArray, or AHK Array
	- on UIA Interface conversion methods, how should the data be returned? wrapped/extracted or raw? should raw data be a ByRef param?
	- do variants need cleared? what about SysAllocString BSTRs?
	- do RECT struts need destroyed?
	- if returning wrapped data & raw is ByRef, will the wrapped data being released destroy the raw data?
	- returning varaint data other than vt=3|8|9|13|0x2000
	- Cached Members?
	- UIA Element existance - dependent on window being visible (non minimized)?
	- function(params, ByRef out="……")
*/

class UIA_Base {
	__New(p="", flag=1) {
		ObjInsert(this,"__Type","IUIAutomation" SubStr(this.__Class,5))
		,ObjInsert(this,"__Value",p)
		,ObjInsert(this,"__Flag",flag)
	}
	__Get(member) {
		if member not in base,__UIA ; base & __UIA should act as normal
		{	if raw:=SubStr(member,0)="*" ; return raw data - user should know what they are doing
				member:=SubStr(member,1,-1)
			if RegExMatch(this.__properties, "im)^" member ",(\d+),(\w+)", m) { ; if the member is in the properties. if not - give error message
				if (m2="VARIANT")	; return VARIANT data - DllCall output param different
					return UIA_Hr(DllCall(this.__Vt(m1), "ptr",this.__Value, "ptr",UIA_Variant(out)))? (raw?out:UIA_VariantData(out)):
				else if (m2="RECT") ; return RECT struct - DllCall output param different
					return UIA_Hr(DllCall(this.__Vt(m1), "ptr",this.__Value, "ptr",&(rect,VarSetCapacity(rect,16))))? (raw?out:UIA_RectToObject(rect)):
				else if UIA_Hr(DllCall(this.__Vt(m1), "ptr",this.__Value, "ptr*",out))
					return raw?out:m2="BSTR"?StrGet(out):RegExMatch(m2,"i)IUIAutomation\K\w+",n)?new UIA_%n%(out):out ; Bool, int, DWORD, HWND, CONTROLTYPEID, OrientationType?
			}
			else throw Exception("Property not supported by the " this.__Class " Class.",-1,member)
		}
	}
	__Set(member) {
		throw Exception("Assigning values not supported by the " this.__Class " Class.",-1,member)
	}
	__Call(member) {
		if !ObjHasKey(UIA_Base,member)&&!ObjHasKey(this,member)
			throw Exception("Method Call not supported by the " this.__Class " Class.",-1,member)
	}
	__Delete() {
		this.__Flag? ObjRelease(this.__Value):
	}
	__Vt(n) {
		return NumGet(NumGet(this.__Value+0,"ptr")+n*A_PtrSize,"ptr")
	}
}	

class UIA_Interface extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee671406(v=vs.85).aspx
	static __IID := "{30cbe57d-d9d0-452a-ab13-7ac5ac4825ee}"
		,  __properties := "ControlViewWalker,14,IUIAutomationTreeWalker`r`nContentViewWalker,15,IUIAutomationTreeWalker`r`nRawViewWalker,16,IUIAutomationTreeWalker`r`nRawViewCondition,17,IUIAutomationCondition`r`nControlViewCondition,18,IUIAutomationCondition`r`nContentViewCondition,19,IUIAutomationCondition`r`nProxyFactoryMapping,48,IUIAutomationProxyFactoryMapping`r`nReservedNotSupportedValue,54,IUnknown`r`nReservedMixedAttributeValue,55,IUnknown"
	
	CompareElements(e1,e2) {
		return UIA_Hr(DllCall(this.__Vt(3), "ptr",this.__Value, "ptr",e1.__Value, "ptr",e2.__Value, "int*",out))? out:
	}
	CompareRuntimeIds(r1,r2) {
		return UIA_Hr(DllCall(this.__Vt(4), "ptr",this.__Value, "ptr",ComObjValue(r1), "ptr",ComObjValue(r2), "int*",out))? out:
	}
	GetRootElement() {
		return UIA_Hr(DllCall(this.__Vt(5), "ptr",this.__Value, "ptr*",out))? new UIA_Element(out):
	}
	ElementFromHandle(hwnd) {
		return UIA_Hr(DllCall(this.__Vt(6), "ptr",this.__Value, "ptr",hwnd, "ptr*",out))? new UIA_Element(out):
	}
	ElementFromPoint(x="", y="") {
		return UIA_Hr(DllCall(this.__Vt(7), "ptr",this.__Value, "int64",x==""||y==""?0*DllCall("GetCursorPos","Int64*",pt)+pt:x&0xFFFFFFFF|y<<32, "ptr*",out))? new UIA_Element(out):
	}	
	GetFocusedElement() {
		return UIA_Hr(DllCall(this.__Vt(8), "ptr",this.__Value, "ptr*",out))? new UIA_Element(out):
	}
	;~ GetRootElementBuildCache 	9
	;~ ElementFromHandleBuildCache 	10
	;~ ElementFromPointBuildCache 	11
	;~ GetFocusedElementBuildCache 	12
	CreateTreeWalker(condition) {
		return UIA_Hr(DllCall(this.__Vt(13), "ptr",this.__Value, "ptr",Condition.__Value, "ptr*",out))? new UIA_TreeWalker(out):
	}
	;~ CreateCacheRequest 	20

	CreateTrueCondition() {
		return UIA_Hr(DllCall(this.__Vt(21), "ptr",this.__Value, "ptr*",out))? new UIA_Condition(out):
	}
	CreateFalseCondition() {
		return UIA_Hr(DllCall(this.__Vt(22), "ptr",this.__Value, "ptr*",out))? new UIA_Condition(out):
	}
	CreatePropertyCondition(propertyId, ByRef var, type="Variant") {
		if (type!="Variant")
			UIA_Variant(var,type,var)
		return UIA_Hr(DllCall(this.__Vt(23), "ptr",this.__Value, "int",propertyId, "ptr",&var, "ptr*",out))? new UIA_PropertyCondition(out):
	}
	CreatePropertyConditionEx(propertyId, ByRef var, type="Variant", flags=0x1) { ; NOT TESTED
	; PropertyConditionFlags_IgnoreCase = 0x1
		if (type!="Variant")
			UIA_Variant(var,type,var)
		return UIA_Hr(DllCall(this.__Vt(24), "ptr",this.__Value, "int",propertyId, "ptr",&var, "uint",flags, "ptr*",out))? new UIA_PropertyCondition(out):
	}
	CreateAndCondition(c1,c2) {
		return UIA_Hr(DllCall(this.__Vt(25), "ptr",this.__Value, "ptr",c1.__Value, "ptr",c2.__Value, "ptr*",out))? new UIA_AndCondition(out):
	}
	CreateAndConditionFromArray(array) { ; ComObj(0x2003)??
	;->in: AHK Array or Wrapped SafeArray
		if ComObjValue(array)&0x2000
			SafeArray:=array
		else {
			SafeArray:=ComObj(0x2003,DllCall("oleaut32\SafeArrayCreateVector", "uint",13, "uint",0, "uint",array.MaxIndex()),1)
			for i,c in array
				SafeArray[A_Index-1]:=c.__Value, ObjAddRef(c.__Value) ; AddRef - SafeArrayDestroy will release UIA_Conditions - they also release themselves
		}
		return UIA_Hr(DllCall(this.__Vt(26), "ptr",this.__Value, "ptr",ComObjValue(SafeArray), "ptr*",out))? new UIA_AndCondition(out):
	}
	CreateAndConditionFromNativeArray(p*) { ; Not Implemented
		return UIA_NotImplemented()
	/*	[in]           IUIAutomationCondition **conditions,
		[in]           int conditionCount,
		[out, retval]  IUIAutomationCondition **newCondition
	*/
		;~ return UIA_Hr(DllCall(this.__Vt(27), "ptr",this.__Value,
	}
	CreateOrCondition(c1,c2) {
		return UIA_Hr(DllCall(this.__Vt(28), "ptr",this.__Value, "ptr",c1.__Value, "ptr",c2.__Value, "ptr*",out))? new UIA_OrCondition(out):
	}
	CreateOrConditionFromArray(array) {
	;->in: AHK Array or Wrapped SafeArray
		if ComObjValue(array)&0x2000
			SafeArray:=array
		else {
			SafeArray:=ComObj(0x2003,DllCall("oleaut32\SafeArrayCreateVector", "uint",13, "uint",0, "uint",array.MaxIndex()),1)
			for i,c in array
				SafeArray[A_Index-1]:=c.__Value, ObjAddRef(c.__Value) ; AddRef - SafeArrayDestroy will release UIA_Conditions - they also release themselves
		}
		return UIA_Hr(DllCall(this.__Vt(29), "ptr",this.__Value, "ptr",ComObjValue(SafeArray), "ptr*",out))? new UIA_AndCondition(out):
	}
	CreateOrConditionFromNativeArray(p*) { ; Not Implemented
		return UIA_NotImplemented()
	/*	[in]           IUIAutomationCondition **conditions,
		[in]           int conditionCount,
		[out, retval]  IUIAutomationCondition **newCondition
	*/
		;~ return UIA_Hr(DllCall(this.__Vt(27), "ptr",this.__Value,
	}
	CreateNotCondition(c) {
		return UIA_Hr(DllCall(this.__Vt(31), "ptr",this.__Value, "ptr",c.__Value, "ptr*",out))? new UIA_NotCondition(out):
	}

	;~ AddAutomationEventHandler 	32
	;~ RemoveAutomationEventHandler 	33
	;~ AddPropertyChangedEventHandlerNativeArray 	34
	AddPropertyChangedEventHandler(element,scope=0x1,cacheRequest=0,handler="",propertyArray="") {
		SafeArray:=ComObjArray(0x3,propertyArray.MaxIndex())
		for i,propertyId in propertyArray
			SafeArray[i-1]:=propertyId
		return UIA_Hr(DllCall(this.__Vt(35), "ptr",this.__Value, "ptr",element.__Value, "int",scope, "ptr",cacheRequest,"ptr",handler.__Value,"ptr",ComObjValue(SafeArray)))
	}
	;~ RemovePropertyChangedEventHandler 	36
	;~ AddStructureChangedEventHandler 	37
	;~ RemoveStructureChangedEventHandler 	38
	AddFocusChangedEventHandler(cacheRequest, handler) {
		return UIA_Hr(DllCall(this.__Vt(39), "ptr",this.__Value, "ptr",cacheRequest, "ptr",handler.__Value))
	}
	;~ RemoveFocusChangedEventHandler 	40
	;~ RemoveAllEventHandlers 	41

	IntNativeArrayToSafeArray(ByRef nArr, n="") {
		return UIA_Hr(DllCall(this.__Vt(42), "ptr",this.__Value, "ptr",&nArr, "int",n?n:VarSetCapacity(nArr)/4, "ptr*",out))? ComObj(0x2003,out,1):
	}
/*	IntSafeArrayToNativeArray(sArr, Byref nArr="", Byref arrayCount="") { ; NOT WORKING
		VarSetCapacity(nArr,(sArr.MaxIndex()+1)*4)
		return UIA_Hr(DllCall(this.__Vt(43), "ptr",this.__Value, "ptr",ComObjValue(sArr), "ptr*",nArr, "int*",arrayCount))? arrayCount:
	}
*/
	RectToVariant(ByRef rect, ByRef out="") {	; in:{left,top,right,bottom} ; out:(left,top,width,height)
		; in:	RECT Struct
		; out:	AHK Wrapped SafeArray & ByRef Variant
		return UIA_Hr(DllCall(this.__Vt(44), "ptr",this.__Value, "ptr",&rect, "ptr",UIA_Variant(out)))? UIA_VariantData(out):
	}
/*	VariantToRect(ByRef var, ByRef out="") { ; NOT WORKING
		; in:	VT_VARIANT (SafeArray)
		; out:	AHK Wrapped RECT Struct & ByRef Struct
		return UIA_Hr(DllCall(this.__Vt(45), "ptr",this.__Value, "ptr",var, "ptr",&(out,VarSetCapacity(out,16))))? UIA_RectToObject(out):
	}
*/
	;~ SafeArrayToRectNativeArray 	46
	;~ CreateProxyFactoryEntry 	47
	GetPropertyProgrammaticName(Id) {
		return UIA_Hr(DllCall(this.__Vt(49), "ptr",this.__Value, "int",Id, "ptr*",out))? StrGet(out):
	}
	GetPatternProgrammaticName(Id) {
		return UIA_Hr(DllCall(this.__Vt(50), "ptr",this.__Value, "int",Id, "ptr*",out))? StrGet(out):
	}
	PollForPotentialSupportedPatterns(e, Byref Ids="", Byref Names="") {
		return UIA_Hr(DllCall(this.__Vt(51), "ptr",this.__Value, "ptr",e.__Value, "ptr*",Ids, "ptr*",Names))? UIA_SafeArraysToObject(Names:=ComObj(0x2008,Names,1),Ids:=ComObj(0x2003,Ids,1)):
	}
	PollForPotentialSupportedProperties(e, Byref Ids="", Byref Names="") {
		return UIA_Hr(DllCall(this.__Vt(52), "ptr",this.__Value, "ptr",e.__Value, "ptr*",Ids, "ptr*",Names))? UIA_SafeArraysToObject(Names:=ComObj(0x2008,Names,1),Ids:=ComObj(0x2003,Ids,1)):
	}
	CheckNotSupported(value) { ; Useless in this Framework???
	/*	Checks a provided VARIANT to see if it contains the Not Supported identifier.
		After retrieving a property for a UI Automation element, call this method to determine whether the element supports the 
		retrieved property. CheckNotSupported is typically called after calling a property retrieving method such as GetCurrentPropertyValue.
	*/
		return UIA_Hr(DllCall(this.__Vt(53), "ptr",this.__Value, "ptr",value, "int*",out))? out:
	}
	ElementFromIAccessible(IAcc, childId=0) {
	/* The method returns E_INVALIDARG - "One or more arguments are not valid" - if the underlying implementation of the
	Microsoft UI Automation element is not a native Microsoft Active Accessibility server; that is, if a client attempts to retrieve
	the IAccessible interface for an element originally supported by a proxy object from Oleacc.dll, or by the UIA-to-MSAA Bridge.
	*/
		return UIA_Hr(DllCall(this.__Vt(56), "ptr",this.__Value, "ptr",ComObjValue(IAcc), "int",childId, "ptr*",out))? new UIA_Element(out):
	}
	;~ ElementFromIAccessibleBuildCache 	57
}

class UIA_Element extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee671425(v=vs.85).aspx
	static __IID := "{d22108aa-8ac5-49a5-837b-37bbb3d7591e}"
		,  __properties := "CurrentProcessId,20,int`r`nCurrentControlType,21,CONTROLTYPEID`r`nCurrentLocalizedControlType,22,BSTR`r`nCurrentName,23,BSTR`r`nCurrentAcceleratorKey,24,BSTR`r`nCurrentAccessKey,25,BSTR`r`nCurrentHasKeyboardFocus,26,BOOL`r`nCurrentIsKeyboardFocusable,27,BOOL`r`nCurrentIsEnabled,28,BOOL`r`nCurrentAutomationId,29,BSTR`r`nCurrentClassName,30,BSTR`r`nCurrentHelpText,31,BSTR`r`nCurrentCulture,32,int`r`nCurrentIsControlElement,33,BOOL`r`nCurrentIsContentElement,34,BOOL`r`nCurrentIsPassword,35,BOOL`r`nCurrentNativeWindowHandle,36,UIA_HWND`r`nCurrentItemType,37,BSTR`r`nCurrentIsOffscreen,38,BOOL`r`nCurrentOrientation,39,OrientationType`r`nCurrentFrameworkId,40,BSTR`r`nCurrentIsRequiredForForm,41,BOOL`r`nCurrentItemStatus,42,BSTR`r`nCurrentBoundingRectangle,43,RECT`r`nCurrentLabeledBy,44,IUIAutomationElement`r`nCurrentAriaRole,45,BSTR`r`nCurrentAriaProperties,46,BSTR`r`nCurrentIsDataValidForForm,47,BOOL`r`nCurrentControllerFor,48,IUIAutomationElementArray`r`nCurrentDescribedBy,49,IUIAutomationElementArray`r`nCurrentFlowsTo,50,IUIAutomationElementArray`r`nCurrentProviderDescription,51,BSTR`r`nCachedProcessId,52,int`r`nCachedControlType,53,CONTROLTYPEID`r`nCachedLocalizedControlType,54,BSTR`r`nCachedName,55,BSTR`r`nCachedAcceleratorKey,56,BSTR`r`nCachedAccessKey,57,BSTR`r`nCachedHasKeyboardFocus,58,BOOL`r`nCachedIsKeyboardFocusable,59,BOOL`r`nCachedIsEnabled,60,BOOL`r`nCachedAutomationId,61,BSTR`r`nCachedClassName,62,BSTR`r`nCachedHelpText,63,BSTR`r`nCachedCulture,64,int`r`nCachedIsControlElement,65,BOOL`r`nCachedIsContentElement,66,BOOL`r`nCachedIsPassword,67,BOOL`r`nCachedNativeWindowHandle,68,UIA_HWND`r`nCachedItemType,69,BSTR`r`nCachedIsOffscreen,70,BOOL`r`nCachedOrientation,71,OrientationType`r`nCachedFrameworkId,72,BSTR`r`nCachedIsRequiredForForm,73,BOOL`r`nCachedItemStatus,74,BSTR`r`nCachedBoundingRectangle,75,RECT`r`nCachedLabeledBy,76,IUIAutomationElement`r`nCachedAriaRole,77,BSTR`r`nCachedAriaProperties,78,BSTR`r`nCachedIsDataValidForForm,79,BOOL`r`nCachedControllerFor,80,IUIAutomationElementArray`r`nCachedDescribedBy,81,IUIAutomationElementArray`r`nCachedFlowsTo,82,IUIAutomationElementArray`r`nCachedProviderDescription,83,BSTR"
	
	SetFocus() {
		return UIA_Hr(DllCall(this.__Vt(3), "ptr",this.__Value))
	}
	GetRuntimeId(ByRef stringId="") {
		return UIA_Hr(DllCall(this.__Vt(4), "ptr",this.__Value, "ptr*",sa))? ComObj(0x2003,sa,1):
	}
	FindFirst(c="", scope=0x2) {
		static tc	; TrueCondition
		if !tc
			tc:=this.__uia.CreateTrueCondition()
		return UIA_Hr(DllCall(this.__Vt(5), "ptr",this.__Value, "uint",scope, "ptr",(c=""?tc:c).__Value, "ptr*",out))? new UIA_Element(out):
	}
	FindAll(c="", scope=0x2) {
		static tc	; TrueCondition
		if !tc
			tc:=this.__uia.CreateTrueCondition()
		return UIA_Hr(DllCall(this.__Vt(6), "ptr",this.__Value, "uint",scope, "ptr",(c=""?tc:c).__Value, "ptr*",out))? UIA_ElementArray(out):
	}
	;~ Find (First/All, Element/Children/Descendants/Parent/Ancestors/Subtree, Conditions)
	;~ FindFirstBuildCache 	7	IUIAutomationElement
	;~ FindAllBuildCache 	8	IUIAutomationElementArray
	;~ BuildUpdatedCache 	9	IUIAutomationElement
	GetCurrentPropertyValue(propertyId, ByRef out="") {
		return UIA_Hr(DllCall(this.__Vt(10), "ptr",this.__Value, "uint",propertyId, "ptr",UIA_Variant(out)))? UIA_VariantData(out):
	}
	GetCurrentPropertyValueEx(propertyId, ignoreDefaultValue=1, ByRef out="") {
	; Passing FALSE in the ignoreDefaultValue parameter is equivalent to calling GetCurrentPropertyValue
		return UIA_Hr(DllCall(this.__Vt(11), "ptr",this.__Value, "uint",propertyId, "uint",ignoreDefaultValue, "ptr",UIA_Variant(out)))? UIA_VariantData(out):
	}
	;~ GetCachedPropertyValue 	12	VARIANT
	;~ GetCachedPropertyValueEx 	13	VARIANT
	GetCurrentPatternAs(pattern="") {
		if IsObject(UIA_%pattern%Pattern)&&(iid:=UIA_%pattern%Pattern.__iid)&&(pId:=UIA_%pattern%Pattern.__PatternID)
			return UIA_Hr(DllCall(this.__Vt(14), "ptr",this.__Value, "int",pId, "ptr",UIA_GUID(riid,iid), "ptr*",out))? new UIA_%pattern%Pattern(out):
		else throw Exception("Pattern not implemented.",-1, "UIA_" pattern "Pattern")
	}
	;~ GetCachedPatternAs 	15	void **ppv
	;~ GetCurrentPattern 	16	Iunknown **patternObject
	;~ GetCachedPattern 	17	Iunknown **patternObject
	;~ GetCachedParent 	18	IUIAutomationElement
	GetCachedChildren() { ; Haven't successfully tested
		return UIA_Hr(DllCall(this.__Vt(19), "ptr",this.__Value, "ptr*",out))&&out? UIA_ElementArray(out):
	}
	;~ GetClickablePoint 	84	POINT, BOOL
}

class UIA_ElementArray extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee671426(v=vs.85).aspx
	static __IID := "{14314595-b4bc-4055-95f2-58f2e42c9855}"
		,  __properties := "Length,3,int"
	
	GetElement(i) {
		return UIA_Hr(DllCall(this.__Vt(4), "ptr",this.__Value, "int",i, "ptr*",out))? new UIA_Element(out):
	}
}

class UIA_TreeWalker extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee671470(v=vs.85).aspx
	static __IID := "{4042c624-389c-4afc-a630-9df854a541fc}"
		,  __properties := "Condition,15,IUIAutomationCondition"
	
	GetParentElement(e) {
		return UIA_Hr(DllCall(this.__Vt(3), "ptr",this.__Value, "ptr",e.__Value, "ptr*",out))? new UIA_Element(out):
	}
	GetFirstChildElement(e) {
		return UIA_Hr(DllCall(this.__Vt(4), "ptr",this.__Value, "ptr",e.__Value, "ptr*",out))&&out? new UIA_Element(out):
	}
	GetLastChildElement(e) {
		return UIA_Hr(DllCall(this.__Vt(5), "ptr",this.__Value, "ptr",e.__Value, "ptr*",out))&&out? new UIA_Element(out):
	}
	GetNextSiblingElement(e) {
		return UIA_Hr(DllCall(this.__Vt(6), "ptr",this.__Value, "ptr",e.__Value, "ptr*",out))&&out? new UIA_Element(out):
	}
	GetPreviousSiblingElement(e) {
		return UIA_Hr(DllCall(this.__Vt(7), "ptr",this.__Value, "ptr",e.__Value, "ptr*",out))&&out? new UIA_Element(out):
	}
	NormalizeElement(e) {
		return UIA_Hr(DllCall(this.__Vt(8), "ptr",this.__Value, "ptr",e.__Value, "ptr*",out))&&out? new UIA_Element(out):
	}
/*	GetParentElementBuildCache(e, cacheRequest) {
		return UIA_Hr(DllCall(this.__Vt(9), "ptr",this.__Value, "ptr",e.__Value, "ptr",cacheRequest.__Value.__Value, "ptr*",out))? new UIA_Element(out):
	}
	GetFirstChildElementBuildCache(e, cacheRequest) {
		return UIA_Hr(DllCall(this.__Vt(10), "ptr",this.__Value, "ptr",e.__Value, "ptr",cacheRequest.__Value, "ptr*",out))? new UIA_Element(out):
	}
	GetLastChildElementBuildCache(e, cacheRequest) {
		return UIA_Hr(DllCall(this.__Vt(11), "ptr",this.__Value, "ptr",e.__Value, "ptr",cacheRequest.__Value, "ptr*",out))? new UIA_Element(out):
	}
	GetNextSiblingElementBuildCache(e, cacheRequest) {
		return UIA_Hr(DllCall(this.__Vt(12), "ptr",this.__Value, "ptr",e.__Value, "ptr",cacheRequest.__Value, "ptr*",out))? new UIA_Element(out):
	}
	GetPreviousSiblingElementBuildCache(e, cacheRequest) {
		return UIA_Hr(DllCall(this.__Vt(13), "ptr",this.__Value, "ptr",e.__Value, "ptr",cacheRequest.__Value, "ptr*",out))? new UIA_Element(out):
	}
	NormalizeElementBuildCache(e, cacheRequest) {
		return UIA_Hr(DllCall(this.__Vt(14), "ptr",this.__Value, "ptr",e.__Value, "ptr",cacheRequest.__Value, "ptr*",out))? new UIA_Element(out):
	}
*/
}

class UIA_Condition extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee671420(v=vs.85).aspx
	static __IID := "{352ffba8-0973-437c-a61f-f64cafd81df9}"
}

class UIA_PropertyCondition extends UIA_Condition {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee696121(v=vs.85).aspx
	static __IID := "{99ebf2cb-5578-4267-9ad4-afd6ea77e94b}"
		,  __properties := "PropertyId,3,PROPERTYID`r`nPropertyValue,4,VARIANT`r`nPropertyConditionFlags,5,PropertyConditionFlags"
}
; should returned children have a condition type (property/and/or/bool/not), or be a generic uia_condition object?
class UIA_AndCondition extends UIA_Condition {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee671407(v=vs.85).aspx
	static __IID := "{a7d0af36-b912-45fe-9855-091ddc174aec}"
		,  __properties := "ChildCount,3,int"
	
	;~ GetChildrenAsNativeArray	4	IUIAutomationCondition ***childArray
	GetChildren() {
		return UIA_Hr(DllCall(this.__Vt(5), "ptr",this.__Value, "ptr*",out))&&out? ComObj(0x2003,out,1):
	}
}
class UIA_OrCondition extends UIA_Condition {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee696108(v=vs.85).aspx
	static __IID := "{8753f032-3db1-47b5-a1fc-6e34a266c712}"
		,  __properties := "ChildCount,3,int"
	
	;~ GetChildrenAsNativeArray	4	IUIAutomationCondition ***childArray
	;~ GetChildren	5	SAFEARRAY
}
class UIA_BoolCondition extends UIA_Condition {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee671411(v=vs.85).aspx
	static __IID := "{8753f032-3db1-47b5-a1fc-6e34a266c712}"
		,  __properties := "BooleanValue,3,boolVal"
}
class UIA_NotCondition extends UIA_Condition {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee696106(v=vs.85).aspx
	static __IID := "{f528b657-847b-498c-8896-d52b565407a1}"
	
	;~ GetChild	3	IUIAutomationCondition
}

class UIA_IUnknown extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ms680509(v=vs.85).aspx
	static __IID := "{00000000-0000-0000-C000-000000000046}"
}

class UIA_CacheRequest  extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee671413(v=vs.85).aspx
	static __IID := "{b32a92b5-bc25-4078-9c08-d7ee95c48e03}"
}


class _UIA_EventHandler {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee696044(v=vs.85).aspx
	static __IID := "{146c3c17-f12e-4e22-8c27-f894b9b79c69}"
	
/*	HandleAutomationEvent	3
		[in]  IUIAutomationElement *sender,
		[in]  EVENTID eventId
*/
}
class _UIA_FocusChangedEventHandler {		
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee696051(v=vs.85).aspx
	static __IID := "{c270f6b5-5c69-4290-9745-7a7f97169468}"
	
/*	HandleFocusChangedEvent	3
		[in]  IUIAutomationElement *sender
*/
}
class _UIA_PropertyChangedEventHandler {		
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee696119(v=vs.85).aspx
	static __IID := "{40cd37d4-c756-4b0c-8c6f-bddfeeb13b50}"
	
/*	HandlePropertyChangedEvent	3
		[in]  IUIAutomationElement *sender,
		[in]  PROPERTYID propertyId,
		[in]  VARIANT newValue
*/
}
class _UIA_StructureChangedEventHandler {		
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee696197(v=vs.85).aspx
	static __IID := "{e81d1b4e-11c5-42f8-9754-e7036c79f054}"
	
/*	HandleStructureChangedEvent	3
		[in]  IUIAutomationElement *sender,
		[in]  StructureChangeType changeType,
		[in]  SAFEARRAY *runtimeId[int]
*/
}
class _UIA_TextEditTextChangedEventHandler { ; Windows 8.1 Preview [desktop apps only]
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/dn302202(v=vs.85).aspx
	static __IID := "{92FAA680-E704-4156-931A-E32D5BB38F3F}"
	
	;~ HandleTextEditTextChangedEvent	3
}


;~ 		UIA_Patterns - http://msdn.microsoft.com/en-us/library/windows/desktop/ee684023
class UIA_DockPattern extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee671421
	static	__IID := "{fde5ef97-1464-48f6-90bf-43d0948e86ec}"
		,	__PatternID := 10011
		,	__Properties := "CurrentDockPosition,4,int`r`nCachedDockPosition,5,int"

	SetDockPosition(Pos) {
		return UIA_Hr(DllCall(this.__Vt(3), "ptr",this.__Value, "uint",pos))
	}
/*	DockPosition_Top	= 0,
	DockPosition_Left	= 1,
	DockPosition_Bottom	= 2,
	DockPosition_Right	= 3,
	DockPosition_Fill	= 4,
	DockPosition_None	= 5
*/
}
class UIA_ExpandCollapsePattern extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee696046
	static	__IID := "{619be086-1f4e-4ee4-bafa-210128738730}"
		,	__PatternID := 10005
		,	__Properties := "CachedExpandCollapseState,6,int`r`nCurrentExpandCollapseState,5,int"
	
	Expand() {
		return UIA_Hr(DllCall(this.__Vt(3), "ptr",this.__Value))
	}
	Collapse() {
		return UIA_Hr(DllCall(this.__Vt(4), "ptr",this.__Value))
	}	
/*	ExpandCollapseState_Collapsed	= 0,
	ExpandCollapseState_Expanded	= 1,
	ExpandCollapseState_PartiallyExpanded	= 2,
	ExpandCollapseState_LeafNode	= 3
*/
}
class UIA_GridItemPattern extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee696053
	static	__IID := "{78f8ef57-66c3-4e09-bd7c-e79b2004894d}"
		,	__PatternID := 10007
		,	__Properties := "CurrentContainingGrid,3,IUIAutomationElement`r`nCurrentRow,4,int`r`nCurrentColumn,5,int`r`nCurrentRowSpan,6,int`r`nCurrentColumnSpan,7,int`r`nCachedContainingGrid,8,IUIAutomationElement`r`nCachedRow,9,int`r`nCachedColumn,10,int`r`nCachedRowSpan,11,int`r`nCachedColumnSpan,12,int"
}
class UIA_GridPattern extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee696064
	static	__IID := "{414c3cdc-856b-4f5b-8538-3131c6302550}"
		,	__PatternID := 10006
		,	__Properties := "CurrentRowCount,4,int`r`nCurrentColumnCount,5,int`r`nCachedRowCount,6,int`r`nCachedColumnCount,7,int"

	GetItem(row,column) { ; Hr!=0 if no result, or blank output?
		return UIA_Hr(DllCall(this.__Vt(3), "ptr",this.__Value, "uint",row, "uint",column, "ptr*",out))? new UIA_Element(out):
	}
}
class UIA_InvokePattern extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee696070
	static	__IID := "{fb377fbe-8ea6-46d5-9c73-6499642d3059}"
		,	__PatternID := 10000
	
	Invoke() {
		return UIA_Hr(DllCall(this.__Vt(3), "ptr",this.__Value))
	}
}
class UIA_ItemContainerPattern extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee696072
	static	__IID := "{c690fdb2-27a8-423c-812d-429773c9084e}"
		,	__PatternID := 10019

	FindItemByProperty(startAfter, propertyId, ByRef value, type=8) {	; Hr!=0 if no result, or blank output?
		if (type!="Variant")
			UIA_Variant(value,type,value)
		return UIA_Hr(DllCall(this.__Vt(3), "ptr",this.__Value, "ptr",startAfter.__Value, "int",propertyId, "ptr",&value, "ptr*",out))? new UIA_Element(out):
	}
}
class UIA_LegacyIAccessiblePattern extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee696074
	static	__IID := "{828055ad-355b-4435-86d5-3b51c14a9b1b}"
		,	__PatternID := 10018
		,	__Properties := "CurrentChildId,6,int`r`nCurrentName,7,BSTR`r`nCurrentValue,8,BSTR`r`nCurrentDescription,9,BSTR`r`nCurrentRole,10,DWORD`r`nCurrentState,11,DWORD`r`nCurrentHelp,12,BSTR`r`nCurrentKeyboardShortcut,13,BSTR`r`nCurrentDefaultAction,15,BSTR`r`nCachedChildId,16,int`r`nCachedName,17,BSTR`r`nCachedValue,18,BSTR`r`nCachedDescription,19,BSTR`r`nCachedRole,20,DWORD`r`nCachedState,21,DWORD`r`nCachedHelp,22,BSTR`r`nCachedKeyboardShortcut,23,BSTR`r`nCachedDefaultAction,25,BSTR"

	Select(flags=3) {
		return UIA_Hr(DllCall(this.__Vt(3), "ptr",this.__Value, "int",flags))
	}
	DoDefaultAction() {
		return UIA_Hr(DllCall(this.__Vt(4), "ptr",this.__Value))
	}
	SetValue(value) {
		return UIA_Hr(DllCall(this.__Vt(5), "ptr",this.__Value, "ptr",&value))
	}
	GetCurrentSelection() { ; Not correct
		;~ if (hr:=DllCall(this.__Vt(14), "ptr",this.__Value, "ptr*",array))=0
			;~ return new UIA_ElementArray(array)
		;~ else
			;~ MsgBox,, Error, %hr%
	}
	;~ GetCachedSelection	24	IUIAutomationElementArray
	GetIAccessible() {
	/*	This method returns NULL if the underlying implementation of the UI Automation element is not a native 
	Microsoft Active Accessibility server; that is, if a client attempts to retrieve the IAccessible interface 
	for an element originally supported by a proxy object from OLEACC.dll, or by the UIA-to-MSAA Bridge.
	*/
		return UIA_Hr(DllCall(this.__Vt(26), "ptr",this.__Value, "ptr*",pacc))&&pacc? ComObj(9,pacc,1):
	}
}
class UIA_MultipleViewPattern extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee696099
	static	__IID := "{8d253c91-1dc5-4bb5-b18f-ade16fa495e8}"
		,	__PatternID := 10008
		,	__Properties := "CurrentCurrentView,5,int`r`nCachedCurrentView,7,int"

	GetViewName(view) { ; need to release BSTR?
		return UIA_Hr(DllCall(this.__Vt(3), "ptr",this.__Value, "int",view, "ptr*",name))? StrGet(name):
	}
	SetCurrentView(view) {
		return UIA_Hr(DllCall(this.__Vt(4), "ptr",this.__Value, "int",view))
	}
	GetCurrentSupportedViews() {
		return UIA_Hr(DllCall(this.__Vt(6), "ptr",this.__Value, "ptr*",out))? ComObj(0x2003,out,1):
	}
	GetCachedSupportedViews() {
		return UIA_Hr(DllCall(this.__Vt(8), "ptr",this.__Value, "ptr*",out))? ComObj(0x2003,out,1):
	}
}
class UIA_RangeValuePattern extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee696147
	static	__IID := "{59213f4f-7346-49e5-b120-80555987a148}"
		,	__PatternID := 10003
		,	__Properties := "CurrentValue,4,double`r`nCurrentIsReadOnly,5,BOOL`r`nCurrentMaximum,6,double`r`nCurrentMinimum,7,double`r`nCurrentLargeChange,8,double`r`nCurrentSmallChange,9,double`r`nCachedValue,10,double`r`nCachedIsReadOnly,11,BOOL`r`nCachedMaximum,12,double`r`nCachedMinimum,13,double`r`nCachedLargeChange,14,double`r`nCachedSmallChange,15,double"

	SetValue(val) {
		return UIA_Hr(DllCall(this.__Vt(3), "ptr",this.__Value, "double",val))
	}
}
class UIA_ScrollItemPattern extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee696165
	static	__IID := "{b488300f-d015-4f19-9c29-bb595e3645ef}"
		,	__PatternID := 10017

	ScrollIntoView() {
		return UIA_Hr(DllCall(this.__Vt(3), "ptr",this.__Value))
	}
}
class UIA_ScrollPattern extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee696167
	static	__IID := "{88f4d42a-e881-459d-a77c-73bbbb7e02dc}"
		,	__PatternID := 10004
		,	__Properties := "CurrentHorizontalScrollPercent,5,double`r`nCurrentVerticalScrollPercent,6,double`r`nCurrentHorizontalViewSize,7,double`r`CurrentHorizontallyScrollable,9,BOOL`r`nCurrentVerticallyScrollable,10,BOOL`r`nCachedHorizontalScrollPercent,11,double`r`nCachedVerticalScrollPercent,12,double`r`nCachedHorizontalViewSize,13,double`r`nCachedVerticalViewSize,14,double`r`nCachedHorizontallyScrollable,15,BOOL`r`nCachedVerticallyScrollable,16,BOOL"
		
	Scroll(horizontal, vertical) {
		return UIA_Hr(DllCall(this.__Vt(3), "ptr",this.__Value, "uint",horizontal, "uint",vertical))
	}
	SetScrollPercent(horizontal, vertical) {
		return UIA_Hr(DllCall(this.__Vt(4), "ptr",this.__Value, "double",horizontal, "double",vertical))
	}
/*	UIA_ScrollPatternNoScroll	=	-1
	ScrollAmount_LargeDecrement	= 0,
	ScrollAmount_SmallDecrement	= 1,
	ScrollAmount_NoAmount	= 2,
	ScrollAmount_LargeIncrement	= 3,
	ScrollAmount_SmallIncrement	= 4
*/
}
;~ class UIA_SelectionItemPattern extends UIA_Base {10010
;~ class UIA_SelectionPattern extends UIA_Base {10001
;~ class UIA_SpreadsheetItemPattern extends UIA_Base {10027
;~ class UIA_SpreadsheetPattern extends UIA_Base {10026
;~ class UIA_StylesPattern extends UIA_Base {10025
;~ class UIA_SynchronizedInputPattern extends UIA_Base {10021
;~ class UIA_TableItemPattern extends UIA_Base {10013
;~ class UIA_TablePattern extends UIA_Base {10012
;~ class UIA_TextChildPattern extends UIA_Base {10029
;~ class UIA_TextEditPattern extends UIA_Base {10032
;~ class UIA_TextPattern extends UIA_Base {10014
;~ class UIA_TextPattern2 extends UIA_Base {10024
;~ class UIA_TogglePattern extends UIA_Base {10015
;~ class UIA_TransformPattern extends UIA_Base {10016
;~ class UIA_TransformPattern2 extends UIA_Base {10028
;~ class UIA_ValuePattern extends UIA_Base {10002
;~ class UIA_VirtualizedItemPattern extends UIA_Base {10020
;~ class UIA_WindowPattern extends UIA_Base {10009
;~ class UIA_AnnotationPattern extends UIA_Base {10023		; Windows 8 [desktop apps only]
;~ class UIA_DragPattern extends UIA_Base {10030			; Windows 8 [desktop apps only]
;~ class UIA_DropTargetPattern extends UIA_Base {10031		; Windows 8 [desktop apps only]
/* class UIA_ObjectModelPattern extends UIA_Base {			; Windows 8 [desktop apps only]
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/hh437262(v=vs.85).aspx
	static	__IID := "{71c284b3-c14d-4d14-981e-19751b0d756d}"
		,	__PatternID := 10022
	
	GetUnderlyingObjectModel() {
		return UIA_Hr(DllCall(this.__Vt(3), "ptr",this.__Value))
	}
}
*/

;~ class UIA_PatternHandler extends UIA_Base {
;~ class UIA_PatternInstance extends UIA_Base {
;~ class UIA_TextRange extends UIA_Base {
;~ class UIA_TextRange2 extends UIA_Base {
;~ class UIA_TextRangeArray extends UIA_Base {




{  ;~ UIA Functions
	UIA_Interface() {
		try {
			if uia:=ComObjCreate("{ff48dba4-60ef-4201-aa87-54103eef594e}","{30cbe57d-d9d0-452a-ab13-7ac5ac4825ee}")
				return uia:=new UIA_Interface(uia), uia.base.base.__UIA:=uia
			throw "UIAutomation Interface failed to initialize."
		} catch e
			MsgBox, 262160, UIA Startup Error, % IsObject(e)?"IUIAutomation Interface is not registered.":e.Message
	}
	UIA_Hr(hr) {
		;~ http://blogs.msdn.com/b/eldar/archive/2007/04/03/a-lot-of-hresult-codes.aspx
		static err:={0x8000FFFF:"Catastrophic failure.",0x80004001:"Not implemented.",0x8007000E:"Out of memory.",0x80070057:"One or more arguments are not valid.",0x80004002:"Interface not supported.",0x80004003:"Pointer not valid.",0x80070006:"Handle not valid.",0x80004004:"Operation aborted.",0x80004005:"Unspecified error.",0x80070005:"General access denied.",0x800401E5:"The object identified by this moniker could not be found.",0x80040201:"UIA_E_ELEMENTNOTAVAILABLE",0x80040200:"UIA_E_ELEMENTNOTENABLED",0x80131509:"UIA_E_INVALIDOPERATION",0x80040202:"UIA_E_NOCLICKABLEPOINT",0x80040204:"UIA_E_NOTSUPPORTED",0x80040203:"UIA_E_PROXYASSEMBLYNOTLOADED"} ; //not completed
		if hr&&(hr&=0xFFFFFFFF) {
			RegExMatch(Exception("",-2).what,"(\w+).(\w+)",i)
			throw Exception(UIA_Hex(hr) " - " err[hr], -2, i2 "  (" i1 ")")
		}
		return !hr
	}
	UIA_NotImplemented() {
		RegExMatch(Exception("",-2).What,"(\D+)\.(\D+)",m)
		MsgBox, 262192, UIA Message, Class:`t%m1%`nMember:`t%m2%`n`nMethod has not been implemented yet.
	}
	UIA_ElementArray(p, uia="") { ; should AHK Object be 0 or 1 based?
		a:=new UIA_ElementArray(p),out:=[]
		Loop % a.Length
			out[A_Index]:=a.GetElement(A_Index-1)
		return out, out.base:={UIA_ElementArray:a}
	}
	UIA_RectToObject(ByRef r) { ; rect.__Value work with DllCalls?
		static b:={__Class:"object",__Type:"RECT",Struct:Func("UIA_RectStructure")}
		return {l:NumGet(r,0,"Int"),t:NumGet(r,4,"Int"),r:NumGet(r,8,"Int"),b:NumGet(r,12,"Int"),base:b}
	}
	UIA_RectStructure(this, ByRef r) {
		static sides:="ltrb"
		VarSetCapacity(r,16)
		Loop Parse, sides
			NumPut(this[A_LoopField],r,(A_Index-1)*4,"Int")
	}
	UIA_SafeArraysToObject(keys,values) {
	;~	1 dim safearrays w/ same # of elements
		out:={}
		for key in keys
			out[key]:=values[A_Index-1]
		return out
	}
	UIA_Hex(p) {
		setting:=A_FormatInteger
		SetFormat,IntegerFast,H
		out:=p+0 ""
		SetFormat,IntegerFast,%setting%
		return out
	}
	UIA_GUID(ByRef GUID, sGUID) { ;~ Converts a string to a binary GUID and returns its address.
		VarSetCapacity(GUID,16,0)
		return DllCall("ole32\CLSIDFromString", "wstr",sGUID, "ptr",&GUID)>=0?&GUID:""
	}
	UIA_Variant(ByRef var,type=0,val=0) {
		; Does a variant need to be cleared? If it uses SysAllocString? 
		return (VarSetCapacity(var,8+2*A_PtrSize)+NumPut(type,var,0,"short")+NumPut(type=8? DllCall("oleaut32\SysAllocString", "ptr",&val):val,var,8,"ptr"))*0+&var
	}
	UIA_IsVariant(ByRef vt, ByRef type="") {
		size:=VarSetCapacity(vt),type:=NumGet(vt,"UShort")
		return size>=16&&size<=24&&type>=0&&(type<=23||type|0x2000)
	}
	UIA_Type(ByRef item, ByRef info) {
	}
	UIA_VariantData(ByRef p, flag=1) {
		; based on Sean's COM_Enumerate function
		; need to clear varaint? what if you still need it (flag param)?
		return !UIA_IsVariant(p,vt)?"Invalid Variant"
				:vt=3?NumGet(p,8,"int")
				:vt=8?StrGet(NumGet(p,8))
				:vt=9||vt=13||vt&0x2000?ComObj(vt,NumGet(p,8),flag)
				:vt<0x1000&&UIA_VariantChangeType(&p,&p)=0?StrGet(NumGet(p,8)) UIA_VariantClear(&p)
				:NumGet(p,8)
	/*
		VT_EMPTY     =      0  		; No value
		VT_NULL      =      1 		; SQL-style Null
		VT_I2        =      2 		; 16-bit signed int
		VT_I4        =      3 		; 32-bit signed int
		VT_R4        =      4 		; 32-bit floating-point number
		VT_R8        =      5 		; 64-bit floating-point number
		VT_CY        =      6 		; Currency
		VT_DATE      =      7  		; Date
		VT_BSTR      =      8 		; COM string (Unicode string with length prefix)
		VT_DISPATCH  =      9 		; COM object 
		VT_ERROR     =    0xA  10	; Error code (32-bit integer)
		VT_BOOL      =    0xB  11	; Boolean True (-1) or False (0)
		VT_VARIANT   =    0xC  12	; VARIANT (must be combined with VT_ARRAY or VT_BYREF)
		VT_UNKNOWN   =    0xD  13	; IUnknown interface pointer
		VT_DECIMAL   =    0xE  14	; (not supported)
		VT_I1        =   0x10  16	; 8-bit signed int
		VT_UI1       =   0x11  17	; 8-bit unsigned int
		VT_UI2       =   0x12  18	; 16-bit unsigned int
		VT_UI4       =   0x13  19	; 32-bit unsigned int
		VT_I8        =   0x14  20	; 64-bit signed int
		VT_UI8       =   0x15  21	; 64-bit unsigned int
		VT_INT       =   0x16  22	; Signed machine int
		VT_UINT      =   0x17  23	; Unsigned machine int
		VT_RECORD    =   0x24  36	; User-defined type
		VT_ARRAY     = 0x2000  		; SAFEARRAY
		VT_BYREF     = 0x4000  		; Pointer to another type of value
					 = 0x1000  4096
		COM_VariantChangeType(pvarDst, pvarSrc, vt=8) {
			return DllCall("oleaut32\VariantChangeTypeEx", "ptr",pvarDst, "ptr",pvarSrc, "Uint",1024, "Ushort",0, "Ushort",vt)
		}
		COM_VariantClear(pvar) {
			DllCall("oleaut32\VariantClear", "ptr",pvar)
		}
		COM_SysAllocString(str) {
			Return	DllCall("oleaut32\SysAllocString", "Uint", &str)
		}
		COM_SysFreeString(pstr) {
				DllCall("oleaut32\SysFreeString", "Uint", pstr)
		}
		COM_SysString(ByRef wString, sString) {
			VarSetCapacity(wString,4+nLen:=2*StrLen(sString))
			Return	DllCall("kernel32\lstrcpyW","Uint",NumPut(nLen,wString),"Uint",&sString)
		}
	*/
	}
	UIA_VariantChangeType(pvarDst, pvarSrc, vt=8) { ; written by Sean
		return DllCall("oleaut32\VariantChangeTypeEx", "ptr",pvarDst, "ptr",pvarSrc, "Uint",1024, "Ushort",0, "Ushort",vt)
	}
	UIA_VariantClear(pvar) { ; Written by Sean
		DllCall("oleaut32\VariantClear", "ptr",pvar)
	}
}
MsgBox(msg) {
	MsgBox %msg%
}

/*
enum TreeScope
    {	TreeScope_Element	= 0x1,
	TreeScope_Children	= 0x2,
	TreeScope_Descendants	= 0x4,
	TreeScope_Parent	= 0x8,
	TreeScope_Ancestors	= 0x10,
	TreeScope_Subtree	= ( ( TreeScope_Element | TreeScope_Children )  | TreeScope_Descendants ) 
    } ;
DllCall("oleaut32\SafeArrayGetVartype", "ptr*",ComObjValue(SafeArray), "uint*",pvt)
HRESULT SafeArrayGetVartype(
  _In_   SAFEARRAY *psa,
  _Out_  VARTYPE *pvt
);
DllCall("oleaut32\SafeArrayDestroy", "ptr",ComObjValue(SafeArray))
HRESULT SafeArrayDestroy(
  _In_  SAFEARRAY *psa
);
Last edited by malcev on 20 Sep 2020, 12:15, edited 1 time in total.
teadrinker
Posts: 1865
Joined: 29 Mar 2015, 09:41
Contact:

Re: Running scripts only when a specific language is active

20 Sep 2020, 12:10

malcev wrote: focused controls + controls running from ApplicationFrameHost.exe
You are right, but your code doesn't work for me either, tried with the calculator window on Windows 10.
teadrinker
Posts: 1865
Joined: 29 Mar 2015, 09:41
Contact:

Re: Running scripts only when a specific language is active

20 Sep 2020, 12:58

Now it works somehow, but actually it's incorrect, it doesn't get the focused control, try

Code: Select all

...
   pid := GetFocusedElement.CurrentProcessId
   ControlGetFocus, FocusedControl, ahk_pid %pid%
   MsgBox, % FocusedControl
...
However, this works also:

Code: Select all

F11:: MsgBox % GetInputLangName( GetInputLangID(WinExist("A")) )
 
GetInputLangID(hWnd) {
   WinGet, processName, ProcessName, ahk_id %hWnd%
   if (processName = "ApplicationFrameHost.exe") {
      WinGet, PID, PID, ahk_id %hWnd%
      WinGet, controlList, controlListHwnd, ahk_id %hWnd%
      Loop, parse, controlList, `n
         DllCall("GetWindowThreadProcessId", "Ptr", A_LoopField, "UIntP", childPID)
      until childPID != PID
      dhw_prev := A_DetectHiddenWindows
      DetectHiddenWindows, On
      hWnd := WinExist("ahk_pid" . childPID)
      DetectHiddenWindows, % dhw_prev
   }
   threadId := DllCall("GetWindowThreadProcessId", "Ptr", hWnd, "UInt", 0)
   lyt := DllCall("GetKeyboardLayout", "Ptr", threadId, "UInt")
   return langID := Format("{:#x}", lyt & 0x3FFF)
}
 
GetInputLangName(langId)  {
   static LOCALE_SENGLANGUAGE := 0x1001
   charCount := DllCall("GetLocaleInfo", "UInt", langId, "UInt", LOCALE_SENGLANGUAGE, "UInt", 0, "UInt", 0)
   VarSetCapacity(localeSig, size := charCount << !!A_IsUnicode, 0)
   DllCall("GetLocaleInfo", "UInt", langId, "UInt", LOCALE_SENGLANGUAGE, "Str", localeSig, "UInt", size)
   return localeSig
}

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, BekaStorm, boiler, DanRim, davidjmorin, Gio, glnklein, Gushou, hd0202, keylo, mikeyww, mmmax, roysubs and 64 guests