Detect option key state?

Years ago, I think in this forum, a helpful member offered this method to detect whether the option key is pressed:

if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSAlternateKeyMask '") > 1 then
	etc.

If I remember correctly, Apple has warned that python will not be installed in some future version of macOS. Is there an alternative to python for this? I found this method:

optionKeyDown=$(osascript -l JavaScript -e "ObjC.import('Cocoa'); ($.NSEvent.modifierFlags & $.NSEventModifierFlagOption) > 1")

… on this page:

The full script on that page works perfectly, but I haven’t been able to figure out how to adapt one of the single-line command for testing the Option key state into an AppleScript or whether it’s possible to do so.

Any help or advice would be gratefully received.

EDIT: I see that I can create this shell script and then “do shell script etc” from an AppleScript, but I would be glad to know how to incorporate the code directly into the AppleScript:

#!/bin/bash
osascript -l JavaScript -e "ObjC.import('Cocoa'); ($.NSEvent.modifierFlags & $.NSEventModifierFlagOption) > 1"

In ASObjC, it’s:

use AppleScript version "2.3"
use scripting additions
use framework "Foundation"
use framework "AppKit" -- for NSEvent 

(((current application's NSEvent's modifierFlags()) div (current application's NSAlternateKeyMask as integer)) mod 2) > 0

Or a little more simply:

use AppleScript version "2.3"
use scripting additions
use framework "Foundation"
use framework "AppKit" -- for NSEvent 

(((current application's NSEvent's modifierFlags()) div 524288) mod 2) > 0 -- 524288 = current application's NSAlternateKeyMask

That’s perfect, Shane. Thank you!

Hello @ShaneStanley ,

since I am a newbie - especially when it comes to ASObjC - and the fact that I’ve read a lot - the 524288 sounds like something unique for each Modifier is that true?

Else I want to know:

  • What are the “Idendifiers ?!” for the other Modifier-Keys ?
  • If possible - How can I use ASObjC to achieve whether I use the Left or Right Modifier (Command; Option & Shift) ?

I appreciate any approach you are able to make - that would help me a lot

Thanks; Greetings from Germany

Tobias

Hello @emendelson ,

based on this post you made and @ShaneStanley 's Code (#Post 2) I’ve developed my first ASObjC Script which will offer me a lot of fun for Use with Keyboard Maestro …

A Huge thank you for Posting …

If you want to have the Code here it is :


use AppleScript version "2.3" -- Mavericks (10.9) or later
use scripting additions
use framework "Foundation"
use framework "AppKit" -- for NSEvent


-- Defining the output Strings for each Modifier
set fnKeyStr to "Function-Key down : "
set shiftKeyStr to "Shift-Key down : "
set ctrlKeyStr to "Control-Key down : "
set optionKeyStr to "Option-Key down : "
set cmdKeyStr to "Command-Key down : "


-- Function-Key
set fnKeyStat to (((current application's NSEvent's modifierFlags()) div ¬
	(current application's NSFunctionKeyMask as integer)) mod 2) > 0

-- Shift-Key
set shiftKeyStat to (((current application's NSEvent's modifierFlags()) div ¬
	(current application's NSShiftKeyMask as integer)) mod 2) > 0

-- Control-Key
set ctrlKeyStat to (((current application's NSEvent's modifierFlags()) div ¬
	(current application's NSControlKeyMask as integer)) mod 2) > 0

-- Option-Key
set optionKeyStat to (((current application's NSEvent's modifierFlags()) div ¬
	(current application's NSAlternateKeyMask as integer)) mod 2) > 0

-- Command-Key
set cmdKeyStat to (((current application's NSEvent's modifierFlags()) div ¬
	(current application's NSCommandKeyMask as integer)) mod 2) > 0


-- Generate the current Output as List
set modifierKeyStat to fnKeyStr & fnKeyStat & return ¬
	& shiftKeyStr & shiftKeyStat & return ¬
	& ctrlKeyStr & ctrlKeyStat & return ¬
	& optionKeyStr & optionKeyStat & return ¬
	& cmdKeyStr & cmdKeyStat

-- Show the List with the current List
return modifierKeyStat

I’ve written this code with the latest SD7 Version on my old MacBook Pro 13" mid 2010
and it’s extreme fast - depending on what I have running in background the code’s runtime is between 0:00:00.01 & 0:00:00.04 .

The Output looks like this:

Bildschirmfoto 2021-06-10 um 23.40.38

Have fun with this …

Greetings from Germany

Tobias

I’m afraid you can’t do that.

Thank you for your quick and short answer…

for the other Questions about the ID’s I’ve found the answer here in this forums

It’s one of your own Topics:

Many Many Thanks for tis Topic Shane… (hope you don’t be afraid about only using your first name… :scream:)

Greetings from Germany

Tobias