Enumerate all available ASCII Carbon Keyboard Layouts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2011 and onwards The OpenVanilla Project (MIT License). | |
// All possible vChewing-specific modifications are of: | |
// (c) 2021 and onwards The vChewing Project (MIT-NTL License). | |
/* | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal in | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
the Software, and to permit persons to whom the Software is furnished to do so, | |
subject to the following conditions: | |
1. The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
2. No trademark license is granted to use the trade names, trademarks, service | |
marks, or product names of Contributor, except as required to fulfill notice | |
requirements above. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
#!/usr/bin/swift | |
import Carbon | |
import Cocoa | |
enum IME { | |
// MARK: - 準備枚舉系統內所有的 ASCII 鍵盤佈局 | |
struct CarbonKeyboardLayout { | |
var strName: String = "" | |
var strValue: String = "" | |
} | |
static var arrEnumerateSystemKeyboardLayouts: [IME.CarbonKeyboardLayout] { | |
// 提前塞入 macOS 內建的兩款動態鍵盤佈局 | |
var arrKeyLayouts: [IME.CarbonKeyboardLayout] = [] | |
arrKeyLayouts += [ | |
IME.CarbonKeyboardLayout.init( | |
strName: NSLocalizedString("Apple Chewing - Dachen", comment: ""), | |
strValue: "com.apple.keylayout.ZhuyinBopomofo"), | |
IME.CarbonKeyboardLayout.init( | |
strName: NSLocalizedString("Apple Chewing - Eten Traditional", comment: ""), | |
strValue: "com.apple.keylayout.ZhuyinEten"), | |
] | |
// 準備枚舉系統內所有的 ASCII 鍵盤佈局 | |
var arrKeyLayoutsASCII: [IME.CarbonKeyboardLayout] = [] | |
let list = TISCreateInputSourceList(nil, true).takeRetainedValue() as! [TISInputSource] | |
for source in list { | |
if let ptrCategory = TISGetInputSourceProperty(source, kTISPropertyInputSourceCategory) { | |
let category = Unmanaged<CFString>.fromOpaque(ptrCategory).takeUnretainedValue() | |
if category != kTISCategoryKeyboardInputSource { | |
continue | |
} | |
} else { | |
continue | |
} | |
if let ptrASCIICapable = TISGetInputSourceProperty( | |
source, kTISPropertyInputSourceIsASCIICapable) | |
{ | |
let asciiCapable = Unmanaged<CFBoolean>.fromOpaque(ptrASCIICapable) | |
.takeUnretainedValue() | |
if asciiCapable != kCFBooleanTrue { | |
continue | |
} | |
} else { | |
continue | |
} | |
if let ptrSourceType = TISGetInputSourceProperty(source, kTISPropertyInputSourceType) { | |
let sourceType = Unmanaged<CFString>.fromOpaque(ptrSourceType).takeUnretainedValue() | |
if sourceType != kTISTypeKeyboardLayout { | |
continue | |
} | |
} else { | |
continue | |
} | |
guard let ptrSourceID = TISGetInputSourceProperty(source, kTISPropertyInputSourceID), | |
let localizedNamePtr = TISGetInputSourceProperty(source, kTISPropertyLocalizedName) | |
else { | |
continue | |
} | |
let sourceID = String(Unmanaged<CFString>.fromOpaque(ptrSourceID).takeUnretainedValue()) | |
let localizedName = String( | |
Unmanaged<CFString>.fromOpaque(localizedNamePtr).takeUnretainedValue()) | |
arrKeyLayoutsASCII += [ | |
IME.CarbonKeyboardLayout.init(strName: localizedName, strValue: sourceID) | |
] | |
} | |
arrKeyLayouts += arrKeyLayoutsASCII.sorted { $0.strValue < $1.strValue } | |
return arrKeyLayouts | |
} | |
} | |
print(IME.arrEnumerateSystemKeyboardLayouts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment