How can I disable the eject key on my mac keyboard?

Solution 1:

I created an app, PowerKey, that allows you to remap the Power and Eject keys on Mac keyboards.

Pick a key replacement such as Forward Delete, or use No Action to disable the key entirely.

You can also launch Apple Scripts or bash scripts via the key.

PowerKey app icon

https://github.com/pkamb/PowerKey

remap options

https://github.com/pkamb/PowerKey

Solution 2:

I actually had Karabiner Elements working great with Sierra on my older Mac Pro - until a recent OS update. I assigned the F6 key to "eject", and that disabled the regular "eject" key. Karabiner also had a recent update (a couple days ago) that appeared to default all my keyboard mods back to standard Apple. Went back to an earlier version in backup, and it didn't fix the problem. Now, I still assign the F6 to "Eject" (and it works fine there), but the "Eject" button is no longer disabled. Did a lot of searching on the net and it seems that Apple has us on this one for now. At this point, I might try making a cover that fits over that corner, so when I accidentally hit the key, it doesn't work.

This is my lo-tech solution.enter image description here

Solution 3:

Karabiner-Elements has been updated and allows me to disable the eject key on macOs 10.13.6 - High Sierra without issue: https://pqrs.org/osx/karabiner/index.html

Screenshot of the simple modification

Solution 4:

I ended up writing a custom application to filter out eject key presses. It uses an event tap to do so. Here's a minimally working example that demonstrates how to do it:

#include <stdio.h>
#include <ApplicationServices/ApplicationServices.h>

static CFMachPortRef machPortRef = NULL;

CGEventRef specialKeyEventTapCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
{
    if (type == kCGEventTapDisabledByTimeout) {
        CGEventTapEnable(machPortRef, true);
        return event;
    }

    uint64_t subtype = CGEventGetIntegerValueField(event, 99);

    if (subtype == 8) {
        static CGEventFlags allModifiers = (kCGEventFlagMaskShift | kCGEventFlagMaskControl | kCGEventFlagMaskAlternate | kCGEventFlagMaskCommand);
        int keycode = ((CGEventGetIntegerValueField(event, 149) & 0xFFFF0000) >> 16);
        CGEventFlags flags = CGEventGetFlags(event);

        if (keycode == NX_KEYTYPE_EJECT && (flags & allModifiers) == 0) {
            // filter eject key when no modifiers are pressed
            return NULL;
        }
    }

    return event;
}

int main()
{
    CFRunLoopSourceRef eventSrc = NULL;

    machPortRef =  CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, CGEventMaskBit(NX_SYSDEFINED), (CGEventTapCallBack)specialKeyEventTapCallback, NULL);

    if (machPortRef == NULL) {
        fprintf(stderr, "CGEventTapCreate failed!\n");
        return 1;
    }

    eventSrc = CFMachPortCreateRunLoopSource(NULL, machPortRef, 0);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), eventSrc, kCFRunLoopDefaultMode);

    CFRunLoopRun();

    CFRelease(machPortRef);
    CFRelease(eventSrc);
}