Saturday, December 16, 2017

#1 2010-07-21 05:49:56 pm

oldmanegan
Member
From:: Tampa, FL (sometimes)
Registered: 2009-08-10
Posts: 16

Detect Modifier Key down

I was reading through a forum post elsewhere and came upon the app this uses. I took a basic script an tested it for options and thought it may be of use to some.
It detects a modifier key (Option, Shift, etc.) down. Can be set to only detect a specific key or to report any modifier key.
(modified to include control key and explain the results better in the comments so it is self contained mostly)

Applescript:



------ Depends on getModKey app from Allan Craig
--- [url]http://allancraig.net/index.php?option=com_docman&Itemid=100[/url]
-- run this in an idle handler to get keys down or in some other fashion to capture a key down when the do shell executes
-- if you append to the quoted text at the end of the 'do shell' line a space and then the mod key name (such as 'option'), it will provide a boolean result on the status of the particular mod key state
-- I have provided several keys and combinations below, notice that they are additive in nature (see the combinations with the Option key)
-- I have the log message so that you can see the numeric value of the result returned, either the key value or the boolean

set ModKeyDown to my ModKeyDownCheck()
-----
set ModKeyAlert to ""
if ModKeyDown > 1 then
   if ModKeyDown = 256 then
       set ModKeyAlert to "Command key pressed"
   else if ModKeyDown = 512 then
       set ModKeyAlert to "Shift key pressed"
   else if ModKeyDown = 1024 then
       set ModKeyAlert to "Caps Lock on"
   else if ModKeyDown = 2048 then
       set ModKeyAlert to "Option key pressed"
   else if ModKeyDown = 2560 then -- note the key combinations cause addition of their values
       set ModKeyAlert to "Shift & Option keys pressed"
   else if ModKeyDown = 2816 then
       set ModKeyAlert to "Shift, Option & Command keys pressed"
   else if ModKeyDown = 3072 then
       set ModKeyAlert to "Caps Lock on & Option key pressed"
   else if ModKeyDown = 4096 then
       set ModKeyAlert to "Control key pressed"
   else if ModKeyDown = 4352 then
       set ModKeyAlert to "Control and Command keys pressed"
   else if ModKeyDown = 131072 then
       set ModKeyAlert to "Function key pressed"
   else if ModKeyDown = 133120 then
       set ModKeyAlert to "Function & Option keys pressed"
   end if
end if
-----
if ModKeyAlert ≠ "" then
   display alert ModKeyAlert
end if
-----
on ModKeyDownCheck()
   set ModKeyDown to do shell script "/Library/UnixApps/getModKey"
   (* use this if you wish only to monitor for Option key down:
   set ModKeyDown to do shell script "/Library/UnixApps/getModKey option"
   *)

   set ModKeyDown to ModKeyDown as integer
   log ModKeyDown
   -- set ModKeyDown to modKeyDown as boolean
   return ModKeyDown
end ModKeyDownCheck
-----

Model: MacBook (early 2009)
Browser: Safari 533.16
Operating System: Mac OS X (10.6)

Last edited by oldmanegan (2010-07-22 07:39:59 am)

Offline

 

#2 2010-07-22 04:42:06 am

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Detect Modifier Key down

Hello.

I just want to add that StefanK has made an alternative version that I find easier to use since you can specify the keys you want to listen to on the command line, and gets a true result if that condition of the keys were true at that moment. I'm not saying that the one is better than the other, just that there is an alternative. The version you describe is the obvious choice when listening for function keys. But can you get out which function key was pressed?

[code]checkModifierKeys(1)      BSD General Commands Manual     checkModifierKeys(1)

NAME
     checkModifierKeys, -- Modifier Key listener for the Mac Os X

SYNOPSIS
     checkModifierKeys, [shift | cmd | control | option | capslock]

DESCRIPTION
     checkModifierKeys, Returns "1" to standard output if one of or more of
     the modifierkeys in the argumentlist were held down under execution or
     "0" if not.

AUTHOR
     Stefan Klieme

Darwin                           July 22, 2010                          Darwin[/code]
You can download StefanK's checkModifierKeys from here

Last edited by McUsr (2010-07-22 04:43:32 am)


Mercurial vcs is a joy to use for scripting.


Filed under: Modifier keys

Offline

 

#3 2010-07-22 06:22:20 am

oldmanegan
Member
From:: Tampa, FL (sometimes)
Registered: 2009-08-10
Posts: 16

Re: Detect Modifier Key down

Thanks.
Yes, with the method I listed one can get the specific mod key pressed, including key combinations, if the button parameter is left off of the do shell command (is shown this way in the script without he param option commented out).
I check out Stefan's method as well.

Offline

 

#4 2010-07-22 07:21:16 am

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Detect Modifier Key down

Hello.

The method you mention has great advantages, when you want to poll for different key combinations, as you will get by with one single do shell script command. That may be an advantage in some situations.

I checked out your alternative as well. Do you happen to know where I find the opcodes for the keys?
I really need the combinations for the control key as well.

Thanks in advance.

Last edited by McUsr (2010-07-22 07:26:37 am)


Mercurial vcs is a joy to use for scripting.


Filed under: Modifier keys

Offline

 

#5 2010-07-22 07:42:33 am

oldmanegan
Member
From:: Tampa, FL (sometimes)
Registered: 2009-08-10
Posts: 16

Re: Detect Modifier Key down

I have updated to include control (I ran it when the control key was down and looked in the log for the result). I did not know the opcodes originally, just acquired them through logging results as the script ran.

I have improved the comments to make it more easily understood (I hope).

The script is mostly the comments and the dialog to display the results of the do shell command. Otherwise it would be quite small.

Offline

 

#6 2010-07-22 07:50:44 am

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Detect Modifier Key down

Hello.

That didn't cross my mind at the moment. smile Thanks!

I re found the utility keycodes at Peter Maurers site (download link).
So I can easily test for the combinations I am interested in. -But I like the AS EventLog. smile

Last edited by McUsr (2010-07-22 07:52:50 am)


Mercurial vcs is a joy to use for scripting.


Filed under: Modifier keys

Offline

 

#7 2014-01-30 08:08:33 am

Jost
Member
Registered: 2005-12-12
Posts: 4

Re: Detect Modifier Key down

Thanks! Very useful form som of my applets. smile

Offline

 

#8 2016-05-24 05:16:13 pm

oscar
Member
Registered: 2007-06-13
Posts: 11

Re: Detect Modifier Key down

This should also tell you they modifier keys that are down, using "vanilla" Applescript

Applescript:


property vers : "1.0"

on isModifierKeyPressed(checkKey)
   set modiferKeysDOWN to {command_down:false, option_down:false, control_down:false, shift_down:false, caps_down:false, numlock_down:false, function_down:false}
   
   if checkKey = "" or checkKey = "option" or checkKey = "alt" then
       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
   end if
   
   if checkKey = "" or checkKey = "command" then
       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
   end if
   
   if checkKey = "" or checkKey = "shift" then
       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
   end if
   
   if checkKey = "" or checkKey = "control" then
       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
   end if
   
   if checkKey = "" or checkKey = "caps" or checkKey = "capslock" then
       if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSAlphaShiftKeyMask '") > 1 then
           set caps_down of modiferKeysDOWN to true
       end if
   end if
   
   if checkKey = "" or checkKey = "numlock" then
       if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSNumericPadKeyMask'") > 1 then
           set numlock_down of modiferKeysDOWN to true
       end if
   end if
   --Set if any key in the numeric keypad is pressed. The numeric keypad is generally on the right side of the keyboard. This is also set if any of the arrow keys are pressed
   
   if checkKey = "" or checkKey = "function" or checkKey = "func" or checkKey = "fn" then
       if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSFunctionKeyMask'") > 1 then
           set function_down of modiferKeysDOWN to true
       end if
   end if
   --Set if any function key is pressed. The function keys include the F keys at the top of most keyboards (F1, F2, and so on) and the navigation keys in the center of most keyboards (Help, Forward Delete, Home, End, Page Up, Page Down, and the arrow keys)
   
   return modiferKeysDOWN
end isModifierKeyPressed

You could call this by doing
my isModifierKeyPressed(""), and this will return all current modifier keys pulled as a record.

You can also specify which key to look at, which runs faster.
my isModifierKeyPressed("option")

So, an example:

display dialog command_down of my isModifierKeyPressed("command")
This would show true if the command key is being held down.

if you specify a key, all others will return as false.

EDIT:
Updated code, and examples

Last edited by oscar (2016-05-28 07:21:55 pm)

Offline

 

#9 2016-05-24 06:30:08 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4456

Re: Detect Modifier Key down

I don't know if I'd call Python-invoking shell scripts "vanilla AppleScript", but that's very handy code. Thanks.  smile

The Python stuff is recognisably just making system calls, so I suppose the same thing could be achieved with less overhead using ASObjC:

Applescript:

use framework "Foundation"
use framework "AppKit"

on modifierKeysPressed()
   set modifierKeysDOWN to {command_down:false, option_down:false, control_down:false, shift_down:false}
   
   set |⌘| to current application
   set currentModifiers to |⌘|'s class "NSEvent"'s modifierFlags()
   
   tell modifierKeysDOWN
       set its option_down to (currentModifiers div (get |⌘|'s NSAlternateKeyMask) mod 2 is 1)
       set its command_down to (currentModifiers div (get |⌘|'s NSCommandKeyMask) mod 2 is 1)
       set its shift_down to (currentModifiers div (get |⌘|'s NSShiftKeyMask) mod 2 is 1)
       set its control_down to (currentModifiers div (get |⌘|'s NSControlKeyMask) mod 2 is 1)
   end tell
   
   return modifierKeysDOWN
end modifierKeysPressed

modifierKeysPressed()


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)