Home > Blockchain >  Detecting the layout of an internal Apple hardware keyboard
Detecting the layout of an internal Apple hardware keyboard

Time:11-23

Is there an API or a method for detecting which of the three main keyboard layouts – ANSI, ISO, or Japanese – a Mac notebook uses?

After fairly extensive research, I could not find any information about this.

CodePudding user response:

After countless hours of searching and digging through dusty manuals, I finally found a way to determine physical keyboard layout types attached to the Mac.

By using ancient Carbon APIs, you can call KBGetLayoutType in combination with LMGetKbdType to return the desired constants. This amazingly still works in macOS Monterey.

To anyone whose looking for a solution in the future, here it is using Swift 5.5:

import Carbon

// Determine the physical keyboard layout type.
// macOS recognizes three keyboard types: ANSI, JIS (Japan), and ISO (Europe).

let physicalKeyboardLayoutType = KBGetLayoutType(Int16(LMGetKbdType()))
switch physicalKeyboardLayoutType {
case _ where physicalKeyboardLayoutType == kKeyboardJIS:
    print("JIS")
case _ where physicalKeyboardLayoutType == kKeyboardISO:
    print("ISO")
default:
    print("ANSI")
}
  • Related