Ext attribute for keyboard layout

Chris
Valued Contributor

We have MacBooks with different keyboard layouts,
so an extension attribute pulling the info which keyboard is actually built into a laptop would be quite helpful.
Anyone have a hint on how to do this?
I have compared the Product IDs of different keyboards in System Profiler,
but couldn't find any consistency...

Any help would be appreciated :)

14 REPLIES 14

seanjsgallagher
Contributor

Are you looking for something that shows different keyboards, or the different keyboard layouts, like the ones in system preferences > Language & Text > Input Sources? Are you looking for both?

Chris
Valued Contributor

I'm looking for the physical keyboard layout.
Hardware wise, not what is set in "Input Sources".
I'd like to be able to tell which MacBooks have a German keyboard (QWERTZ)
and which have a US keyboard (QWERTY) etc...

Sorry if that was unclear.

seanjsgallagher
Contributor

No worries. Probably me, not enough coffee yet.

Ok, I have found where you can get the input sources. Are these keyboard types ordered this way, or are you talking about physically connected external keyboards. (time for more coffee).

Chris
Valued Contributor

*passing coffee to Sean*

For now I'm only interested in the internal keyboards.
As we have people from all over the world working here,
we order some MBs with German keyboards, some with U.S. keyboards,
and there's a couple UK and Japanese floating around also.

Of course i could go through the packing slips and fill my inventory manually,
but I'd prefer a script doing that for me ;)

tep
Contributor II

Another "Dead Thread Thursday".....

@Chris - did you find an effective way to do this?

Chris
Valued Contributor

Hey @tep , no, unfortunately not.
What we're doing now is manually populate an EA with the keyboard layout
and then have a script set the layout in OS X according to the EA's value.

sean
Valued Contributor

What happens if you run this on the different machine models?

#!/bin/bash

serial_number=`system_profiler SPHardwareDataType | awk '/Serial Number/ {print substr($NF,9,4)}'`
curl -s http://support-sp.apple.com/sp/product?cc=$serial_number | sed 's|.*<locale>(.*)</locale>.*|1|'

exit 0

I also notice that the keyboard appears to have a country code

ioreg -c AppleEmbeddedKeyboard | sed -n '/AppleEmbeddedKeyboard/,/CountryCode/p'

Chris
Valued Contributor

Unfortunately the last four of the serial are always the same for one model, no matter which keyboard.
http://support-sp.apple.com/sp/product?cc=$serial_number will report en_US by default,
or de_DE if you append &lang=de_DE to the URL for example.

I also always get "13" as the Country Code from ioreg :(

sean
Valued Contributor

What about this Python script? It isn't immediately what you are after, but you should get different answers. It would involve user input, akin to when Apple asks you to push the key immediately left of the Z key.

Key Listener

mire3212
New Contributor II

@Chris I also have a need to find the physical layout of the keyboard on our MacBook Air/Pro inventory. Any luck so far?

Chris
Valued Contributor

Sorry, haven't really looked into this any more.
I guess using a script with user input as mentioned above is the only way.

federico_joly
New Contributor II

Hey, I came across the same need a few weeks ago (I know it's been a while, sorry). I think this can be the start for a full stack solution (haven't tested if it is affected by plugging an external kb with another physical layout).
If I run this: defaults read ~/Library/Preferences/com.apple.HIToolbox.plist AppleSelectedInputSources | egrep -w 'KeyboardLayout Name'

I definetly get the Input Source, so software, in my case "Spanish - ISO"

But if I run this: ioreg -l | grep KeyboardLanguage | cut -d" -f 4

I'm pretty sure I get the physical layout here, in my case "Spanish - ISO"
I run this last command in another macbook, with a physical kb UK layout and an "Spanish - ISO" (only) Input Source and I get "British"

If anyone is certain that I'm wrong here, please let me know.

Thanks,
Federico.

tlarkin
Honored Contributor

I came up with a solution, but it requires Python 3 + Objective C bridge, but it works and I am using it to track keyboard types now

#!/opt/snowflake/bin/python3
# you will need to set the path to your python 3 env above as mine probably does not exist in your env
# REQUIRES Python 3 + Objc Bridge 

# import modules
import plistlib
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
from Foundation import NSHomeDirectoryForUser

# global vars
USER, UID, GID = SCDynamicStoreCopyConsoleUser(None, None, None)
USERHOME = NSHomeDirectoryForUser(USER)
FILE = USERHOME + '/Library/Preferences/com.apple.HIToolbox.plist'

# script logic
with open(FILE, 'rb') as f:
    data = plistlib.load(f)
    keyboard = data['AppleEnabledInputSources'][0]['KeyboardLayout Name']
    print(f'<result>{keyboard}</result>')

my code results

/opt/snowflake/bin/python3 /Users/tlarkin/IdeaProjects/tlarkin/get_keyboard_layout.py
<result>U.S.</result>

If you are interested in building and shipping Python 3 to your fleet, I used this nifty tool from Greg and started shipping Python 3 to my entire fleet the day after Catalina beta was released in preparation of all the changes and EOL of Python 2.

spoe
New Contributor

For those using osquery-like tools, I added an extended attribute with the following command:

!/bin/sh

keyboardType=$(/usr/local/bin/osqueryi --csv --header=no "select value from plist where path = '/Library/Preferences/com.apple.HIToolbox.plist' and value like '%keylayout%';")

echo "<result>$keyboardType</result>"