Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Disable Mac OS X Keyboard (built-in only)
#!/bin/bash
# Unload the keyboard so I can use my external keyboard
# and not worry about accidentally pressing buttons on
# the built-in. This may not be a problem for you but, you
# should see my desk sometimes (Oh no!!) ;-)
sudo kextunload /System/Library/Extensions/AppleUSBTopCase.kext/Contents/PlugIns/AppleUSBTCKeyboard.kext/
@stembrain

This comment has been minimized.

Copy link

@stembrain stembrain commented Sep 19, 2013

How to reload the kb?
sude kextload ?

@ptim

This comment has been minimized.

Copy link

@ptim ptim commented Oct 27, 2013

sudo kextload /System/Library/Extensions/AppleUSBTopCase.kext/Contents/PlugIns/AppleUSBTCKeyboard.kext/

(apparently! http://hintsforums.macworld.com/showpost.php?s=3c2007699e473182d749410f2fceb2aa&p=670405&postcount=10 )

@john2x

This comment has been minimized.

Copy link

@john2x john2x commented Dec 20, 2013

Does this still work for you on OS X Mavericks 10.9.1? It used to work for me before, but now I'm getting:

(kernel) Can't unload kext com.apple.driver.AppleUSBTCKeyboard; classes have instances:
(kernel)     Kext com.apple.driver.AppleUSBTCKeyboard class AppleUSBTCKeyboard has 1 instance.
Failed to unload com.apple.driver.AppleUSBTCKeyboard - (libkern/kext) kext is in use or retained (cannot unload).
@shinriyo

This comment has been minimized.

Copy link

@shinriyo shinriyo commented Nov 14, 2014

I can' too
mine 10.9.5

@xinhaoyuan

This comment has been minimized.

Copy link

@xinhaoyuan xinhaoyuan commented Dec 10, 2014

Despite the error, the internal keyboard seems disabled after the command.

os version 10.9.5

@GregHorn27

This comment has been minimized.

Copy link

@GregHorn27 GregHorn27 commented Mar 1, 2015

I get the same error, but my keyboard is still enabled. OSX 10.10.2. Below is what the error says. Any help is appreciated.

(kernel) Kext com.apple.driver.AppleUSBTCKeyboard not found for unload request.
Failed to unload com.apple.driver.AppleUSBTCKeyboard - (libkern/kext) not found.

@astrohfiziks

This comment has been minimized.

Copy link

@astrohfiziks astrohfiziks commented Jul 17, 2015

Great work. Even though I still get the error, it works beautifully. Thank you. Is there a way to make a button out of this snippet? I use this at least once a day since my built-in keyboard has a mysterious shortage.

@arthurnn

This comment has been minimized.

Copy link

@arthurnn arthurnn commented Oct 30, 2015

for the ones looking for a way to disable the internal keyboard because you have a external one attached you can use this https://pqrs.org/osx/karabiner/gallery.html.en#disable-built-in-keyboard-if-external-keyboard-exists

also i tested the code about on OSX 10.11 and the unload works but i cannot load the kernel extension back and it only works if I reboot my computer

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Apr 26, 2016

@arthurnn: Kudos to you!

@xyoun

This comment has been minimized.

Copy link

@xyoun xyoun commented May 29, 2016

@arthurnn: awesome!

@jakevossen5

This comment has been minimized.

Copy link

@jakevossen5 jakevossen5 commented Jul 12, 2016

WARNING:

Running this code on 10.11.3 crashed my mac. Just turned off and gave me the "something went wrong with your mac error" before restarting twice, and turning back on fine (with they keyboard enabled)

Looks like this solution doesn't work anymore.

@rahul1990gupta

This comment has been minimized.

Copy link

@rahul1990gupta rahul1990gupta commented Aug 21, 2020

It used to work for me on Sierra. After I upgraded my OS to catalina, It stopped working. It gives me this error.

(kernel) Can't remove kext com.apple.driver.AppleUSBTCKeyboard; services failed to terminate - 0xe00002c7.
Failed to unload com.apple.driver.AppleUSBTCKeyboard - (iokit/common) unsupported function.
@mike-lloyd03

This comment has been minimized.

Copy link

@mike-lloyd03 mike-lloyd03 commented Aug 24, 2020

I'm getting the same error as @rahul1990gupta

@jm9151821

This comment has been minimized.

Copy link

@jm9151821 jm9151821 commented Oct 23, 2020

Im avin te same issue, did anyone ind a soution to tis

@orvn

This comment has been minimized.

Copy link

@orvn orvn commented Apr 7, 2021

  • Unload native keyboard: sudo kextunload -v 5 /System/Library/Extensions/AppleUSBTopCase.kext/Contents/PlugIns/AppleUSBTCKeyboard.kext/
  • Reload native keyboard: sudo kextload -v 5 /System/Library/Extensions/AppleUSBTopCase.kext/Contents/PlugIns/AppleUSBTCKeyboard.kext/

This does work on new versions of MacOS, but only when System Integrity Protection (SIP) is not affecting the context. SIP was introduced in El Capitan and got some upgrades in Mojave, so all newer MacOS will have this enabled: csrutil status

  • On El Capitan, Sierra and High Sierra, the kernel extension changes will throw an error due to some perms being protected, but the keyboard disabling does work
  • On Mojave, Catalina, Big Sur (and beyond), SIP protections won't even let a sudoer see com.apple.driver.AppleUSBTCKeyboard, so the kextload finds nothing to unload

if you're on Mojave and above, running csrutil disable would "fix" the issue, but there are three caveats:

  1. SIP can't be disabled from the active OS instance itself, so the easiest solution is to boot from the recovery partition (cmd + r on boot) and run csrutil disable from there
  2. You must re-enable (via kextload) your built-in keyboard, or use a non-bluetooth keyboard, if you want to enter recovery mode at a later date, after disabling your keyboard with kextunload
  3. You are disabling MacOS's native protections for system-level directories, which technically would allow a 3rd party service to inject malicious code in a number of ways. Technically the app would need sudo permissions first to do that though. This is easy enough to protect and manage on a *nix server somewhere because you (should) have protections in place and a strict policy on who can su or sudo and when. But on an os with a GUI, with applications being closed, opened, installed, etc. constantly, it's not so easy to ensure something doesn't slip in.

TBD:

  • Is there a way to limit SIP's strictness without reboot/recovery (like selinux config)? The answer is a soft "no" per the official docs. But there is an interesting output of System Integrity Protection status: enabled (Custom Configuration). This could suggest that it may be possible to selectively disable SIP features and set a custom config, but no suggestion on how this could be done is exposed in the docs. If someone knows more about this, lmk (/usr/bin/csrutil is a binary in mach-o format by the way)
  • How do 3rd parties like Karabiner-Elements accomplish disabling the keyboard? Perhaps they don't even need to touch the kernel extensions. It was implemented in this issue, v0.90.63, but I can't find a release with that tag so I don't see the commit. Searching through the codebase doesn't show anything too obvious re: kext. Any insight on that would be helpful too.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment