Eject key

On a Mac keyboard there is an Eject button.
I wonder if there is a way to detect when that button is pressed.

KeyBoard.AsyncKeyDown with the right scan code is the only native way I can think of
http://documentation.xojo.com/api/hardware/keyboard.html#keyboard-asynckeydown
http://documentation.xojo.com/api/hardware/keyboard.html

I’m sure apple has a document about what that keys code is … somewhere

EDIT : I found a couple stack over flow posts that said the key code is &h92 but that doesnt seem to work here

I found something on Stackoverflow as well…

They are indeed talking about 0x92. But that post was from 2014. So, things might have changed. I wonder why though…

If all you do is put a BREAK statement in Window.KeyDown it wont be reported
And AsyncKeyDown with &h92 in another event, like a Pushbutton action event, doesnt report it as being pressed
This may be one the OS eats long before you ever get a chance to handle it using plain Xojo code

Not sure if you could set up something with MBS CGEvents to catch it
https://www.monkeybreadsoftware.net/class-cgeventtapmbs.shtml
https://www.monkeybreadsoftware.net/class-cgeventsourcembs.shtml
https://www.monkeybreadsoftware.net/class-cgeventmbs.shtml

Sounds like a good start to figuring out how to access it. I usually google for my question, appending MBS to my search query. Christian has everything in his Swiss… er… German Army Knive…

But, since it is very AM already over here, my quest will have to wait until tomorrow…

This key does not exists anymore (since mid-2014 or earler) on MacBook Pros.

It exists elsewhere on Apple made products like the Magic Keyboard

CGEventTapMBS class is there to capture events.
Not sure whether it helps.

I recently bought a new Magic Keyboard, so I can confirm that.
Since I am a big fan of shortcuts I use it often. Like option+command+eject to turn my computer to sleep. So the OS still uses it.
And in the very rare instance of connecting a DVD drive, I use it as well…

I’ll give it a try. I’m sure it helps.

https://developer.apple.com/documentation/appkit/nsevent/1535851-function-key_unicodes

I knew I got the impression you couldn’t detect Fn from somewhere!

I completely forgot:

on the keyboard that do not have an eject key, that key is replaced by the “Power Key”, so the keyboard short cut “Command-Power” allows to shut down the computer :wink:

this post makes me think using an event tap is low level enough to catch it

https://stackoverflow.com/questions/61471518/use-eject-key-as-a-modifier-hotkey-with-hammerspoon

I forgot to mention:

a. my MacBook Pro 13" (mid 2014) does not have a CD-DVD bult-in (thus no needed Eject key).

I connected my BR external Drive to hear music and at a moment I was asking myself “What if I press the Power Key ?” (it was the Eject key prior the CD-DVD removal.

Nothing happened (as expected, but now I am sure).

Neither does my 16" MBP
But when I connect the external Apple DVD to the MBP the eject key on the external keyboard works just fine :slight_smile:

I do not think to do that test on my Bluetooth external keyboard who also have an eject key.

Thanks for the addition.

[quote=491875:@Emile Schwarz]I completely forgot:

on the keyboard that do not have an eject key, that key is replaced by the “Power Key”, so the keyboard short cut “Command-Power” allows to shut down the computer ;)[/quote]
This is to shut the computer down (I guess, at least, since I don’t have such a Mac at hand and I trust what you wrote), but option+command+eject is for sleeping the Mac (I use that every day before going to bed, really a time-saver combination!).

[quote=492237:@Emile Schwarz]I forgot to mention:

a. my MacBook Pro 13" (mid 2014) does not have a CD-DVD bult-in (thus no needed Eject key).

I connected my BR external Drive to hear music and at a moment I was asking myself “What if I press the Power Key?” (it was the Eject key prior the CD-DVD removal.

Nothing happened (as expected, but now I am sure).[/quote]
Yes, these “replaced” keys can be misleading sometimes. It’s still nice that the currently-shown labels are doing what one would expect.

For the sake of mentioning, you can always use these workarounds:
• Eject from the Finder, iTunes or other apps (e.g. Disk Utility)
• Have the “Eject” status menu (I guess it’s still available, right?)
• Write “drutil eject” in Terminal. This way, you can even make a Xojo app to map some keyboard’s keys to this shell function.

[quote=492238:@Norman Palardy]Neither does my 16" MBP
But when I connect the external Apple DVD to the MBP the eject key on the external keyboard works just fine :)[/quote]
On my 2008 Mac Pro (the one I’m using right now, and every day, with Mojave ;-). ), I have 2 IDE DVD drives built-in (they can hardly be found these days).
It happens that the 2nd is the only one of them which can handle RAM DVD discs, which I use fairly often. Option-eject is the key (pun intended) to handle this 2nd drive :wink: