Question

I have a Das Keyboard for Mac that includes an eject key just to the right of the backspace key and to the left of the home key. I never need to use it, and it accidentally press it constantly which causes my CD tray to open. It's really annoying.

Is there anyway to disable this key?

If there's a way to keep the key's other functionality that's accessed by holding various modifiers (for example controlshifteject to put the display to sleep) that would be great. However if there only method disables the key entirely, I'm okay with that.

Note that I'm using Sierra so using Karabiner is not an option.

Was it helpful?

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

OTHER TIPS

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

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

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

While you say "Note that I'm using Sierra so using Karabiner is not an option." you will find that Karabiner Elements, which does run in Sierra, will do what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top