Skip to main content
Question

Ext attribute for keyboard layout


Forum|alt.badge.img+13
  • Valued Contributor
  • 268 replies

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

Forum|alt.badge.img+16

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?


Forum|alt.badge.img+13
  • Author
  • Valued Contributor
  • 268 replies
  • November 25, 2011

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.


Forum|alt.badge.img+16

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).


Forum|alt.badge.img+13
  • Author
  • Valued Contributor
  • 268 replies
  • November 25, 2011

*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 ;)


Forum|alt.badge.img+15
  • Valued Contributor
  • 78 replies
  • September 3, 2015

Another "Dead Thread Thursday".....

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


Forum|alt.badge.img+13
  • Author
  • Valued Contributor
  • 268 replies
  • September 7, 2015

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.


Forum|alt.badge.img+12
  • Contributor
  • 529 replies
  • September 9, 2015

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'

Forum|alt.badge.img+13
  • Author
  • Valued Contributor
  • 268 replies
  • September 9, 2015

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 :(


Forum|alt.badge.img+12
  • Contributor
  • 529 replies
  • September 9, 2015

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


Forum|alt.badge.img+3
  • New Contributor
  • 6 replies
  • October 12, 2015

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


Forum|alt.badge.img+13
  • Author
  • Valued Contributor
  • 268 replies
  • October 12, 2015

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


Forum|alt.badge.img+1

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.


Forum|alt.badge.img+31
  • Honored Contributor
  • 2721 replies
  • August 1, 2019

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.


Forum|alt.badge.img+1
  • New Contributor
  • 2 replies
  • January 12, 2021

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>"


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings