Skip to content

Instantly share code, notes, and snippets.

@Grayson
Created August 18, 2011 14:03
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Grayson/1154126 to your computer and use it in GitHub Desktop.
Save Grayson/1154126 to your computer and use it in GitHub Desktop.
Applescript snippet to test if Option key is held down.
on isOptionKeyPressed()
return (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSAlternateKeyMask > 1'") is "True"
end isOptionKeyPressed
@mrtoner
Copy link

mrtoner commented Apr 13, 2013

Thanks, Grayson, very helpful.

@identd113
Copy link

identd113 commented May 24, 2016

on isModifierKeyPressed()

set modiferKeysDOWN to {command_down:false, option_down:false, control_down:false, shift_down:false}

if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSAlternateKeyMask '") > 1 then
    set option_down of modiferKeysDOWN to true
end if

if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSCommandKeyMask '") > 1 then
    set command_down of modiferKeysDOWN to true
end if

if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSShiftKeyMask '") > 1 then
    set shift_down of modiferKeysDOWN to true
end if

if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa. NSControlKeyMask '") > 1 then
    set control_down of modiferKeysDOWN to true
end if

return modiferKeysDOWN
end isModifierKeyPressed

@kennethrichardson
Copy link

kennethrichardson commented Jan 28, 2018

Hello All,
The post by identd113 was a saving grace when I needed to know how to do this. Here is my gift back to you. This script also tests for the caps lock and the fn key (on a laptop) and is significantly faster. You can paste the text below into the script editor and run it to test it. Enjoy.

display dialog "start"

-- bit 16 = CAPS LOCK
-- bit 17 = shift (131072)
-- bit 18 = ctrl (262144)
-- bit 19 = option (524288)
-- bit 20 = command (1048576)
-- bit 23 = fn laptop key (8388608)
-- by Ken Richardson

on getModifierKeyBits()
set modifierKeysDOWN to {command_down:false, option_down:false, control_down:false, shift_down:false, fn_down:false, capslock_down:false}

set modifierBits to do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags()'"
set modifierBits to modifierBits * 1
if (modifierBits > 0) then
	if (modifierBits > 8388607) then
		-- fn key is pressed, subtract it away
		set modifierBits to modifierBits - 8388608
		set fn_down of modifierKeysDOWN to true
	end if
	if (modifierBits > 1048575) then
		-- command key is pressed, subtract it away
		set modifierBits to modifierBits - 1048576
		set command_down of modifierKeysDOWN to true
	end if
	if (modifierBits > 524287) then
		-- option key is pressed, subtract it away
		set modifierBits to modifierBits - 524288
		set option_down of modifierKeysDOWN to true
	end if
	if (modifierBits > 262143) then
		-- ctrl key is pressed, subtract it away
		set modifierBits to modifierBits - 262144
		set control_down of modifierKeysDOWN to true
	end if
	if (modifierBits > 131071) then
		-- shift key is pressed, subtract it away
		set modifierBits to modifierBits - 131072
		set shift_down of modifierKeysDOWN to true
	end if
	if (modifierBits > 65535) then
		-- capslock key is pressed, subtract it away
		set modifierBits to modifierBits - 65536
		set capslock_down of modifierKeysDOWN to true
	end if
end if
return modifierKeysDOWN

end getModifierKeyBits
set answer to getModifierKeyBits()

@jimsmart
Copy link

jimsmart commented Feb 8, 2018

Thanks for this! — Since Yosemite (I think), it's now possible to call ObjC from any AppleScript, so the shell call to Python can be eliminated for a further performance gain. This is what I'm using now:-

use framework "Cocoa"
on getModifierKeyBits()
	set modifierKeysDOWN to {command_down:false, option_down:false, control_down:false, shift_down:false, fn_down:false, capslock_down:false}
	set modifierBits to current application's NSEvent's |modifierFlags|()
	set modifierBits to modifierBits * 1
	if (modifierBits > 0) then
		if (modifierBits > 8388607) then
			-- fn key is pressed, subtract it away
			set modifierBits to modifierBits - 8388608
			set fn_down of modifierKeysDOWN to true
		end if
		if (modifierBits > 1048575) then
			-- command key is pressed, subtract it away
			set modifierBits to modifierBits - 1048576
			set command_down of modifierKeysDOWN to true
		end if
		if (modifierBits > 524287) then
			-- option key is pressed, subtract it away
			set modifierBits to modifierBits - 524288
			set option_down of modifierKeysDOWN to true
		end if
		if (modifierBits > 262143) then
			-- ctrl key is pressed, subtract it away
			set modifierBits to modifierBits - 262144
			set control_down of modifierKeysDOWN to true
		end if
		if (modifierBits > 131071) then
			-- shift key is pressed, subtract it away
			set modifierBits to modifierBits - 131072
			set shift_down of modifierKeysDOWN to true
		end if
		if (modifierBits > 65535) then
			-- capslock key is pressed, subtract it away
			set modifierBits to modifierBits - 65536
			set capslock_down of modifierKeysDOWN to true
		end if
	end if
	return modifierKeysDOWN
end getModifierKeyBits

HTH!

@scriptFactors
Copy link

what about this:

on getModKeys()
"/usr/bin/python -c 'import Cocoa;k="";a=Cocoa.NSEvent.modifierFlags()/65536
if a>31 : k=k+"function";a=a-32
if a>15 : k=k+"command";a=a-16
if a>7 : k=k+"option";a=a-8
if a>3 : k=k+"control";a=a-4
if a>1 :k=k+"shift";a=a-2
if a>0 :k=k+"capslock";print(k)'"
do shell script result
return result
end getModKeys

getModKeys()
if "shift" is in result then display dialog "shift key pressed"

@moritzz
Copy link

moritzz commented Nov 26, 2018

Thanks @jimsmart !

@EllieDaddy
Copy link

EllieDaddy commented Oct 27, 2020

Hi guys,

Thanks for your examples. I learned more about detecting key pressed.

@kennethrichardson's code works well. And he explained the founctionality of it. But I found key up, down, left and right will trigger the code. I modified a little bit to avoid this situation. I hope it is helpful for guys who face same case.

It works well in my Mac (Catalina 10.15.7).

Thanks.

on getModifierKeyBits()
	set modifierKeysDOWN to {command_down:false, option_down:false, control_down:false, shift_down:false, fn_down:false, capslock_down:false}
	set modifierBits to do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags()'"
	set modifierBits to modifierBits * 1
	if (modifierBits > 0) then
		if (modifierBits > 8388607) then
			if (modifierBits < 8388609) then
				-- fn key is pressed, subtract it away
				set modifierBits to modifierBits - 8388608
				set fn_down of modifierKeysDOWN to true
			end if
		end if
		if (modifierBits > 1048575) then
			if (modifierBits < 1048577) then
				-- command key is pressed, subtract it away
				set modifierBits to modifierBits - 1048576
				set command_down of modifierKeysDOWN to true
			end if
		end if
		if (modifierBits > 524287) then
			if (modifierBits < 524289) then
				-- option key is pressed, subtract it away
				set modifierBits to modifierBits - 524288
				set option_down of modifierKeysDOWN to true
			end if
		end if
		if (modifierBits > 262143) then
			if (modifierBits < 262145) then
				-- ctrl key is pressed, subtract it away
				set modifierBits to modifierBits - 262144
				set control_down of modifierKeysDOWN to true
			end if
		end if
		if (modifierBits > 131071) then
			if (modifierBits < 131073) then
				-- shift key is pressed, subtract it away
				set modifierBits to modifierBits - 131072
				set shift_down of modifierKeysDOWN to true
			end if
		end if
		if (modifierBits > 65535) then
			if (modifierBits < 65537) then
				-- capslock key is pressed, subtract it away
				set modifierBits to modifierBits - 65536
				set capslock_down of modifierKeysDOWN to true
			end if
		end if
	end if
	return modifierKeysDOWN
end getModifierKeyBits

@alin89c
Copy link

alin89c commented Sep 22, 2021

I use very often lines that start with do shell script in my scripts. Unfortunately, the script won't compile if it contains the use framweork "Cocoa" line. Is there anything I can do about it? I've read that I need to use an NSTask object instead, but I can't figure out the mechanics of it.

This is the code that doesn't work.

use framework "Cocoa"
do shell script "eval $(/usr/libexec/path_helper -s); sendmidi dev SessionKiano ch 2 on 109 127"

Thank you.

EDIT:
I've found the solution:
According to this, I just needed to add below "use framework..." another line: use scripting additions.
Hurray!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment